pytest入门一:用例的执行范围

饭宝  论坛元老 | 2024-12-16 04:47:35 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1038|帖子 1038|积分 3114

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

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

x
从一个或多个目次开始查找,可以在命令行指定文件名或目次名。如果未指定,则利用当前目次。
  

  • 测试文件以 test_ 开头或以 _test 结尾
  • 测试类以 Test 开头 ,而且不能带有 init 方法
  • 测试函数以 test_ 开头
  • 断言利用根本的 assert 即可
  • 所有的包 pakage 必须要有__init__.py 文件
  
  1. # test_2.py
  2. import sys
  3. import pytest
  4. def add(x,y):
  5.     return x + y
  6. @pytest.mark.add
  7. def test_add():
  8.     assert add(1,2) == 3
  9.     assert add(2,3) == 5
  10.     assert add(10,2) == 12
  11. def is_win32():
  12.     return True if sys.platform == 'win32' else False
  13. class TestDemo:
  14.     def test_demo(self):
  15.         x = "hello world"
  16.         print(f"{x} python")
  17.         assert 'h' in x
  18.     @pytest.mark.skipif(sys.platform == 'win32',reason='win32跳过用例')
  19.     def test_demo3(self):
  20.         x = "hello world"
  21.         print(f"{x} python")
  22.         assert 'h' in x
  23.     only_win32 = pytest.mark.skipif(is_win32(),reason='win32跳过用例')
  24.     @only_win32
  25.     def test_demo4(self):
  26.         x = "hello world"
  27.         print(f"{x} python")
  28.         assert 'h' in x
  29.     @pytest.mark.skip("变更")
  30.     def test_demo2(self):
  31.         x = 'hello'
  32.         assert hasattr(x,"check")
  33.     @pytest.mark.xfail(reason="bug待修复")
  34.     def test_demo5(self):
  35.         x = 'hello'
  36.         assert hasattr(x, "check")
  37. if __name__ == "__main__":
  38.     pytest.main(['-v', '-s'])
复制代码
1)执行全部
if name == "__main__": pytest.main(['-v', '-s'])

2)执行某个文件 
if name == "__main__":
    pytest.main(['-v', '-s','test_2.py'])


 3)执行某个类
if name == "__main__":
    pytest.main(['-v', '-s','test_2.py::TestDemo'])


4) 执行某个方法
if name == "__main__":
    pytest.main(['-v', '-s','test_2.py::TestDemo::test_demo'])


5) mark 打标执行  pytest.mark.add

6) skip忽略执行  @pytest.mark.skip("变更")

7) 条件忽略skipif    @pytest.mark.skipif(sys.platform == 'win32',reason='win32跳过用例')

8)xfail:预期失败 @pytest.mark.xfail
真失败会显示xfailed,成功会显示xpassed,应用场景:已知bug标注,后续验证修复



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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

饭宝

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