pytest自动化测试框架从0到1实战

张开发
2026/4/20 15:48:00 15 分钟阅读

分享文章

pytest自动化测试框架从0到1实战
点击文末小卡片免费获取软件测试全套资料资料在手涨薪更快1、运行方式命令行模式pytest -s login.py主函数模式if __name__ __main__: pytest.main([-s, login.py])pytest.ini运行在 pytest.ini 文件中配置 pytest 的运行参数。注意点位置配置文件一般会放在项目的根目录下编码必须是ANSI可以使用文本编辑工具修改编码格式规则无论是哪种运行方式都会读取配置文件是最高级的文件开头第一行除了注释一定为 [pytest] 这是固定格式文件名 pytest.ini 也是固定的不能自己重命名常用参数addopts 命令行的参数用空格分隔testpaths 测试用例的路径markers 标记参数赋值方式为 key:valuepython_files 模块的命名规则 xx.pypython_classes 类名的命名规则 Xxxpython_functions 方法的命名规则 **required_plugins 插件的使用xfail_strict true 禁用xpass2、断言pytest 单元测试框架并没有提供专门的断言方法而是直接使用Python 的 assert 进行断言。import pytest # 功能用于计算 a 与 b 相加的和 def add(a, b): return a b # 功能用于判断素数 def is_prime(n): if n 1: return False for i in range(2, n): if n % i 0: return False return True # 测试相等 def test_add_1(): assert add(3, 4) 7 # 测试不相等 def test_add_2(): assert add(17, 22) ! 50 # 测试大于或等于 def test_add_3(): assert add(17, 22) 50 # 测试小于或等于 def test_add_4(): assert add(17, 22) 38 # 测试包含 def test_in(): a hello b he assert b in a # 测试不包含 def test_not_in(): a hello b hi assert b not in a # 判断是否为 True def test_true_1(): assert is_prime(13) # 判断是否为 True def test_true_2(): assert is_prime(7) is True # 判断是否不为 True def test_true_3(): assert not is_prime(4) # 判断是否不为 True def test_true_4(): assert is_prime(6) is not True # 判断是否为 False def test_false_1(): assert is_prime(8) is False if __name__ __main__: pytest.main([-s, 0701.py])3、FixtureFixture 通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境。setup_module/teardown_module在当前文件中在所有测试用例执行之前与之后执行。setup_function/teardown_function在每个测试函数之前与之后执行。setup/teardown在每个测试函数之前与之后执行。这两个方法同样可以作用于类方法。4、参数化# argnames参数名 # argvalues参数对应值类型必须为可迭代类型一般使用list pytest.mark.parametrize(argnames, argvalues, indirectFalse,idsNone, scopeNone)示例import pytest class TestLogin: pytest.mark.parametrize((username, password), [(zhangsan, zhangsan123), ( xiaoming, xiaoming123)]) def test_a(self, username, password): print(username) print(password) assert 15、运行测试pytest 提供了丰富的参数运行测试用例通过“pytest --help”可以查看帮助。运行名称中包含某字符串的测试用例例如通过“-k”来指定在名称中包含“add”的测试用例pytest -k add test.pyif __name__ __main__: pytest.main([-k, add, test.py])减少测试的运行冗长运行日志少了很多信息“-q”用来减少测试运行的冗长也可以使用“–quiet”代替。pytest -q test.pyif __name__ __main__: pytest.main([-q, test.py])如果出现一条测试用例失败则退出测试这在测试用例的调试阶段是有用的当出现一条失败的测试用例时应该先通过调试让这条测试用例运行通过而不是继续执行后面的测试用例。pytest -x test.pyif __name__ __main__: pytest.main([-x, test.py])运行测试目录测试目录既可以指定相对路径如 ./test_dir 也可以指定绝对路径如D:\pytest_sample\test_dir。pytest ./test_dir指定特定类或方法执行这里指定运行 test_fixtures_02.py 文件中 TestMultiply 类下的 test_numbers_5_6()方法文件名、类名和方法名之间用“::”符号分隔。pytest test_fixtures_02.py::TestMultiply::test_numbers_5_66、跳过测试使用方式在需要跳过的测试脚本之上加上装饰器 pytest.mark.skipif(condition, reason“xxx”)# condition跳过的条件必传参数 # reason标注原因必传参数 pytest.mark.skipif(condition, reasonNone)7、生成测试报告生成 JUnit XML 文件pytest ./test_dir --junit-xml./report/log.xml生成在线测试报告pytest ./test_dir --pastebinall上述代码可生成一个 session-log 链接复制链接通过浏览器打开会得到一张 HTML格式的测试报告。8、pytest 插件pytest-htmlpytest-html 可以生成 HTML 格式的测试报告。首先通过 pip 命令安装 pytest-html 扩展。pip install pytest-html其次运行测试用例并生成测试报告.pytest ./ --html./report/result.htmlpytest-rerunfailurespytest-rerunfailures 可以在测试用例失败时进行重试。pip install pytest-rerunfailures通过“–reruns”参数设置测试用例运行失败后的重试次数。pytest -v test.py --reruns 3pytest-parallelpytest-parallel 扩展可以实现测试用例的并行运行。pip install pytest-parallel参数“–tests-per-worker”用来指定线程数“auto”表示自动分配。pytest -q test.py --tests-per-worker auto示例from time import sleep def test_01(): sleep(3) def test_02(): sleep(5) def test_03(): sleep(6)不使用线程运行测试用例花费14.05s使用后被缩短到 6.02s。pytest-ordering控制函数执行顺序。pip3 install pytest-ordering使用标记于被测试函数pytest.mark.run(orderx)根据order传入的参数来解决运行顺序order值全为正数或全为负数时运行顺序值越小优先级越高正数和负数同时存在正数优先级高最后下方这份完整的软件测试 视频教程已经整理上传完成需要的朋友们可以自行领取【保证100%免费】​​​软件测试面试文档我们学习必然是为了找到高薪的工作下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料并且有字节大佬给出了权威的解答刷完这一套面试资料相信大家都能找到满意的工作。

更多文章