繁体   English   中英

在运行时处理 pytest 测试结果

[英]Processing pytest test results at runtime

所以我让 pytest 运行了我的测试,这很好,但我想对测试结果做一些实际的事情。 我正在使用 unittest,这给了我一个时髦的结果对象,我可以在测试运行后处理它。 Pytest 似乎只是给了我一个大文本转储——为此编写一个解析器听起来令人麻木的无聊。

我如何将结果转化为我可以使用的东西? 我肯定错过了什么。

顺便说一句 - 我正在使用 pytest.main() 运行我的测试,而不是通过命令行 py.test。 我希望有某种可以在运行时与之交互的结果对象。 我意识到我可以将结果写入磁盘、从磁盘读取、解析所述结果然后对结果采取行动——但似乎这些磁盘操作只是我应该能够避免的额外步骤。

py.test 结果文件并不真正意味着人类可读。 我不确定他们是否有第三方解析器。 它们旨在由HudsonTravis-ci等持续集成服务器读取。 正如@limelights 所说,您可以使用--junitxml=path标志获取这些服务的 xml 文件。 更多在这里 或使用--resultlog=path标志。 更多在这里

您也许可以对session对象进行一些自省

您可以在pytest_sessionfinish回调中进行操作以检查会话对象并查看您可以对其执行哪些爬网操作。 我知道您可以获得测试用例名称和路径以及所有这些,但它们的最终结果状态对我来说是难以快速找到的。

def pytest_sessionfinish(session):
    type(session)
    session
    import pdb
    pdb.set_trace()
(Pdb) type(session)
<class '_pytest.main.Session'>
(Pdb) session
<Session driving_develop_fw exitstatus=<ExitCode.TESTS_FAILED: 1> testsfailed=1 testscollected=2>


(Pdb) for d in dir(session): print(d)
CollectError
Failed
Interrupted
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__weakref__
_bestrelpathcache
_collect
_collectfile
_fixturemanager
_initialparts
_initialpaths
_matchnodes
_name2pseudofixturedef
_node_cache
_node_location_to_relpath
_nodeid
_norecursepatterns
_notfound
_parsearg
_perform_collect
_pkg_roots
_prunetraceback
_recurse
_repr_failure_py
_setupstate
_tryconvertpyarg
_visit_filter
add_marker
addfinalizer
collect
config
exitstatus
extra_keyword_matches
fspath
genitems
get_closest_marker
gethookproxy
getparent
ihook
isinitpath
items
iter_markers
iter_markers_with_node
keywords
listchain
listextrakeywords
listnames
matchnodes
name
nodeid
own_markers
parent
perform_collect
pytest_collectreport
pytest_collectstart
pytest_runtest_logreport
repr_failure
session
setup
shouldfail
shouldstop
startdir
teardown
testscollected
testsfailed
trace
warn

它是运行期间收集的单个测试用例的项目。 session.testscollectedsession.testsfailed可以让您计算收集了多少测试以及在这些测试中发现了多少失败。

仅供参考,会话参考似乎在层次结构中出现了几次。

(Pdb) hex(id(session))
'0x7fb717b1dcf8'
(Pdb) hex(id(session.items[1].session))
'0x7fb717b1dcf8'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM