读取json配置文件,实现数据驱动测试

json文件格式:

文件中一共有三组数据,也对应着三个测试用例:注册成功、用户名已经存在、邮件已存在。

{
  "test1-注册成功": {
    "username": "test",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "注册成功"
  },
  "test2-用户名已经存在": {
    "username": "test",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "用户名已经存在"
  },
  "test3-邮件已存在": {
    "username": "test1",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "邮件已经存在"
  }
}

在测试用例中读取json文件,驱动测试执行:

①导入ddt模块包:import ddt
②使用@ddt.ddt修饰类
③使用@ddt.file_data(“json文件路径”)修饰测试用例方法。
④测试用例方法的参数的名字需要与json中的key值保持一致,否则无法正确传递参数值。
当前测试用例类使用的框架是unittest框架。

# coding: utf-8
import unittest
import ddt
import os
from interface.registerInterface import RegisterInterface


@ddt.ddt
class RegisterTest(unittest.TestCase):
    '''注册'''
    @classmethod
    def setUpClass(cls) -> None:
        cls.url = 'http://localhost:8080/jwshoplogin/user/register.do'
        cls.registerInterface = RegisterInterface(cls.url)
        pass

    def setUp(self) -> None:
        pass

    def tearDown(self) -> None:
        # print("用例执行通过。")
        pass

    @classmethod
    def tearDownClass(cls) -> None:
        pass

    @ddt.file_data(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'jsondata') + r'\registerTest.json')
    def test(self, username, password, email, phone, question, answer, exceptionresult):
        userinfo = {
            "username": username,
            "password": password,
            "email": email,
            "phone": phone,
            "question": question,
            "answer": answer
        }
        self.registerInterface.aw_send_register_request(userinfo)
        result = self.registerInterface.aw_check_result(exceptionresult)
        self.assertEqual(result, True, "测试失败,预期结果为{0},实际为{1}".format(exceptionresult, result))
        pass


if __name__ == '__main__':
    unittest.main()

上述类中引用的RegisterInterface类:

# coding: utf-8
import requests

'''1、username:test2
2、password:123456
3、email:[email protected]
4、phone:13549960321
5、question:我最喜欢的宠物是?
6、answer:cat
'''


class RegisterInterface(object):
    def __init__(self, url):
        self.url = url
        self.response = None
        pass

    def aw_send_register_request(self, userinfo):
        self.response = requests.post(self.url, data=userinfo)
        # print(self.response.text)
        pass

    def aw_check_result(self, exception):
        if exception in self.response.text:
            return True
        else:
            if self.response.json()['data']:
                return self.response.json()['data']
            else:
                return self.response.json()['msg']

执行测试用例:

虽然在RegisterTest测试用例中只书写了一个test方法,但执行的用例个数却有3个,这是因为json数据文件中含有3组数据,这就是ddt数据驱动测试的魅力所在,大大提高了代码的复用率,而且如果需要增加同样请求过程的测试用例,只需要在json文件中增加一组测试数据即可,无需修改代码。
读取json配置文件,实现数据驱动测试_第1张图片
增加一组数据至json文件中:

{
  "test1-注册成功": {
    "username": "test",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "注册成功"
  },
  "test2-用户名已经存在": {
    "username": "test",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "用户名已经存在"
  },
  "test3-邮件已存在": {
    "username": "test1",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335433",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "邮件已经存在"
  },
  "test4-注册成功": {
    "username": "test1",
    "password": "123456",
    "email": "[email protected]",
    "phone": "13425335411",
    "question": "我最喜欢的宠物是?",
    "answer": "cat",
    "exceptionresult": "注册成功"
  }
}

执行结果:
读取json配置文件,实现数据驱动测试_第2张图片

你可能感兴趣的:(python,python)