pytest学习和使用14-Pytest用例执行结果有哪几种状态?

举报
虫无涯 发表于 2023/02/08 08:58:49 2023/02/08
【摘要】 1 用例执行状态状态说明passed测试通过failed断言失败error用例本身代码报错xfail预期失败,加了 @pytest.mark.xfail() 2 xfail示例# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2022/12/27 # 文件名称:test_case_status.py# 作用:用例的执行状态# 联系:VX(NoamaNel...

1 用例执行状态

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

2 xfail示例

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_case_status.py
# 作用:用例的执行状态
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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 in 0.07s ==============================

3 failed示例

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_case_status.py
# 作用:用例的执行状态
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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       assert 8 == 9

test_case_status.py:24: AssertionError
=========================== short test summary info ===========================
FAILED test_case_status.py::test_case - assert 8 == 9
============================== 1 failed in 0.07s ==============================

4 error示例

  • 正常情况:
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_case_status.py
# 作用:用例的执行状态
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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 in 0.02s ==============================
  • 我们把@pytest.fixture()去掉,就会error
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_case_status.py
# 作用:用例的执行状态
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

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 in 0.03s ===============================
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。