Repository navigation
Enable per test output correlation for pytest #22594
Description
Activity
- addedfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds PRReady to be worked onReady to be worked on
on Dec 6, 2023 Note: if you want to present Rerun results (available if you have the popular
pytest-rerunfailuresplugin installed), you will need to enable it by specifying-rARinstead ofrA.Reacted by Eleanor Boyd and JorgeRuizITCLThis feature is highly appreciated, would make it much easier to investigate complex test failures when running multiple tests.
Reacted by Eleanor Boyd, Joseph Finlayson, Michał Zawadzki, Thomas PUJOL, JorgeRuizITCL, Markus Scheidgen, dayson, bjfar, yipo and jobinnthomasThis would be pretty helpful as you can use the test runner to debug wip implementations.
Reacted by Eleanor Boyd, bjfar and jobinnthomasI just wanted to add that the same issue of log messages not appearing in test output occurs when using unittest too, not just pytest.
I've done some initial work on this. Here is a screen shot:
Sadly I discovered that the vscode api doesn't allow for attaching the output on succeeded tests. This was something I was hoping to do.Figured out how to do it from microsoft/vscode-discussions#2876My code for this is here:
main...garyvdm:vscode-python:test_result_captured_output
I wanted to re-use as much of the pytest code for formatting the output. This currently means that I'm using some private APIs. My next steps are to see if I can refactor the pytest api's to turn them into a public api. I'm thinking this will involve moving some methods from
TerminalReportertoBaseReport.Progress update:
- Lots or reading of pytest api's. Figured out a clean way to achieve the output with out using
TerminalReporter. - Engaged with pytest team on how to get a configured
TerminalWriterwithout using private apis.
Is `_pytest.config.create_terminal_writer` a private api? pytest-dev/pytest#14097 - I did lots of experimentation with
TestRun.appendOutputfor getting the output to show for successful tests. Unfortunately this api adds the output to both the individual test result, and to the main output result. The latter then messes up the pytest progress bar, as well as making the output too verbose. So I have logged a feature request to the vscode team to add an api to only add output to the individual test result.
If been able to click on successful tests and seeing the output is important to you, please can I ask that you go upvote this issue:
Add a way to attach output to a test, without it being added to the main test run output. vscode#286533
Next steps:
- Currently, we are not showing the output from teardown phase of tests. I'm going to work on finding a way to do this.
- Lots or reading of pytest api's. Figured out a clean way to achieve the output with out using
- marked Open correct position in
Pytest Test Logwith a single click on any test #17371 as a duplicate of this issueon Sep 23, 2026
A request was made in an earlier issue for this type of feature add, before I thought it was not possible given the configuration but following help from pytest contributors it should work. What will now happen is, based on pytest arguments the user controls the output (including stdout, stderr and logging) will be captured and returned in the error popup box inline and the testResult panel for each test. Error popup is only applicable for tests which fail, the panel will display output from either both types. All output will now be correlated with each test unless configured otherwise by the user.
Steps for Implementation:
def pytest_report_teststatus(report, config):report.capstdout,report.caplogandreport.capstderrmessageattribute of the Test Outcome object which is converted to json and sent to the extensionmessageattribute should be designed to allow for distinction between the different types of output since themessageattribute is of type string this will be done on the python sidemessageattribute will be read by the extension, it is then displayed dependent on test outcomemessageisn't empty and if it isn't then present that message to the user. My plan is this message will display in the testResult panel similar to the failed tests.User Configurability
Importantly in this design is the ability for user control of the output. There are two main pytest parameters that give the user control that our extension must respect:
-sor--capture=no: When you use this option, pytest does not capture the output of the tests and instead prints to console (default value is capture is on)-rAor--show-capture=all: This parameter has many "levels" where the user can chose passed tests, failed tests, skipped tests etc to have their capture shown to the user.-s-sflag, pytest will not capture any output and so ourmessageattribute will have a stack trace in the case of failure and any logging. the-sflag does not impact logging.-rAmessagewill respect this setting level. The only additional consideration is that if amessagevalue is empty (which might happen if the test passes and therAflag is off) we do not want to display a blank message in the testResult panel and therefore a check needs to be in place to confirm if it is empty.Test Case Examples
as well as the arguments
-sand-rAtried in different combinationsExpected Behavior Based on Arguments
when a test fails, the value of message will be:
-s-rAwhen a test passes, the value of message will be:
-s-rAVisuals
test failure popup:

testResult panel:
