How to collect data in pytest using junitxml?

Use the following code (conftest.py):

import random def test_val(): value = random.random() assert value < 0.5 

Running py.test --junitxml=result.xml conftest.py generates result.xml (when the test passes):

 <?xml version="1.0" encoding="utf-8"?> <testsuite errors="0" failures="0" name="" skips="0" tests="1" time="0.047"> <testcase classname="conftest" name="test_val" time="0.0"/> </testsuite> 

Now. What I would like to do is save the value generated by test_val() in results.xml . Is there any way to do this? I can not find anything related in the pytest doc .

+4
source share
2 answers

The sent junitxml plugin has no hooks for adding such data, you can print it to stdout, though, since it is added to the junitxml data.

So, while you print magazines, at least you can find out the data.

+3
source

You can solve your problem, but not with junitxml.

You can use pytest-harvest for this. Just install it and you can directly use the predefined appliances:

 import random def test_val(results_bag): value = random.random() # save it (before test !) results_bag.value = value assert value < 0.5 def test_synthesis(module_results_df): """ Shows that the 'module_results_df' fixture already contains what you need """ # drop the 'pytest_obj' column module_results_df.drop('pytest_obj', axis=1, inplace=True) print("\n 'module_results_df' dataframe:\n") print(module_results_df) 

Productivity

 >>> pytest -s -v ============================= test session starts ============================= collecting ... collected 2 items tmp.py::test_val PASSED tmp.py::test_synthesis 'module_results_df' dataframe: status duration_ms value test_id test_val passed 0.999928 0.443547 PASSED ========================== 2 passed in 1.08 seconds =========================== 

Then you can write the report to a separate file (requiring the junit xml file to be combined with you). Note that you do not need to write the above in the test, you can do this using any of the pytest hooks (either where you have access to the above devices, or to the pytest request.session object).

See the documentation for details.

Finally, if you also want to use parameters, fixtures, steps ... you can look at this sample data performance test.

Incidentally, I am the author;)

0
source

Source: https://habr.com/ru/post/1402647/


All Articles