pytest入门二:用例的执行顺序

打印 上一主题 下一主题

主题 1033|帖子 1033|积分 3099

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import sys
  2. import pytest
  3. class TestDemo:
  4.     def test_demo1(self):
  5.         x = "hello world"
  6.         print(f"{x} python")
  7.         assert 'h' in x
  8.     def test_demo3(self):
  9.         x = "hello world"
  10.         print(f"{x} python")
  11.         assert 'h' in x
  12.     def test_demo2(self):
  13.         x = 'hello'
  14.         assert hasattr(x,"check")
  15. if __name__ == "__main__":
  16.     pytest.main(['-v', '-s','test_2.py'])
复制代码
看着默认执行顺序是代码中函数的顺序

更改用例的执行顺序,可以安装包 pip install pytest-ordering
按序执行@pytest.mark.run(order=x)
  1. import sys
  2. import pytest
  3. class TestDemo:
  4.     @pytest.mark.run(order=1)
  5.     def test_demo1(self):
  6.         x = "hello world"
  7.         print(f"{x} python")
  8.         assert 'h' in x
  9.     @pytest.mark.run(order=3)
  10.     def test_demo3(self):
  11.         x = "hello world"
  12.         print(f"{x} python")
  13.         assert 'h' in x
  14.     @pytest.mark.run(order=2)
  15.     def test_demo2(self):
  16.         x = 'hello'
  17.         assert hasattr(x,"check")
  18. if __name__ == "__main__":
  19.     pytest.main(['-v', '-s','test_2.py'])
复制代码

依赖执行
pip install pytest-dependency
用例默认执行顺序是test1-->test2-->test3,test2依赖test1,test3依赖test2。
test1通过才会执行test2,test2通过才会执行test3,假如test1失败,则后续依赖的用例跳过,
假如依赖的用例还未执行,也会跳过
  1. import sys
  2. import pytest
  3. class TestDemo:
  4.     @pytest.mark.dependency(name="test1")
  5.     def test_demo1(self):
  6.         x = "hello world"
  7.         print(f"{x} python")
  8.         assert 'h' in x
  9.     @pytest.mark.dependency(name="test3",depends=['test2'])
  10.     def test_demo3(self):
  11.         x = "hello world"
  12.         print(f"{x} python")
  13.         assert 'h' in x
  14.     @pytest.mark.dependency(name="test2",depends=['test1'])
  15.     def test_demo2(self):
  16.         x = 'hello'
  17.         assert hasattr(x,"check")
  18. if __name__ == "__main__":
  19.     pytest.main(['-v', '-s','test_2.py'])
复制代码
 test_demo2未执行,test_demo3跳过

 
  1. import sys
  2. import pytest
  3. class TestDemo:
  4.     @pytest.mark.dependency(name="test1")
  5.     @pytest.mark.run(order=1)
  6.     def test_demo1(self):
  7.         x = "hello world"
  8.         print(f"{x} python")
  9.         assert 'h' in x
  10.     @pytest.mark.dependency(name="test3",depends=['test2'])
  11.     @pytest.mark.run(order=3)
  12.     def test_demo3(self):
  13.         x = "hello world"
  14.         print(f"{x} python")
  15.         assert 'h' in x
  16.     @pytest.mark.dependency(name="test2",depends=['test1'])
  17.     @pytest.mark.run(order=2)
  18.     def test_demo2(self):
  19.         x = 'hello'
  20.         assert hasattr(x,"check")
  21. if __name__ == "__main__":
  22.     pytest.main(['-v', '-s','test_2.py'])
复制代码
 顺序对了,但是test_demo2失败,test_demo3跳过

 
  1. import sys
  2. import pytest
  3. class TestDemo:
  4.     @pytest.mark.dependency(name="test1")
  5.     @pytest.mark.run(order=1)
  6.     def test_demo1(self):
  7.         x = "hello world"
  8.         print(f"{x} python")
  9.         assert 'h' in x
  10.     @pytest.mark.dependency(name="test3",depends=['test2'])
  11.     @pytest.mark.run(order=3)
  12.     def test_demo3(self):
  13.         x = "hello world"
  14.         print(f"{x} python")
  15.         assert 'h' in x
  16.     @pytest.mark.dependency(name="test2",depends=['test1'])
  17.     @pytest.mark.run(order=2)
  18.     def test_demo2(self):
  19.         x = 'hello'
  20.         assert hasattr(x,"replace")
  21. if __name__ == "__main__":
  22.     pytest.main(['-v', '-s','test_2.py'])
复制代码
顺序对,依赖对 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表