- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有许多项目,我使用 pytest.mark.xfail 标记来标记失败但不应该失败的测试,以便失败的测试用例可以在问题解决之前添加。我不想跳过这些测试,因为如果我所做的某些事情导致它们开始通过,我希望得到通知,以便我可以删除xfail
标记避免回归。
问题是,由于 xfail
测试实际上会运行直到失败,因此导致失败的任何行都会被视为“已覆盖”,即使它们是未通过测试的一部分,这给了我一些误导性的指标,让我知道有多少代码实际上经过了测试,可以正常工作。一个最小的例子是:
pkg.py
def f(fail):
if fail:
print("This line should not be covered")
return "wrong answer"
return "right answer"
test_pkg.py
import pytest
from pkg import f
def test_success():
assert f(fail=False) == "right answer"
@pytest.mark.xfail
def test_failure():
assert f(fail=True) == "right answer"
运行python -m pytest --cov=pkg
,我得到:
platform linux -- Python 3.7.1, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: /tmp/cov, inifile:
plugins: cov-2.6.0
collected 2 items
tests/test_pkg.py .x [100%]
----------- coverage: platform linux, python 3.7.1-final-0 -----------
Name Stmts Miss Cover
----------------------------
pkg.py 5 0 100%
如您所见,所有五行都被覆盖,但第 3 行和第 4 行仅在 xfail
测试期间被命中。
我现在处理这个问题的方法是设置tox
来运行pytest -m "not xfail"--cov && pytest -m xfail
之类的东西,但是在除了有点麻烦之外,这只是过滤掉带有 xfail
标记的东西,这意味着 有条件的 xfails 也会被过滤掉,无论条件是否满足遇见了。
有没有办法让覆盖率
或pytest
不计算失败测试的覆盖率?或者,我可以使用一种忽略 xfail
测试覆盖率的机制,该机制仅在满足条件时忽略条件 xfail
测试。
最佳答案
由于您使用的是 pytest-cov
插件,请利用它的 no_cover
标记。当使用 pytest.mark.no_cover 注解时,测试时将关闭代码覆盖率。唯一需要实现的是将 no_cover
标记应用于所有标有 pytest.mark.xfail
的测试。在您的 conftest.py
中:
import pytest
def pytest_collection_modifyitems(items):
for item in items:
if item.get_closest_marker('xfail'):
item.add_marker(pytest.mark.no_cover)
运行您的示例现在将产生:
$ pytest --cov=pkg -v
=================================== test session starts ===================================
platform darwin -- Python 3.7.1, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow, inifile:
plugins: cov-2.6.0
collected 2 items
test_pkg.py::test_success PASSED [ 50%]
test_pkg.py::test_failure xfail [100%]
---------- coverage: platform darwin, python 3.7.1-final-0 -----------
Name Stmts Miss Cover
----------------------------
pkg.py 5 2 60%
=========================== 1 passed, 1 xfailed in 0.04 seconds ===========================
xfail
标记中的条件标记参数可以通过 marker.args
和 marker.kwargs
访问,因此,如果您有标记
@pytest.mark.xfail(sys.platform == 'win32', reason='This fails on Windows')
访问参数
marker = item.get_closest_marker('xfail')
condition = marker.args[0]
reason = marker.kwargs['reason']
为了考虑条件标志,上面的钩子(Hook)可以修改如下:
def pytest_collection_modifyitems(items):
for item in items:
marker = item.get_closest_marker('xfail')
if marker and (not marker.args or marker.args[0]):
item.add_marker(pytest.mark.no_cover)
关于python - 使覆盖率仅计算成功的测试并忽略 xfailing 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191930/
我有一个标记为 xfail 的 py.test 测试函数: @pytest.mark.xfail def test_that_fails(): assert 1 == 2 在我的 pytest
作为背景,我们的测试用例在同一产品的 20 多个部署中运行,在其中一些部署中,测试用例由于已知错误而失败,而在其他部署中,它们通过了测试。 任何人都可以帮忙提出一个建议,以便最好地“xfail”一个特
我有许多项目,我使用 pytest.mark.xfail 标记来标记失败但不应该失败的测试,以便失败的测试用例可以在问题解决之前添加。我不想跳过这些测试,因为如果我所做的某些事情导致它们开始通过,我希
我经常使用 py.test 生成测试,其中一些测试用例预计会失败。我怎样才能将它们标记为 xfail?如果我将 @py.test.mark.xfail 放在测试函数上,则意味着它的所有实例都是 xfa
有没有办法在机器人框架中将测试标记为 XFAIL?我想执行测试,如果它们有关联的缺陷标签,我想将它们标记为 XFAIL。 是否可以使用 ResultWriter 或任何其他模块来实现它。 最佳答案 我
我是 pytest 的新手,我想知道如何在使用某些参数调用时将测试标记为“预期失败”。我这样参数化测试: @pytest.mark.parametrize("param1", [False, True
我有一个使用requests 的测试并且需要一个有效的连接。 如果连接断开,我希望它xfail,否则正常通过。 这是我尝试过的: from requests import ConnectionErro
我有一些 python 代码和一堆 pytest 测试。 我预计会失败的一些测试(也就是说,我正在测试错误处理,所以我传入了应该导致我的代码抛出异常的输入。) 为了做到这一点,我有这样的代码: CF_
在 py.test(版本 4.6.2)中,您有一个标记装饰器用于将测试标记为失败,例如 @pytest.mark.xfail def test1(): return 1/0 也可以验证异常本身
我有一个重固定测试函数,它在某些固定输入时失败(它应该)。我怎样才能指出这一点?这就是我现在正在做的,也许还有更好的方法。我是 py.test 的新手,所以我很感激任何提示。 下一部分是所有输入灯具。
我想缩小 pytest xfail 标记的范围。就我目前使用的情况而言,它标志着整个测试功能,功能中的任何失败都很酷。 我想将其缩小到更小的范围,也许使用类似于“with pytest.raises
从 allure.qatools.ru 的首页来看,这些类别似乎已创建: 待处理 通过 已取消 损坏 失败 这如何映射到 pytest 类别? 我希望看到跳过、xpass、xfail和错误。 它们受支
我将使用装饰器将测试标记为 xfail,并为它们提供预计在应用程序的当前开发状态中发生的特定异常。 据我了解 http://pytest.org/latest/skipping.html 中的文档
当测试 xfailed 时,打印的原因报告有关测试文件、测试类和测试用例,而跳过的测试用例仅报告测试文件和调用 skip 的行。 这是一个测试例子: #!/usr/bin/env pytest imp
我是一名优秀的程序员,十分优秀!