pytest学习和使用12-Unittest和Pytest参数化详解
Posted 虫无涯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytest学习和使用12-Unittest和Pytest参数化详解相关的知识,希望对你有一定的参考价值。
12-Unittest和Pytest参数化详解
1 Unittest参数化
1.1 ddt
1.1.1 简介
- 数据驱动ddt可以实现测试数据与测试脚本的分离;
- 通过ddt来将测试数据加载到脚本中;
1.1.2 说明
- 测试数据为嵌套字典的列表;
- 测试类前加修饰
@ddt
; - 测试用例前加修饰
@data()
- 运行后用例会自动加载成多个单独的用例。
1.1.3 安装
pip install ddt
1.1.4 版本信息
C:\\Users\\Administrator>pip show ddt
Name: ddt
Version: 1.4.2
Summary: Data-Driven/Decorated Tests
Home-page: https://github.com/datadriventests/ddt
Author: Carles Barrobés
Author-email: carles@barrobes.com
License: UNKNOWN
Location: d:\\python37\\lib\\site-packages
Requires:
Required-by:
1.1.5 实例1
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_ddt.py
# 作用:unittest数据驱动ddt
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
from ddt import *
test_case = ["data": "name": "NoamaNelson", "pwd": "123456", "info": "msg": "登陆成功", "code": "200",
"data": "name": "noama", "pwd": "123456", "info": "msg": "登陆失败,用户名或密码错误!", "code": "201",
"data": "name": "", "pwd": "123456", "info": "msg": "用户名不能为空!", "code": "201",
"data": "name": "NoamaNelson", "pwd": "", "info": "msg": "密码不能为空!", "code": "201",
"data": "name": "", "pwd": "", "info": "msg": "用户名和密码不能为空!", "code": "201"]
@ddt
class TestCase(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("打开浏览器进入登陆界面")
@classmethod
def tearDownClass(cls) -> None:
print("关闭退出浏览器")
def login(self, name, pwd):
if name == "NoamaNelson" and pwd == "123456":
return "msg": "登陆成功", "code": "200"
elif name == "noama" and pwd == "123456":
return "msg": "登陆失败,用户名或密码错误!", "code": "201"
elif name == "" and pwd == "123456":
return "msg": "用户名不能为空!", "code": "201"
elif name == "NoamaNelson" and pwd == "":
return "msg": "密码不能为空!", "code": "201"
elif name == "" and pwd == "":
return "msg": "用户名和密码不能为空!", "code": "201"
else:
return False
@data(*test_case)
def test_case_data(self, case):
print(f"case:case")
print(f"case[info]:case['info']")
print(f"返回值为:self.login(case['data']['name'], case['data']['pwd'])")
self.assertEqual(case['info'], self.login(case['data']['name'], case['data']['pwd']))
if __name__ == '__main__':
unittest.main()
test_unittest_ddt.py::TestCase::test_case_data_1 打开浏览器进入登陆界面
PASSED
[ 20%]case:'data': 'name': 'NoamaNelson', 'pwd': '123456', 'info': 'msg': '登陆成功', 'code': '200'
case[info]:'msg': '登陆成功', 'code': '200'
返回值为:'msg': '登陆成功', 'code': '200'
test_unittest_ddt.py::TestCase::test_case_data_2 PASSED
[ 40%]case:'data': 'name': 'noama', 'pwd': '123456', 'info': 'msg': '登陆失败,用户名或密码错误!', 'code': '201'
case[info]:'msg': '登陆失败,用户名或密码错误!', 'code': '201'
返回值为:'msg': '登陆失败,用户名或密码错误!', 'code': '201'
test_unittest_ddt.py::TestCase::test_case_data_3 PASSED
[ 60%]case:'data': 'name': '', 'pwd': '123456', 'info': 'msg': '用户名不能为空!', 'code': '201'
case[info]:'msg': '用户名不能为空!', 'code': '201'
返回值为:'msg': '用户名不能为空!', 'code': '201'
test_unittest_ddt.py::TestCase::test_case_data_4 PASSED
[ 80%]case:'data': 'name': 'NoamaNelson', 'pwd': '', 'info': 'msg': '密码不能为空!', 'code': '201'
case[info]:'msg': '密码不能为空!', 'code': '201'
返回值为:'msg': '密码不能为空!', 'code': '201'
test_unittest_ddt.py::TestCase::test_case_data_5 PASSED
[100%]case:'data': 'name': '', 'pwd': '', 'info': 'msg': '用户名和密码不能为空!', 'code': '201'
case[info]:'msg': '用户名和密码不能为空!', 'code': '201'
返回值为:'msg': '用户名和密码不能为空!', 'code': '201'
关闭退出浏览器
1.1.6 实例2
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_ddt1.py
# 作用:unittest数据驱动ddt
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
from ddt import *
num = ["zhangsan": 10,
"lisi": 20,
"wangwu": 30,
"zhaoliu": 40]
lsit_num = [10, 20, 30, 40]
@ddt
class TestCase(unittest.TestCase):
@data(*num)
def test_case_data(self, case):
m = [i for i in case.values()]
print(f"case:m[0]")
self.assertIn(m[0], lsit_num)
if __name__ == '__main__':
unittest.main()
Ran 4 tests in 0.000s
OK
case:10
case:20
case:30
case:40
1.2 paramunittest
1.2.1 说明
paramunittest
参数化,传入的参数类型可以是元组,列表,字典,对象,函数;- 通过
@paramunittest.parametrized
装饰器传给调用者; - 通过
paramunittest.ParametrizedTestCase
执行测试案例; - 通过通过
setParameters
方法接收装饰器传递过来的参数。
1.2.2 安装
pip install paramunittest
1.2.3 版本信息
C:\\Users\\Administrator>pip show paramunittest
Name: ParamUnittest
Version: 0.2
Summary: Simple extension to have parametrized unit tests.
Home-page: https://github.com/rik0/ParamUnittest
Author: Enrico Franchi
Author-email: enrico.franchi@gmail.com
License: BSD
Location: d:\\python37\\lib\\site-packages
Requires:
Required-by:
1.2.3 实例:参数传入元组数据
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
# 传入元组
@paramunittest.parametrized(
(4, 5),
(6, 7)
)
class TestCase(paramunittest.ParametrizedTestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case(self):
print(self.n1, self.n2)
self.assertGreater(self.n2, self.n1)
if __name__ == '__main__':
unittest.main()
Ran 2 tests in 0.002s
OK
4 5
6 7
1.2.4 实例:参数传入列表数据
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
# 传入列表
@paramunittest.parametrized(
[4, 5],
[6, 7]
)
class TestCase(paramunittest.ParametrizedTestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case(self):
print(self.n1, self.n2)
self.assertGreater(self.n2, self.n1)
if __name__ == '__main__':
unittest.main()
Ran 2 tests in 0.002s
OK
4 5
6 7
1.2.5 实例:参数传入字典数据
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
# 传入字典
@paramunittest.parametrized(
"n1": 4, "n2": 5,
"n1": 6, "n2": 7
)
class TestCase(paramunittest.ParametrizedTestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case(self):
print(self.n1, self.n2)
self.assertGreater(self.n2, self.n1)
if __name__ == '__main__':
unittest.main()
Ran 2 tests in 0.002s
OK
4 5
6 7
1.2.6 实例:参数传入对象
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
test_data = ["n1": 4, "n2": 5, "n1": 6, "n2": 7]
@paramunittest.parametrized(
*test_data
)
class TestCase(paramunittest.ParametrizedTestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case(self):
print(self.n1, self.n2)
self.assertGreater(self.n2, self.n1)
if __name__ == '__main__':
unittest.main()
Ran 2 tests in 0.002s
OK
4 5
6 7
1.2.7 实例:参数传入函数
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
# 传入函数
def test_data():
return ["n1": 4, "n2": 5, "n1": 6, "n2": 7]
@paramunittest.parametrized(
*test_data()
)
class TestCase(paramunittest.ParametrizedTestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case(self):
print(self.n1, self.n2)
self.assertGreater(self.n2, self.n1)
if __name__ == '__main__':
unittest.main()
Ran 2 tests in 0.002s
OK
4 5
6 7
1.2.8 实例:通过继承unittest.TestCase类执行案例
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/21
# 文件名称:test_unittest_paramunittest.py
# 作用:unittest参数化paramunittest
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
import paramunittest
# 传入函数
def test_data():
return ["n1": 4, "n2": 5, "n1": 6, "n2": 7]
@paramunittest.parametrized(
*test_data()
)
# class TestCase(paramunittest.ParametrizedTestCase):
# def setParameters(self, n1, n2):
# self.n1 = n1
# self.n2 = n2
class TestCase(unittest.TestCase):
def setParameters(self, n1, n2):
self.n1 = n1
self.n2 = n2
def test_case以上是关于pytest学习和使用12-Unittest和Pytest参数化详解的主要内容,如果未能解决你的问题,请参考以下文章
pytest学习和使用9-fixture中conftest.py如何使用?
pytest学习和使用18-pytest.ini配置文件如何使用?