pytest运行指定的测试参数
文章目录
一.mark
pytest提供了标记机制,允许你使用marker对测试函数做标记,一个测试函数可以有多个marker,一个marker也可以用来标记多个测试函数
1.背景:
当我们需要进行冒烟测试,不可能把所有的用例都跑一遍,我们可以挑选一些重要的用例进行冒烟测试,为了把选定的测试加入冒烟测试,可以对他们添加@pytest.mark.smoke装饰器。
案例1:只执行smoke类型的测试用例
test_menus1.py
# encoding=utf-8import pytestimport loggingimport randomimport time@pytest.mark.smokeclass TestMenus: @pytest.mark.smoke def test_menu1(self): logging.info('执行测试用例1') assert 2 == 2 #@pytest.mark.repeat(2) @pytest.mark.smoke def test_menu2(self): logging.info('执行测试用例2') assert 1 == 1 def test_menu3(self): logging.info('执行测试用例3') assert 2 == 2if __name__ == '__main__': pytest.main()
test_work1.py
#encoding=utf-8import pytestclass TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
执行命令行:
pytest
我们可以看到,
测试模块menus—>test_menus1.py文件—>类上加了装饰器,也就是说需要执行3条用例;
测试模块menus—>test_menus1.py文件—>test_h加了装饰器,说明执行1条用例
总需要执行4条用例
-m参数执行一下,这样我们标记的smoke的用例就被执行了,其他的没有被执行
-m后面也可以使用表达式,可以在标记之间添加and,or,not关键字
执行命令:表示执行没有标记smoke的测试用例
pytest -m "not smoke"
特别注意
pytest -m “not smoke”:必须使用双引号
二.跳过测试skip
要跳过某个测试,只需要简单的在测试函数上方添加@pytest.mark.skip()装饰器即可
案例1:pytest.mark.skip():跳过某些用例
test_menus.py
# encoding=utf-8import pytestimport loggingimport randomimport time@pytest.mark.smokeclass TestMenus: @pytest.mark.skip() @pytest.mark.smoke def test_menu1(self): logging.info('执行测试用例1') assert 2 == 2 #@pytest.mark.repeat(2) @pytest.mark.smoke def test_menu2(self): logging.info('执行测试用例2') assert 1 == 1 def test_menu3(self): logging.info('执行测试用例3') assert 2 == 2if __name__ == '__main__': pytest.main()
test_work1.py
#encoding=utf-8import pytestclass TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) @pytest.mark.skip() def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
执行测试命令
pytest
案例2:如果在整个类中添加@pytest.mark.skip():表示整个类中的测试用例都将跳过
#encoding=utf-8import pytest@pytest.mark.skip()class TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) @pytest.mark.skip() def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
命令行执行:
pytest
三.跳过测试skipif
我们可以给要跳过的测试添加理由和条件,这时应当使用skipif来替代skip
前面的是个条件,可以是python的表达式,表达式的值为True时跳过,为false时执行。后面的是原因,在来执行一次
test_menus1.py
# encoding=utf-8import pytestimport loggingimport randomimport time@pytest.mark.smokeclass TestMenus: @pytest.mark.skipif(24<8,reason='版本不匹配') @pytest.mark.smoke def test_menu1(self): logging.info('执行测试用例1') assert 2 == 2 #@pytest.mark.repeat(2) @pytest.mark.smoke def test_menu2(self): logging.info('执行测试用例2') assert 1 == 1 def test_menu3(self): logging.info('执行测试用例3') assert 2 == 2if __name__ == '__main__': pytest.main()
test_work1.py
#encoding=utf-8import pytest#@pytest.mark.skip()class TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) @pytest.mark.skipif(8<24,reason='版本不匹配') def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
执行命令
-rs:控制台查看跳过信息
pytest -rs
四.运行测试子集
运行测试有很多方式,不但可以选择运行某个测试目录、文件、类中的测试,还可以选择运行某一个测试用例
目录结构
1.运行单个目录
test_menus1.py
# encoding=utf-8import pytestimport loggingimport randomimport time@pytest.mark.smokeclass TestMenus: @pytest.mark.skipif(24<8,reason='版本不匹配') @pytest.mark.smoke def test_menu1(self): logging.info('执行测试用例1') assert 2 == 2 #@pytest.mark.repeat(2) @pytest.mark.smoke def test_menu2(self): logging.info('执行测试用例2') assert 1 == 1 def test_menu3(self): logging.info('执行测试用例3') assert 2 == 2if __name__ == '__main__': pytest.main()
test_work1.py
#encoding=utf-8import pytest#@pytest.mark.skip()class TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) @pytest.mark.skipif(8<24,reason='版本不匹配') def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
案例1:运行menus目录下的文件
执行命令
D:\project_development\api_pytest>pytest testcases/menus
collecting ... ----这是前置方法---- testcases\menus\test_menus1.py::TestMenus.test_menu1 ✓ 33% ███▍ testcases\menus\test_menus1.py::TestMenus.test_menu2 ✓ 67% ██████▋----这是后置方法---- testcases\menus\test_menus1.py::TestMenus.test_menu3 ✓ 100% ██████████Results (0.10s): 3 passed
2.运行单个测试文件
运行单个文件里的全部测试,以路径名加文件名作为pytest参数即可
案例2:运行test_menus1.py文件中的所有用例
执行命令
D:\project_development\api_pytest>pytest testcases/menus/test_menus1.py
3.运行单个测试函数
运行单个测试函数,只需要在文件名后添加::符号和函数名
案例3:运行test_menus1.py文件中的test_menu1这条用例
执行命令
D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus::test_menu1
特别注意:运行单个测试函数时,下面的方法不可取,会报错
ERROR: not found: D:\project_development\api_pytest\testcases\menus\test_menus1.py::test_menu1(no name 'D:\\project_development\\api_pytest\\testcases\\menus\\test_menus1.py::test_menu1' in any of [<Module test_menus1.py>])
4.运行单个测试类
与运行单个函数类似,只需要在文件名后面加上::符号和类名
案例4:执行test_menus1.py下的TestMenus类
执行命令
D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus
5.用测试名划分测试集合
-k选项允许用一个表达式指定需要运行的测试,该表达式可以匹配测试名(或者子串)。表达式中可以包含and,or,not
案例:执行用例中包含’2’的测试用例
test_menus1.py
# encoding=utf-8import pytestimport loggingimport randomimport time@pytest.mark.smokeclass TestMenus: @pytest.mark.skipif(24<8,reason='版本不匹配') @pytest.mark.smoke def test_menu1(self): logging.info('执行测试用例1') assert 2 == 2 #@pytest.mark.repeat(2) @pytest.mark.smoke def test_menu2(self): logging.info('执行测试用例2') assert 1 == 1 def test_menu3(self): logging.info('执行测试用例3') assert 2 == 2if __name__ == '__main__': pytest.main()
test_work1.py
#encoding=utf-8import pytest#@pytest.mark.skip()class TestWork: @pytest.mark.smoke def test_h(self,home_url): print("用例:%s" % home_url) @pytest.mark.skipif(8<24,reason='版本不匹配') def test_work1(self): assert 1 == 1 def test_work2(self): assert 1 == 1if __name__ == '__main__': pytest.main()
执行命令
D:\project_development\api_pytest>pytest -k 2
来源地址:https://blog.csdn.net/YZL40514131/article/details/127601200
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341