I am trying to use the pytest
invocation and the pytest.ini
file to automatically log SETUP
and TEARDOWN
messages into the junit
xml file. This is what I have so far (in my jenkins pipeline) for the pytest
invocation:
pytest -o log_cli=true --durations=20 --full-trace -rPA --setup-show --show-capture=all --capture=tee-sys -x -vv --junitxml='afile.xml' tests/<secret> --assert-commit-sha '${GIT_COMMIT}' --environment '${TEST_ENV_NAME}'"
And the pytest.ini
file:
[pytest]
log_cli=true
log_cli_level=warning
log_format = %(asctime)s.%(msecs)03d %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
# The initial try
; junit_logging=all
junit_logging=system-out
junit_log_passing_tests = True
; junit_family=xunit2
Here is the output from the original test sent to std::cout
(names replaced):
tests/integration/test_output.py::test_outs[output_1]
SETUP F target_with_reboot (fixtures used: target_dev)
SETUP F output_state_expected[(0, 0, 0, 1, 0, 0)]
integration/test_output.py::test_outs[output_1] (fixtures used: output_state_expected, request, target_dev, target_with_reboot)PASSED
TEARDOWN F output_state_expected[(0, 0, 0, 1, 0, 0)]
TEARDOWN F target_with_reboot
This output is still not getting sent to the JUnit XML file. Is there a way to get those teardown and setup messages to show up in a log file? This is an example of the generated XML junit file using the pytest
arguments:
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="7" time="308.753" timestamp="2022-11-30T20:46:31.600326" hostname="11c171ce2d27"><testcase classname="system.test_up" name="test_is_up" time="0.058"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="system.test_up" name="test_assert_commit" time="6.821"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="integration.test_outputs" name="test_get_outputs" time="0.031"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="integration.test_outputs" name="test_set_outputs[output_state_expected0]" time="96.031"><system-out>--------------------------------- Captured Log ---------------------------------
2022-11-30 20:46:38.602 WARNING not on root screen, cylcing 1
2022-11-30 20:46:41.140 WARNING not on root screen, cylcing 2
2022-11-30 20:46:43.665 WARNING not on root screen, cylcing 3
2022-11-30 20:46:46.191 WARNING not on root screen, cylcing 4
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="integration.test_outputs" name="test_set_outputs[output_state_expected1]" time="68.280"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="integration.test_outputs" name="test_set_outputs[output_state_expected2]" time="92.446"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase><testcase classname="integration.test_smoke" name="test_smoketest_ui" time="45.039"><system-out>--------------------------------- Captured Log ---------------------------------
--------------------------------- Captured Out ---------------------------------
</system-out><system-err>--------------------------------- Captured Err ---------------------------------
</system-err></testcase></testsuite></testsuites>
I understand that the setup and teardown text shows up with the --setup-show
argument to pytest
. I am simply trying to log that output from pytest
into the junit XML file.
I was looking at the source code here:
https://docs.pytest.org/en/7.1.x/_modules/_pytest/junitxml.html
And it looks like it is possible to do what I want to do. I am just not sure how. Sources cited:
https://stackoverflow.com/questions/61167147/add-logs-in-junit-xml-from-pytest-generate-tests-method
https://buildmedia.readthedocs.org/media/pdf/pytest/latest/pytest.pdf
https://stackoverflow.com/questions/60212552/pytest-deprecation-junit-family-default-value-will-change-to-xunit2