Registering py.test for tests that pass

I've been over and over the documentation, but I can't figure out how to get py.test to be logged to pass the tests. For example, if I run "py.test --resultlog = mylog.txt myPytest.py" , mylog.txt only has one line for each test passed. I want to add other things to every test written in my log file, whether it passed or failed. For example, I need to know when they started, some output, etc. How do I get py.test to include test data (passed and failed) in the py.test log file?

+4


source to share


4 answers


You can see if the junitxml output will provide more information. But I suspect that if you want the actual time, not the duration, you will have to write your own plugin. The documentation provides the relevant bindings: http://pytest.org/latest/plugins.html?highlight=hooks#reporting-hooks



+1


source


Pytest now supports the -s argument to prevent hijacking calls to pass tests.

pytest -s

      



From the documentation :

-s, –capture = no Normally stdout and stderr are logged and displayed only for failed tests. The -s option can be used to disable capturing, display standard calls to print operators, log calls, etc.

+1


source


you can use py-cov to combine with pytest

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

      

and this is where you get all the information you need as we are using to pull the coverage.xml file into sonarqube and get all the data available.

If you really want the custom data / logs coming from your test functions, just pass -s

so that pytest will stop capturing the output and print it to the console (and you can pipe it to a file, maybe not good)

0


source


This hook is experimental and is called whenever an assertion passes. From documentation-

Use this hook to do some post-approval processing. The initial assertion information is available on the orig line and the pytest validation information is available on the expl line.

This hook must be explicitly enabled with the ini option of the enable_assertion_pass_hook file:

[pytest]
enable_assertion_pass_hook=true

      

With this option enabled, you need to clean up the .pyc files in your project directory and in the interpreter libraries, as the assertions need to be rewritten.

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')

      

0


source







All Articles