简体   繁体   English

py.test记录通过的测试

[英]py.test logging for tests that pass

I have been over and over the documentation, but I can not fine out how I get py.test to write to the log for tests that pass. 我一遍又一遍地在文档中,但我不知道如何让py.test写入日志以获得通过的测试。 For example if I run "py.test --resultlog=mylog.txt myPytest.py" mylog.txt only has one line for each test that passed. 例如,如果我运行“py.test --resultlog = mylog.txt myPytest.py” mylog.txt只为每个通过的测试都有一行。 I want to add other things to each test recorded in my log file, regardless whether thy passed or failed. 我想在我的日志文件中记录的每个测试中添加其他内容,无论您是通过还是失败。 For example I need to know the time they ran, some of the output data etc. How do I get py.test to include data about the test (passed and failed) in the py.test log file? 例如,我需要知道它们运行的​​时间,一些输出数据等。如何让py.test在py.test日志文件中包含有关测试(传递和失败)的数据?

You can have a look if the junitxml output provides any more information. 您可以查看junitxml输出是否提供了更多信息。 But I suspect that if you want the actual time rather then duration etc you will have to write your own plugin. 但我怀疑,如果你想要实际时间而不是持续时间等,你将不得不编写自己的插件。 The documentation gives you the relevant hooks: http://pytest.org/latest/plugins.html?highlight=hooks#reporting-hooks 该文档为您提供了相关的钩子: http//pytest.org/latest/plugins.html?highlight = hooks # reporting- hooks

pytest now supports the -s argument to prevent capturing logging calls for passing tests. pytest现在支持-s参数,以防止捕获日志调用以传递测试。

pytest -s

From the documentation : 文档

-s, –capture=no Normally stdout and stderr are captured and only shown for failing tests. -s,-capture = no通常stdout和stderr被捕获,仅显示失败的测试。 The -s option can be used to disable capturing, showing stdcalls for print statements, logging calls, etc. -s选项可用于禁用捕获,显示打印语句的stdcalls,记录调用等。

you could use py-cov combine with pytest 你可以使用py-cov与pytest结合使用

py.test --cov $(MOD_NAME) --cov-config .coveragerc --cov-report xml --junit-xml=results.xml

and here you will get all the info you need as we are used to push the coverage.xml file to sonarqube and get all the data available. 在这里,您将获得所需的所有信息,因为我们习惯将coverage.xml文件推送到sonarqube并获取所有可用数据。

If you do really need customized data/logs coming from your tests functions just pass -s so pytest will stop catching the output and it will print it on the console (and you can stream it to a file maybe, not that nice) 如果你真的需要来自你的测试函数的自定义数据/日志,只需传递-s所以pytest将停止捕获输出,它将在控制台上打印它(你可以将它流式传输到文件,不是很好)

This hook is an experimental and it is called whenever the assertion passes. 这个钩子是一个实验,只要断言通过就会调用它。 From documentation- 从文件 -

Use this hook to do some processing after a passing assertion. 在传递断言后使用此钩子进行一些处理。 The original assertion information is available in the orig string and the pytest introspected assertion information is available in the expl string. 原始断言信息在orig字符串中可用,而pytest内省断言信息在expl字符串中可用。

This hook must be explicitly enabled by the enable_assertion_pass_hook ini-file option: 必须通过enable_assertion_pass_hook ini-file选项显式启用此挂接:

[pytest]
enable_assertion_pass_hook=true

You need to clean the .pyc files in your project directory and interpreter libraries when enabling this option, as assertions will require to be re-written. 启用此选项时,需要清除项目目录和解释器库中的.pyc文件,因为断言需要重写。

import logging
log = logging.getLogger(__name__)
def pytest_assertion_pass(item, lineno, orig, expl):
   """
      Prints the log(log file) every time the assert passes
   """
   log.info(str(item) + ' | lineno: ' + str(lineno) + ' | ' + orig + ' | PASS')

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

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