pytest从入门到精通-1


pytest从入门到精通-1 pytest从入门到精通-1 一、pytest单元测试框架 二、单元测试框架和自动化测试框架有什么关系 三、pytest简介 1.什么是单元测试框架 单元测试是指在软件开发当中,针对软件的最小单位(函数,方法)进行正确性的检查 测试 2.单元测框架的分类 3.单元测试框架主要做什么 java junit testng python unittest pytest 1.测试发现 2.测试执行 3.测试判断 4.测试报告 从多个文件里面去找测试用例 按照一定的顺序和规则去执行,并生成结果 通过断言判断预期结果和实际结果的差异 统计测试进度,耗时,通过率等,生成测试报告 1.什么是自动化测试框架 2.自动化测试框架的作用 自动化测试框架是由测试组长或者自动化测试主管搭建的一套用于自动化测试的系统框 架 1.提高测试效率,降低维护成本 2.减少人工干预,提高测试的准确性,增加代码的重用性 3.核心思想是让不懂代码的人也能够通过这个框架去实现自动化测试 3.pytest单元测试框架和自动化测试框架的关系 单元测试框架是自动化测试框架中的组成部分之一 pom设置模式是自动化测试框架中组成部分之一 数据驱动是自动化测试框架中组成部分之一 关键字驱动是自动化测试框架中组成部分之一 全局配置文件是自动化测试框架中组成部分之一 日志监控是自动化测试框架中组成部分之一 pytest全局配置文件为pytest.ini, 需要放置在项目的根目录下 selenium,requests二次封装是自动化测试框架中组成部分之一 断言是自动化测试框架中组成部分之一 报告邮件是自动化测试框架中组成部分之一 其他更多的内容 1.pytest是一个非常成熟的python的单元测试框架,比unittest更灵活,更容易上手 2.pytest可以和selenium,requests,appium结合实现web自动化,接口自动化,app自动化 3.pytest可以实现测试用例的跳过以及reruns失败用例重试 4.pytest可以和allure生成非常美观的测试报告 5.pytest可以和Jenkins持续集成 6.pytest有很多非常强大的插件,并且这些插件能够实现很多的实用的操作 插件 pytest pytest-xdist pytest-ordering pytest-rerunfailures pytest-html 用于测试用例分布式执行,多CPU分发 用于改变测试用例的执行顺序 用例失败后重跑 生成html格式的自动化测试报告 allure-pytest 用于生成美观的测试报告 插件安装 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt requirements.txt pytest pytest-xdist pytest-ordering pytest-rerunfailures pytest-html allure-pytest 查看检验安装后的版本 pytest --version 四、使用pytest,默认的测试用例的规则以及基础应用 1.模块名必须以test_开头或者_test结尾 test_baidu.py google_test.py 2.测试类必须以Test开头,并且不能有init方法 class TestBaiDu(): 3.测试方法必须以test开头 def test001(self): print("测试百度搜索") def test002(): print("测试百度登录") class TestBaiDu(): def test001(self): print("测试百度搜索") 五、pytest测试用例的运行方式 1.主函数模式 2.命令模式 参数详解 1.运行所有 pytest.main() import pytest def test002(): print("测试百度登录") class TestBaiDu(): def test001(self): print("测试百度搜索") if __name__ == '__main__': pytest.main() 模块内运行 模块外运行 test_baidu.py import pytest def test002(): print("测试百度登录") class TestBaiDu(): def test001(self): print("测试百度搜索") runPytest.py import pytest pytest.main() test_baidu.py 2.指定模块 3.指定目录 4.通过nodeid指定用例运行 1.运行所有 pytest (mypytest) D:\PycharmProjects\mypytest>pytest pytest.main(['-vs','test_baidu.py']) runPytest.py import pytest pytest.main(['-vs','test_baidu.py']) 2.指定模块 pytest -vs test_baidu.py (mypytest) D:\PycharmProjects\mypytest>pytest -vs test_baidu.py pytest.main(['-vs','./testcase']) runPytest.py import pytest pytest.main(['-vs','./testcase']) 3.指定目录 pytest -vs ./testcase (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase nodeid由模块名,分隔符,类名,方法名,函数名组成 运行目录下模块中的函数 pytest.main(['-vs','./testcase/souhu_test.py::testsouhulogin']) runPytest.py import pytest pytest.main(['-vs','./testcase/souhu_test.py::testsouhulogin']) 其中模块名字和函数之间的分隔符用英文双冒号(::) 运行目录下模块中类的函数 pytest.main(['-vs','./testcase/souhu_test.py::TestSouHu::testsearch']) runPytest.py import pytest pytest.main(['-vs','./testcase/souhu_test.py::TestSouHu::testsearch']) 其中模块名字和类名之间的分隔符用英文双冒号(::) 类名与函数名之间的分隔符也用英文双冒号(::) 4.通过nodeid指定用例运行 pytest -vs ./testcase/souhu_test.py::TestSouHu::testsearch (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase/souhu_test.py::TestSouHu::testsearch 3.通过读取pytest.ini全局配置文件运行 -s 表示输出调试信息,包括print打印的信息 -v 表示更详细的信息,会打印出用例名字和执行结果 -vs 这两个参数一起用 -n 支持多线程或者分布式运行测试用例 主函数模式运行 命令模式运行 runPytest.py import pytest pytest.main(['-vs','./testcase','-n 2']) (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase -n 2 --reruns NUM 失败用例重跑 命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase -n 2 --reruns 2 -x 表示只要有一个用例报错,那么测试停止 主函数模式运行 命令模式运行 runPytest.py import pytest pytest.main(['-vsx','./testcase']) (mypytest) D:\PycharmProjects\mypytest>pytest -vsx ./testcase --maxfail=2 出现两个用例失败就停止 命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase --maxfail=2 -k 根据测试用例的部分字符串指定测试用例 命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase -k "ch" --html ./report/report.html 生成html的测试报告 命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest -vs ./testcase -m "smoke or usermanage or productnamage" pytest.ini 这个文件是pytest单元测试框架的核心配置文件 1.位置 2.编码 3.作用 4.运行的规则;不管是主函数的模式运行,命令行模式运行,都会去读这个配置文件。 一般放在项目的根目录 必须是ANSI,可以使用notpad++修改编码格式 改变pytest默认的行为 pytest.ini内容 [pytest] addopts=-vs #命令行的参数,用空格分隔 testpaths=./testcase #测试用例的路径 python_files=test_*.py #模块名的规则 python_classes=Test* #类名的规则 python_functions=test #方法名的规则 使用命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest 注释实际配置中要将中文去掉,文件编码模式修改为ANIS编码(用nodepad++修改) [pytest] addopts=-vs --html ./report/report.html testpaths=./testcase python_files=*_test.py python_classes=Test* python_functions=test 六、pytest执行测试用例的顺序是怎样的呢 unittest 是根据测试用例名称的ascII值的大小来绝对的执行的顺序 pytest 默认从上到下顺序执行 改变默认的执行顺序 使用mark标记 @pytest.mark.run(order=1) import pytest def testsouhulogin(): print("登录搜狐") class TestSouHu(): @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) def testswipe(self): print("搜狐滑动") assert False 七、如何分组执行(冒烟,分模块执行,分接口和web执行) smoke 冒烟用例,分布在各个模块里面 -m "smoke or usermanage or productnamage" 分组执行 (mypytest) D:\PycharmProjects\mypytest>pytest -m "smoke" 代码中的标记 import pytest def testsouhulogin(): print("登录搜狐") class TestSouHu(): @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) def testswipe(self): print("搜狐滑动") assert False 命令模式运行 (mypytest) D:\PycharmProjects\mypytest>pytest -m "smoke" 代码中标记的“smoke”与命令模式中-m后面的“smoke”一定要一致 八、pytest跳过测试用例 1.无条件跳过 2.有条件跳过 @pytest.mark.skip(reason="太难了") import pytest def testsouhulogin(): print("登录搜狐") class TestSouHu(): @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) def testswipe(self): print("搜狐滑动") assert False @pytest.mark.skipif(age>=18,reason='已成年') 第一个参数是条件:age>=18 第二个参数是描述原因:reason='已成年' import pytest def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False allure 安装allure 1.下载allure 网址::https://github.com/allure-framework/allure2/releases/tag/2.17.3 下载其中的如allure-2.17.3.zip,解压放到本地,并配置环境变量到path,如D:\Program Files\allure-2.17.3\bin(配置到bin目录) 2.验证allure是否配置成功 cmd中或者pycharm的命令行中输入 allure --version 出现版本信息即可 3.生成allure报告 1.先生成临时的json格式的报告 2.再根据生成的json格式的报告生成html的报告 在pytest.ini配置文件中添加 --alluredir ./temps --clean-alluredir [pytest] addopts=-vs --html ./report/report.html --alluredir ./temps --clean-alluredir testpaths=./testcase python_files=*_test.py python_classes=Test* python_functions=test 表示生成临时的json格式的报告到项目根目录下的temps文件夹下; --clean-alluredir,表示先清除temps文件夹下之前的内容,保证是最新的json内容; os.system("allure generate ./temps -o ./reports --clean") 主函数模式运行 runPytest.py import pytest import os import time pytest.main() time.sleep(2) #生成json格式报告后等2秒 os.system("allure generate ./temps -o ./reports --clean") 表示生成的json格式放在temps目录下,生成的报告放在reports目录下; --clean,表示生成报告前先清除原有内容 参数都可以放到pytest.ini中的addopts 的值中 [pytest] addopts=-vs -m "smoke" #命令行的参数,用空格分隔 testpaths=./testcase #测试用例的路径 python_files=test_*.py #模块名的规则 python_classes=Test* #类名的规则 python_functions=test #方法名的规则 [pytest] addopts=-vs -m "smoke" --maxfail=2 --html ./report/report.html #命令行的参数,用空格分隔 Created With EdrawMind