使用 docstrings 列出 py.test 中的测试 [英] Use docstrings to list tests in py.test

查看:47
本文介绍了使用 docstrings 列出 py.test 中的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的测试文件:

# test_single.pydef test_addition():二加二还是四"断言 2 + 2 == 4def test_addition2():一加一还是二"断言 1 + 1 == 2

py.test 中的默认输出就像

$ py.test test_single.py -v[...]test_single.py::test_addition PASSEDtest_single.py::test_addition2 PASSED

我想要

二加二还是四 PASSED一加一还是二PASSED

即使用文档字符串作为测试的描述.

我尝试在 conftest.py 文件中使用自定义:

导入pytest@pytest.mark.tryfirstdef pytest_runtest_makereport(item, call, __multicall__):# 执行所有其他钩子以获取报告对象rep = __multicall__.execute()if rep.when == "call":额外 = item._obj.__doc__.strip()rep.nodeid = 额外退货代表

这很接近,但它在每一行重复文件名:

$ py.test test_single.py======================================================================================== 测试会话开始 ==========================================================================================平台达尔文 -- Python 2.7.7 -- py-1.4.26 -- pytest-2.6.4插件:greendots、osxnotify、pycharm收集了 2 件物品test_single.py而且二加二还是四.test_single.py而且一加一还是二.====================================================================================== 2 在 0.11 秒内通过 ========================================================================================

如何避免输出中带有 test_single.py 的行,或者只打印一次?

查看 py.test 的源代码及其一些插件没有帮助.

我知道

如果您省略了文档字符串,将使用类/函数名称.

Here is a simple test file:

# test_single.py
def test_addition():
    "Two plus two is still four"
    assert 2 + 2 == 4

def test_addition2():
    "One plus one is still two"
    assert 1 + 1 == 2

The default output in py.test is like

$ py.test test_single.py -v
[...]
test_single.py::test_addition PASSED
test_single.py::test_addition2 PASSED

I would like to have

Two plus two is still four PASSED
One plus one is still two PASSED

i.e. use the docstrings as descriptions for the tests.

I tried to use a customization in a conftest.py file:

import pytest

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    # execute all other hooks to obtain the report object
    rep = __multicall__.execute()
    if rep.when == "call":
        extra = item._obj.__doc__.strip()
        rep.nodeid =  extra
    return rep

that is close, but it repeats the filename on every line:

$ py.test test_single.py
======================================================================================== test session starts =========================================================================================
platform darwin -- Python 2.7.7 -- py-1.4.26 -- pytest-2.6.4
plugins: greendots, osxnotify, pycharm
collected 2 items

test_single.py
And two plus two is still four .
test_single.py
And one plus one is still two .

====================================================================================== 2 passed in 0.11 seconds ======================================================================================

How can I avoid the lines with test_single.py in the output, or maybe print it only once?

Looking into the source of py.test and some of its plugins did not help.

I am aware of the pytest-spec plugin, but that uses the function's name as a description. I don't want to write def test_two_plus_two_is_four().

解决方案

To expand on my comment to @michael-wan's answer: to achive something similar to specplugin put into conftest.py:

def pytest_itemcollected(item):
    par = item.parent.obj
    node = item.obj
    pref = par.__doc__.strip() if par.__doc__ else par.__class__.__name__
    suf = node.__doc__.strip() if node.__doc__ else node.__name__
    if pref or suf:
        item._nodeid = ' '.join((pref, suf))

and the pytest output of

class TestSomething:
"""Something"""

def test_ok(self):
    """should be ok"""
    pass

will look like

If you omit docstrings class/func names will be used.

这篇关于使用 docstrings 列出 py.test 中的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆