将测试结果报告为json文件的pytest插件

pytest-json-report的Python项目详细描述


Pytest JSON报告

Build StatuscodecovPyPI VersionPython Versions

这个pytest插件将测试报告创建为json。这使得在其他应用程序中处理测试结果变得很容易。

它可以报告摘要、测试详细信息、捕获的输出、日志、异常回溯等。此外,您还可以使用可用的fixture和hook来add metadatacustomize报告。

目录

安装

pip install pytest-json-report --upgrade 

选项

OptionDescription
^{}Create JSON report
^{}Target path to save JSON report (use "none" to not save the report)
^{}Just create a summary without per-test details
^{}List of fields to omit in the report (choose from: ^{}, ^{}, ^{}, ^{}, ^{}, ^{})
^{}Pretty-print JSON with specified indentation level

用法

只需使用--json-report运行pytest。默认情况下,报表保存在.report.json中。

$ pytest --json-report -v tests/
$ cat .report.json
{"created": 1518371686.7981803, ... "tests":[{"nodeid": "test_foo.py", "outcome": "passed", ...}, ...]}

如果您只需要知道有多少测试通过或失败,而不关心细节,则只能生成摘要:

$ pytest --json-report --json-report-summary

可以省略许多字段以保持报表大小较小。例如,这将忽略关键字和stdout/stderr输出:

$ pytest --json-report --json-report-omit keywords streams

如果不想保存报告,可以指定none作为目标文件名:

$ pytest --json-report --json-report-file none

元数据

您可以使用json_metadata测试设备将自己的元数据添加到测试项:

deftest_something(json_metadata):json_metadata['foo']={"some":"thing"}json_metadata['bar']=123

或者可以使用pytest_json_runtest_metadata钩子(在conftest.py中)根据当前测试运行添加元数据。返回的DICT将自动添加到现有元数据中。例如,这会将每个测试的call阶段的开始和停止时间添加到元数据:

defpytest_json_runtest_metadata(item,call):ifcall.when!='call':return{}return{'start':call.start,'stop':call.stop}

此外,还可以使用pytest-metadata's ^{} switch添加元数据,这将向报表的environment部分添加元数据,而不是向特定的测试项添加元数据。您需要确保所有元数据都是json可序列化的。

修改报告

您可以使用pytest_json_modifyreport挂钩在保存整个报表之前对其进行修改。

只需在conftest.py中实现钩子,例如:

defpytest_json_modifyreport(json_report):# Add a key to the reportjson_report['foo']='bar'# Delete the summary from the reportdeljson_report['summary']

pytest_sessionfinish之后,报表对象也可以通过config._json_report.report直接用于脚本。因此您可以使用一些内置挂钩来访问它:

defpytest_sessionfinish(session):report=session.config._json_report.reportprint(report['exitcode'])...

如果really想要更改如何将测试阶段运行的结果转换为json,则可以使用pytest_json_runtest_stage钩子。它接受一个^{}并返回一个json可序列化dict:

defpytest_json_runtest_stage(report):return{'outcome':report.outcome}

直接调用

当直接从代码中调用pytest.main()时,也可以使用插件:

importpytestfrompytest_jsonreport.pluginimportJSONReportplugin=JSONReport()pytest.main(['test_foo.py'],plugins=[plugin])

然后您可以访问report对象:

print(plugin.report)

并手动保存报告:

plugin.save_report('/tmp/my_report.json')

格式

json报告包含会话的元数据、摘要、收集器、测试和警告。您可以在^{}中找到一个示例报告。

KeyDescription
^{}Report creation date. (Unix time)
^{}Session duration in seconds.
^{}Process exit code as listed in the pytest docs. The exit code is a quick way to tell if any tests failed, an internal error occurred, etc.
^{}Absolute root path from which the session was started.
^{}Environment entry.
^{}Summary entry.
^{}Collectors entry. (absent if ^{} or if no collectors)
^{}Tests entry. (absent if ^{})
^{}Warnings entry. (absent if ^{} or if no warnings)

示例

{"created":1518371686.7981803,"duration":0.1235666275024414,"exitcode":1,"root":"/path/to/tests","environment":ENVIRONMENT,"summary":SUMMARY,"collectors":COLLECTORS,"tests":TESTS,"warnings":WARNINGS,}

小结

每个类别的结果数和测试项目总数。

KeyDescription
^{}Number of tests with that outcome. (absent if number is 0)
^{}Total number of tests.

示例

{"passed":2,"failed":3,"xfailed":1,"xpassed":1,"error":2,"skipped":1,"total":10}

环境

环境部分由pytest-metadata提供。该插件提供的所有元数据都将添加到此处,因此您需要确保它是json可序列化的。

示例

{"Python":"3.6.4","Platform":"Linux-4.56.78-9-ARCH-x86_64-with-arch","Packages":{"pytest":"3.4.0","py":"1.5.2","pluggy":"0.6.0"},"Plugins":{"json-report":"0.4.1","xdist":"1.22.0","metadata":"1.5.1","forked":"0.2","cov":"2.5.1"},"foo":"bar",# Custom metadata entry passed via pytest-metadata}

收集器

收集器节点的列表。这些功能有助于在不运行测试的情况下检查哪些测试可用,或者在测试发现期间调试错误。

KeyDescription
^{}ID of the collector node. (See docs) The root node has an empty node ID.
^{}Outcome of the collection. (Not the test outcome!)
^{}Nodes collected by the collector.
^{}Representation of the collection error. (absent if no error occurred)

result是收集的节点列表:

KeyDescription
^{}ID of the node.
^{}Type of the collected node.
^{}Line number. (absent if not applicable)

示例

[{"nodeid":"","outcome":"passed","result":[{"nodeid":"test_foo.py","type":"Module"}]},{"nodeid":"test_foo.py","outcome":"passed","result":[{"nodeid":"test_foo.py::test_pass","type":"Function","lineno":24,},...]},{"nodeid":"test_bar.py","outcome":"failed","result":[],"longrepr":"/usr/lib/python3.6 ... invalid syntax"},...]

测试

测试节点列表。每个完成的测试阶段都会生成一个stage对象(setupcallteardown)及其自身的outcome

KeyDescription
^{}ID of the test node.
^{}Line number where the test starts.
^{}List of keywords and markers associated with the test.
^{}Outcome of the test run.
^{}Test stage entry. To find the error in a failed test you need to check all stages. (absent if stage didn't run)
^{}Metadata item. (absent if no metadata)

示例

[{"nodeid":"test_foo.py::test_fail","lineno":50,"keywords":["test_fail","test_foo.py","test_foo0"],"outcome":"failed","setup":TEST_STAGE,"call":TEST_STAGE,"teardown":TEST_STAGE,"metadata":{"foo":"bar",}},...]

试验阶段

测试阶段的项目。

KeyDescription
^{}Duration of the test stage in seconds.
^{}Outcome of the test stage. (can be different from the overall test outcome)
^{}Crash entry. (absent if no error occurred)
^{}List of traceback entries. (absent if no error occurred)
^{}Standard output. (absent if none available)
^{}Standard error. (absent if none available)
^{}Log entry. (absent if none available)
^{}Representation of the error. (absent if no error occurred)

示例

{"duration":0.00018835067749023438,"outcome":"failed","crash":{"path":"/path/to/tests/test_foo.py","lineno":54,"message":"TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'"},"traceback":[{"path":"test_foo.py","lineno":65,"message":""},{"path":"test_foo.py","lineno":63,"message":"in foo"},{"path":"test_foo.py","lineno":63,"message":"in <listcomp>"},{"path":"test_foo.py","lineno":54,"message":"TypeError"}],"stdout":"foo\nbar\n","stderr":"baz\n","log":LOG,"longrepr":"def test_fail_nested():\n ..."}

对数

日志记录列表。日志记录的字段是^{} attributes,但exc_infoargs字段始终为空并且msg包含格式化的日志消息。

可以对日志记录应用^{}将其转换回一个logging.LogRecord对象。

示例

[{"name":"root","msg":"This is a warning.","args":null,"levelname":"WARNING","levelno":30,"pathname":"/path/to/tests/test_foo.py","filename":"test_foo.py","module":"test_foo","exc_info":null,"exc_text":null,"stack_info":null,"lineno":8,"funcName":"foo","created":1519772464.291738,"msecs":291.73803329467773,"relativeCreated":332.90839195251465,"thread":140671803118912,"threadName":"MainThread","processName":"MainProcess","process":31481},...]

警告

会话期间出现的警告列表。(参见pytest docs on warnings。)

KeyDescription
^{}File name.
^{}Line number.
^{}Warning message.
^{}When the warning was captured. (^{}, ^{} or ^{} as listed here)

示例

[{"code":"C1","path":"/path/to/tests/test_foo.py","nodeid":"test_foo.py::TestFoo","message":"cannot collect test class 'TestFoo' because it has a __init__ constructor"}]

相关工具

  • pytest-json有一些很好的特性,但似乎未被维护。我从那里借鉴了一些想法和测试用例。

  • tox has a swtich创建包含测试结果摘要的json报告。但是,它只提供了总体结果,没有任何每个测试的细节。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java GWT对话框从不显示帮助   java在简单的MapReduce作业中带来了极大的开销   javacom。mysql。jdbc。例外情况。jdbc4。MySQLIntegrityConstraintViolationException:列不能为null/onetoone映射   如何通过TCP/IP与Java和Labview进行通信,并发送浮点数据缓冲区?   java Apache camel与spring事件基本示例   java如何使我的秒表应用程序在完全关闭后仍能运行?   java Nutch爬网错误输入路径不存在   java是Mapreduce中按值传递还是按引用传递的键?   正则表达式替换java中的特定字符   Java DOM XML解析   java Eclipse未显示服务器的项目   使用Arraylist进行快速排序的排序Java实现疑难解答   java Split text包含字符串列表中的数字   检查Java中的两个lambda是否执行相同的代码?   java为什么dispatchTouchEvent避免在屏幕上单击?