pytest插件,使用标记将测试与GitHub问题相关联。
项目描述
构建状态 覆盖率状态 需求状态 版本 许可协议 支持的Python版本
pytest插件,通过标记与GitHub集成。集成允许测试根据链接的GitHub问题的状态xfail(或跳过)。
安装
使用pip安装插件
pip install pytest-github
使用方法
安装后,以下pytest命令行参数可用。
py.test \
[--github-cfg=GITHUB_CFG] \
[--github-username=GITHUB_USERNAME] \
[--github-token=GITHUB_TOKEN] \
[--github-completed=GITHUB_COMPLETED] \
[--github-summary]
接下来,创建一个名为
github.yml
的配置文件,包含您的GitHub用户名和个人API令牌。以下是一个示例文件。
github:
username: j.doe
token: XXXXXXXXXXXXX
标记
以下pytest标记可用
@pytest.mark.github(*args): GitHub issue integration
标记可以用来影响测试的结果。请参考以下示例以获取指导。
示例:xfail
通常,当测试失败时,人们可能会在GitHub上创建一个issue来跟踪问题的解决过程。或者,你也可以使用内置的xfail标记。这就是pytest-github能够发挥作用的地方。为了避免在每次测试运行时都要审核已知的失败,以及避免总是使用xfail,可以考虑使用github标记来根据GitHub issue的状态动态影响测试结果。
以下示例演示了如何使用github标记来影响一个已知失败测试的结果。
@pytest.mark.github('https://github.com/some/open/issues/1')
def test_will_xfail():
assert False
使用py.test运行此测试将产生以下输出
test.py::test_will_xfail xfail
示例:使用‘raises’关键字预期特定异常
为了避免在GitHub issue被解决期间掩盖测试可能发现的额外失败,你可以使用raises关键字参数将预期失败限制为特定的异常
@pytest.mark.github('https://github.com/some/open/issues/1', raises=ZeroDivisionError)
def test_will_xfail():
foo = 1/0
@pytest.mark.github('https://github.com/some/open/issues/1', raises=ValueError)
def test_will_fail():
# This test has been marked with an open issue but it will still fail
# because the exception raised is different from the one indicated by
# the 'raises' keyword.
foo = 1/0
使用py.test运行此测试将产生以下输出
collected 2 items
collected 1 github issues
test.py::test_will_xfail xfail
test.py::test_will_fail FAILED
示例:XPASS
以下示例演示了一个与开放的GitHub issue关联的测试成功的情况。
@pytest.mark.github('https://github.com/some/open/issues/1')
def test_will_xpass():
assert True
在这个示例中,使用了XPASS结果(也称为意外通过)。
test.py::test_will_xpass XPASS
示例:PASSED
以下示例演示了一个与关闭的GitHub issue关联的测试成功的情况。当一个测试与GitHub
@pytest.mark.github('https://github.com/some/closed/issues/2')
def test_will_pass():
assert True
在这个示例中,使用了PASSED结果。
test.py::test_will_pass PASSED
示例:FAILED
以下示例演示了一个与关闭的GitHub issue关联的测试失败的情况。
@pytest.mark.github('https://github.com/some/closed/issues/2')
def test_will_fail():
assert False
在这个示例中,使用了FAILED结果。
test.py::test_will_fail FAILED
示例:SKIPPED
以下示例演示了一个与开放的GitHub issue关联的测试失败的情况。
@pytest.mark.github('https://github.com/some/open/issues/1', skip=True)
def test_will_skip():
assert False
在这个示例中,使用了SKIPPED结果。
test.py::test_will_skip SKIPPED
在这个示例中,使用了SKIPPED结果。
test.py::test_will_skip SKIPPED
示例:IDS
以下示例演示了一个使用github标记来影响已知失败测试子集结果的参数化测试。
@pytest.mark.github('https://github.com/some/open/issues/1', ids=['even2', 'even4'])
@pytest.mark.parametrize("count", [1, 2, 3, 4], ids=["odd1", "even2", "odd3", "even4"])
def test_will_xfail(count):
assert count % 2
GitHub标记及其关联测试的摘要
--github-summary选项列出了所有由github标记引用的GitHub issue。列表分为两部分,Resolved Issues(已解决issue)和Unresolved Issues(未解决issue),如果一个issue具有GITHUB_COMPLETED标签之一,则认为该issue已解决。在每一个issue下面列出了所有引用该issue的测试。
示例输出
Unresolved Issues https://github.com/repo/open/issues/1 - test_suite.py:test_foo https://github.com/repo/open/issues/2 - test_suite.py:test_bar Resolved Issues https://github.com/repo/open/issues/3 - test_suite.py:test_baz - test_suite.py:test_bah