在写接口或功能自动化case的时候,会遇到一些case有前后执行顺序的场景。好比:注册->登录,先调用注册接口在数据库里面生成用户名和密码,而后使用登录接口验证登录。html
在pytest里,若是注册和登录分别是两个testcase,默认执行执行顺序是从前到后的。python
有一个pytest的测试case用例:test_login.py数据库
#/usr/bin/env python3 import pytest def test_login(): assert True def test_reg(): assert True
运行:pytest test_login.pyide
能够看到test_login在test_reg以前执行,这和咱们的指望相悖。函数
解决方法:测试
1. 能够在编写testcase脚本时候,把test_reg放到test_login前面。编码
2.使用pytest-ordering插件url
第一种方式属于硬编码的方式,在后续维护过程当中很容易出现维护的混乱性(好比后续有人要新加case和注册登陆相关,可能改起来就比较麻烦了)。spa
第二种的好处就是能够经过pytest-ordering插件,使用装饰器的方式写清楚这种前后顺序,方便阅读和维护。.net
sudo pip3(pip) install pytest-ordering
1. 经过指定序数词指定,好比first,second,third...second-to-last(倒数第二), last(最后)。这种含义比较直接(不过容易写错单词)
#/usr/bin/env python3 import pytest @pytest.mark.run('second') def test_login(): assert True @pytest.mark.run('first') def test_reg(): assert True
2. 经过指定序号指定,好比1,2,3...-2(倒数第二), -1(最后)。这种写错的概率下,也比较直接。
#/usr/bin/env python3 import pytest @pytest.mark.run(order=2) def test_login(): assert True @pytest.mark.run(order=1) def test_reg(): assert True
3. 经过指定函数名指定,好比test_login和test_order都要在test_reg后执行,这种用法就比较适合此类场景。
#/usr/bin/env python3 import pytest @pytest.mark.run(after='test_reg') def test_login(): assert True def test_reg(): assert True @pytest.mark.run(after='test_reg') def test_order(): assert True
以上就是pytest-ordering插件的三种写法,若是有任何疑问和建议能够留言~
博主:测试生财
座右铭:专一测试与自动化,致力提升研发效能;经过测试精进完成原始积累,经过读书理财奔向财务自由。
csdn:https://blog.csdn.net/ccgshigao