【阅读目录】
-
1 用例执行状态
-
2 xfail示例
-
3 failed示例
-
4 error示例
1 用例执行状态
状态 | 说明 |
---|---|
passed | 测试通过 |
failed | 断言失败 |
error | 用例本身代码报错 |
xfail | 预期失败,加了 @pytest.mark.xfail() |
2 xfail示例
-
import pytest
-
# 断言装饰器
-
@pytest.mark.xfail(raises=ZeroDivisionError)
-
def test_f():
-
1 / 0
-
if __name__ == '__main__':
-
pytest.main(["-s", "test_case_status.py"])
-
test_case_status.py::test_f XFAIL [100%]
-
@pytest.mark.xfail(raises=ZeroDivisionError)
-
def test_f():
-
> 1 / 0
-
E ZeroDivisionError: division by zero
-
test_case_status.py:14: ZeroDivisionError
-
============================= 1 xfailed in0.07s ==============================
3 failed示例
-
import pytest
-
# failed
-
@pytest.fixture()
-
def sum():
-
add = 3 + 5
-
assert add == 8
-
return add
-
def test_case(sum):
-
assert sum == 9
-
if __name__ == '__main__':
-
pytest.main(["-s", "test_case_status.py"])
-
sum = 8
-
def test_case(sum):
-
> assert sum == 9
-
E assert8 == 9
-
test_case_status.py:24: AssertionError
-
=========================== short test summary info ===========================
-
FAILED test_case_status.py::test_case - assert8 == 9
-
============================== 1 failed in0.07s ==============================
4 error示例
-
正常情况:
-
import pytest
-
@pytest.fixture()
-
def userinfo():
-
name = "zhang"
-
assert name == "zhang"
-
return name
-
def test_case(userinfo):
-
assert userinfo == "zhang"
-
if __name__ == '__main__':
-
pytest.main(["-s", "test_case_status.py"])
-
test_case_status.py::test_case PASSED [100%]
-
============================== 1 passed in0.02s ==============================
-
我们把
@pytest.fixture()
去掉,就会error
:
-
import pytest
-
def userinfo():
-
name = "zhang"
-
assert name == "zhang"
-
return name
-
def test_case(userinfo):
-
assert userinfo == "zhang"
-
if __name__ == '__main__':
-
pytest.main(["-s", "test_case_status.py"])
-
=================================== ERRORS ====================================
-
_________________________ ERROR at setup of test_case _________________________
-
file F:\pytest_study\test_case\test_g\test_case_status.py, line 31
-
def test_case(userinfo):
-
E fixture 'userinfo'not found
-
> 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
-
> use 'pytest --fixtures [testpath]'for help on them.
-
F:\pytest_study\test_case\test_g\test_case_status.py:31
-
=========================== short test summary info ===========================
-
ERROR test_case_status.py::test_case
-
============================== 1 error in0.03s ===============================
行动吧,在路上总比一直观望的要好,未来的你肯定会感 谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入扣群: 320231853,里面有各种软件测试+开发资料和技术可以一起交流学习哦。
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!