简体   繁体   English

在运行时处理 pytest 测试结果

[英]Processing pytest test results at runtime

So I have have pytest run my tests and that's great, but I want to actually do something with the test results.所以我让 pytest 运行了我的测试,这很好,但我想对测试结果做一些实际的事情。 I was using unittest, and that gives me a swanky results object that I can process after the tests have run.我正在使用 unittest,这给了我一个时髦的结果对象,我可以在测试运行后处理它。 Pytest just seems to give me a big text dump - writing a parser for that sounds mind-numbingly boring. Pytest 似乎只是给了我一个大文本转储——为此编写一个解析器听起来令人麻木的无聊。

How do I get the results into something I can use?我如何将结果转化为我可以使用的东西? I must be missing something.我肯定错过了什么。

btw - I'm running my tests using pytest.main(), not via the command line py.test.顺便说一句 - 我正在使用 pytest.main() 运行我的测试,而不是通过命令行 py.test。 I was hoping to have some sort of result object I can interact with at runtime.我希望有某种可以在运行时与之交互的结果对象。 I realize I can write results to disk, read from disk, parse said results and then act on results - but it seems like those disk operations are just extra steps I should be able to avoid.我意识到我可以将结果写入磁盘、从磁盘读取、解析所述结果然后对结果采取行动——但似乎这些磁盘操作只是我应该能够避免的额外步骤。

py.test result files are not really meant to be human readable. py.test 结果文件并不真正意味着人类可读。 I'm not sure if there is a third-party parser for them or not.我不确定他们是否有第三方解析器。 They are meant to be read by a continuous integration server like Hudson or Travis-ci .它们旨在由HudsonTravis-ci等持续集成服务器读取。 As @limelights said you get the xml files for these services with the --junitxml=path flag.正如@limelights 所说,您可以使用--junitxml=path标志获取这些服务的 xml 文件。 More here .更多在这里 or with the --resultlog=path flag.或使用--resultlog=path标志。 More here .更多在这里

You may be able to do some introspection with the session object您也许可以对session对象进行一些自省

you can play around in the pytest_sessionfinish callback to inspect the session object and see what crawling actions you can do over it.您可以在pytest_sessionfinish回调中进行操作以检查会话对象并查看您可以对其执行哪些爬网操作。 I know you can get the test case names and paths and all that, but their final result status is proving elusive to me to locate in a quick time.我知道您可以获得测试用例名称和路径以及所有这些,但它们的最终结果状态对我来说是难以快速找到的。

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

It is the items that are the individual test cases that were collected during the run.它是运行期间收集的单个测试用例的项目。 The session.testscollected and session.testsfailed give you a count for how many tests were collected and how many failures were seen across those tests. session.testscollectedsession.testsfailed可以让您计算收集了多少测试以及在这些测试中发现了多少失败。

Fyi the session reference seems to show up a few times down the hierarchy.仅供参考,会话参考似乎在层次结构中出现了几次。

(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