标记失败用到的情况是,本身就知道这是失败的例子,所以,不用让他运行,直接跳过。或者是依赖于某个方法,某个方式失败的话,用例直接标记成失败。
标记失败有两种方法,一种是方法内部,一种是方法外部。内部用pytest.xfail(""),外部用装饰@pytest.mark.xfail(condition=None, *, reason=None, raises=None, run=True, strict=False)
具体看以下的两个例子:
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest parameter = [{"user":"admin","password":"123456"}] @pytest.mark.xfail(reason="no run") def test_mark(): pass @pytest.fixture(scope="module") def login(request): user = request.param["user"] password = request.param["password"] print("正在操作登录,账号:%s, 密码:%s" % (user, password)) if password: return True else: return False @pytest.mark.parametrize("login",parameter,indirect=True) class Test_xfail(): def test_01(self,login): """用例1登录""" result = login print("用例1:%s" % result) assert result == True def test_02(self, login): result = login print("用例2,登录结果:%s" % result) if not result: pytest.xfail("登录不成功, 标记为xfail") assert 1 == 1 def test_03(self, login): result = login print("用例3,登录结果:%s" % result) if not result: pytest.xfail("登录不成功, 标记为xfail") assert 1 == 1
运行结果:显示一个跳过,3个通过
============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 4 items python_work_apple/pytest_package/mark/test_xfail.py X正在操作登录,账号:admin, 密码:123456 .用例1:True .用例2,登录结果:True .用例3,登录结果:True [100%] ===================== 3 passed, 1 xpassed in 0.03 seconds ======================
下面的一个,让密码为空
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest parameter = [{"user":"admin","password":""}] @pytest.mark.xfail(reason="no run") def test_mark(): pass @pytest.fixture(scope="module") def login(request): user = request.param["user"] password = request.param["password"] print("正在操作登录,账号:%s, 密码:%s" % (user, password)) if password: return True else: return False @pytest.mark.parametrize("login",parameter,indirect=True) class Test_xfail(): def test_01(self,login): """用例1登录""" result = login print("用例1:%s" % result) assert result == True def test_02(self, login): result = login print("用例2,登录结果:%s" % result) if not result: pytest.xfail("登录不成功, 标记为xfail") assert 1 == 1 def test_03(self, login): result = login print("用例3,登录结果:%s" % result) if not result: pytest.xfail("登录不成功, 标记为xfail") assert 1 == 1
运行结果:显示一个失败,一个跳过(xpass),用例2和3没执行,直接标记为xfail了
============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 4 items python_work_apple/pytest_package/mark/test_xfail.py X正在操作登录,账号:admin, 密码: F用例1:False python_work_apple/pytest_package/mark/test_xfail.py:24 (Test_xfail.test_01[login0]) True != False Expected :False Actual :True <Click to see difference> self = <test_xfail.Test_xfail object at 0x10b5f9e80>, login = False def test_01(self,login): """用例1登录""" result = login print("用例1:%s" % result) > assert result == True E assert False == True python_work_apple/pytest_package/mark/test_xfail.py:29: AssertionError x用例2,登录结果:False self = <test_xfail.Test_xfail object at 0x10be03dd8>, login = False def test_02(self, login): result = login print("用例2,登录结果:%s" % result) if not result: > pytest.xfail("登录不成功, 标记为xfail") E _pytest.outcomes.XFailed: 登录不成功, 标记为xfail python_work_apple/pytest_package/mark/test_xfail.py:35: XFailed x用例3,登录结果:False self = <test_xfail.Test_xfail object at 0x10bdfbbe0>, login = False def test_03(self, login): result = login print("用例3,登录结果:%s" % result) if not result: > pytest.xfail("登录不成功, 标记为xfail") E _pytest.outcomes.XFailed: 登录不成功, 标记为xfail python_work_apple/pytest_package/mark/test_xfail.py:43: XFailed [100%] =================================== FAILURES =================================== __________________________ Test_xfail.test_01[login0] __________________________ self = <test_xfail.Test_xfail object at 0x10b5f9e80>, login = False def test_01(self,login): """用例1登录""" result = login print("用例1:%s" % result) > assert result == True E assert False == True python_work_apple/pytest_package/mark/test_xfail.py:29: AssertionError ---------------------------- Captured stdout setup ----------------------------- 正在操作登录,账号:admin, 密码: ----------------------------- Captured stdout call ----------------------------- 用例1:False ================ 1 failed, 2 xfailed, 1 xpassed in 0.11 seconds ================ Process finished with exit code 0