python - 通过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 ====================================================================
这个怎么做 ?
解决方案
我不使用pytest
,我使用uinttest2
,但这似乎应该有效:
def test_blah():
'''This is test blah'''
print test_blah.__doc__
test_blah()
此外,这些单元测试工具中的大多数都提供了一种标准输出格式,第三方应用程序使用它来显示正在运行的测试(例如pyunit
在 Eclipse IDE 下),因此可以使用这些第三方工具之一运行您的测试。
推荐阅读
- powershell - “add-newipaddress”导致“Windows 系统错误 1168”和“找不到对象”
- java - AXIS 2 Web 服务访问中的 ClassCastException:RequestURIBasedDispatcher
- python - 使用托管标识向 SQL 数据库验证 Azure 应用服务
- google-apps-script - 有没有办法使用 Google Apps 脚本中的日历服务来获取群组访客?
- react-testing-library - 如何更新反应测试库?
- ssl - 如何跳过bitbucket中openshift webhook的证书验证?
- typescript - 声明'export type T1 = object & ComponentOptions的'&'是什么意思
' 在打字稿中? - javascript - 如何动态添加相同输入已输入文本框的次数?
- android - 从 Android Studio 中的 apk 中排除模块
- windows - 如何在智能卡子系统架构中自定义加密服务提供程序(基本 CSP)