马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
从一个或多个目次开始查找,可以在命令行指定文件名或目次名。如果未指定,则利用当前目次。
- 测试文件以 test_ 开头或以 _test 结尾
- 测试类以 Test 开头 ,而且不能带有 init 方法
- 测试函数以 test_ 开头
- 断言利用根本的 assert 即可
- 所有的包 pakage 必须要有__init__.py 文件
- # test_2.py
- import sys
- import pytest
- def add(x,y):
- return x + y
- @pytest.mark.add
- def test_add():
- assert add(1,2) == 3
- assert add(2,3) == 5
- assert add(10,2) == 12
- def is_win32():
- return True if sys.platform == 'win32' else False
- class TestDemo:
- def test_demo(self):
- x = "hello world"
- print(f"{x} python")
- assert 'h' in x
- @pytest.mark.skipif(sys.platform == 'win32',reason='win32跳过用例')
- def test_demo3(self):
- x = "hello world"
- print(f"{x} python")
- assert 'h' in x
- only_win32 = pytest.mark.skipif(is_win32(),reason='win32跳过用例')
- @only_win32
- def test_demo4(self):
- x = "hello world"
- print(f"{x} python")
- assert 'h' in x
- @pytest.mark.skip("变更")
- def test_demo2(self):
- x = 'hello'
- assert hasattr(x,"check")
- @pytest.mark.xfail(reason="bug待修复")
- def test_demo5(self):
- x = 'hello'
- assert hasattr(x, "check")
- if __name__ == "__main__":
- 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企服之家,中国第一个企服评测及商务社交产业平台。 |