首页 > 解决方案 > 通过pytest运行时如何在输出或命令行中显示测试函数的文档字符串

问题描述

我有一个 pytest 模块,其中包含以下带有文档字符串的测试函数,然后当我运行 pytest 时,我希望在每次测试运行的命令行中显示文档字符串,以便它可以清楚地查看测试用例。例如:-

test_module.py:-

def test_add():
    '''
    This is a test function for adding two numbers
    '''
    expected_ouput= 10
    actual_output = x.add(1,9)

    assert expected_output == actual_output

def test_sub():
    '''
    This is a test function for subtracting two numbers
    '''
    expected_ouput= 1
    actual_output = x.sub(1,9)

    assert expected_output == actual_output 


>pytest 

输出:-

====================================================================== test session starts =======================================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/deepak/PycharmProjects/test_projects, inifile:
collected 3 items

tests/test_module.py .
--
test_add
This is a test  function for adding two numbers                                                                                                                            [ 33%]
--

test_sub
This is a test function for subtracting two numbers
                                                                                                             [100%]

=================================================================== 2 passed in 19.78 seconds ====================================================================

这个怎么做 ?

标签: pythonpytest

解决方案


我不使用pytest,我使用uinttest2,但这似乎应该有效:

def test_blah():
    '''This is test blah'''
    print test_blah.__doc__

test_blah()

此外,这些单元测试工具中的大多数都提供了一种标准输出格式,第三方应用程序使用它来显示正在运行的测试(例如pyunit在 Eclipse IDE 下),因此可以使用这些第三方工具之一运行您的测试。


推荐阅读