【阅读目录】

  • 1 用例执行状态

  • 2 xfail示例

  • 3 failed示例

  • 4 error示例

1 用例执行状态
状态 说明
passed 测试通过
failed 断言失败
error 用例本身代码报错
xfail 预期失败,加了 @pytest.mark.xfail()
2 xfail示例

  1. import pytest

  2. # 断言装饰器

  3. @pytest.mark.xfail(raises=ZeroDivisionError)

  4. def test_f():

  5. 1 / 0

  6. if __name__ == '__main__':

  7. pytest.main(["-s", "test_case_status.py"])


  1. test_case_status.py::test_f XFAIL [100%]

  2. @pytest.mark.xfail(raises=ZeroDivisionError)

  3. def test_f():

  4. > 1 / 0

  5. E ZeroDivisionError: division by zero

  6. test_case_status.py:14: ZeroDivisionError

  7. ============================= 1 xfailed in0.07s ==============================

3 failed示例

  1. import pytest

  2. # failed

  3. @pytest.fixture()

  4. def sum():

  5. add = 3 + 5

  6. assert add == 8

  7. return add

  8. def test_case(sum):

  9. assert sum == 9

  10. if __name__ == '__main__':

  11. pytest.main(["-s", "test_case_status.py"])


  1. sum = 8

  2. def test_case(sum):

  3. > assert sum == 9

  4. E assert8 == 9

  5. test_case_status.py:24: AssertionError

  6. =========================== short test summary info ===========================

  7. FAILED test_case_status.py::test_case - assert8 == 9

  8. ============================== 1 failed in0.07s ==============================

4 error示例
  • 正常情况:


  1. import pytest

  2. @pytest.fixture()

  3. def userinfo():

  4. name = "zhang"

  5. assert name == "zhang"

  6. return name

  7. def test_case(userinfo):

  8. assert userinfo == "zhang"

  9. if __name__ == '__main__':

  10. pytest.main(["-s", "test_case_status.py"])


  1. test_case_status.py::test_case PASSED [100%]

  2. ============================== 1 passed in0.02s ==============================

  • 我们把@pytest.fixture()去掉,就会error


  1. import pytest

  2. def userinfo():

  3. name = "zhang"

  4. assert name == "zhang"

  5. return name

  6. def test_case(userinfo):

  7. assert userinfo == "zhang"

  8. if __name__ == '__main__':

  9. pytest.main(["-s", "test_case_status.py"])


  1. =================================== ERRORS ====================================

  2. _________________________ ERROR at setup of test_case _________________________

  3. file F:\pytest_study\test_case\test_g\test_case_status.py, line 31

  4. def test_case(userinfo):

  5. E fixture 'userinfo'not found

  6. > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, metadata, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, worker_id

  7. > use 'pytest --fixtures [testpath]'for help on them.

  8. F:\pytest_study\test_case\test_g\test_case_status.py:31

  9. =========================== short test summary info ===========================

  10. ERROR test_case_status.py::test_case

  11. ============================== 1 error in0.03s ===============================

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐