call to pytest_runtest_logreport is not proper
See original GitHub issuefrom _pytest/runner.py
def call_and_report(item, when, log=True, **kwds):
call = call_runtest_hook(item, when, **kwds)
hook = item.ihook
report = hook.pytest_runtest_makereport(item=item, call=call)
#import pdb; pdb.set_trace()
if log:
hook.pytest_runtest_logreport(report=report) # -----> **this logreport has been overridden in our
custom plugins**
if check_interactive_exception(call, report):
hook.pytest_exception_interact(node=item, call=call, report=report)
return report
for every test function , “pytest_runtest_logreport
” is called after each of the setup/call/teardwon sections are executed.
but during the teardown of the last test function:
def teardown_exact(self, item, nextitem):
needed_collectors = nextitem and nextitem.listchain() or []
self._teardown_towards(needed_collectors)
nextitem
is none
[which is expected] so needed_collectors = []
In _teardown_towards
function self.stack
never gets equl to needed_collectors
unless all the teardown functions are called, that is function , module and session . and after which the runtest_log_report
is called . which is not the case if it were not the last test case.
def _teardown_towards(self, needed_collectors):
import pdb; pdb.set_trace()
while self.stack:
if self.stack == needed_collectors[:len(self.stack)]:
break
self._pop_and_teardown()
due to this our reporting is getting impacted as we have overridden the pytest_runtest_logreport
, please
help here.
edited by @The-Compiler to add proper code formatting as this was unreadable otherwise 😉
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to manage logging — pytest documentation
pytest captures log messages of level WARNING or above automatically and displays them in their own section for each failed test in the...
Read more >Logging within pytest tests - python - Stack Overflow
Since version 3.3, pytest supports live logging, meaning that all the log records emitted in tests will be printed to the terminal ...
Read more >Effective Python Testing With Pytest
By placing disable_network_calls() in conftest.py and adding the autouse=True option, you ensure that network calls will be disabled in every ...
Read more >pytest Documentation - Read the Docs
test run reporting finishing. Note: Calling pytest.main() will result in importing your tests and any modules that they import. Due to the.
Read more >Testing Python Applications with Pytest - Semaphore Tutorial
This tutorial will get you started with using pytest to test your next ... Running the tests at this point should fail since...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gkambalu I see, thanks for confirming. I don’t think the current implementation is incorrect though, let me explain my reasoning:
pytest_runtest_logreport
is used to report “setup”, “call” and “teardown” stages after the stage has been complete. This allows hook implementations to react to failure at any of those stages.So the current sequence of events:
“teardown” for
"test_foo.py::test_second"
is called only after all the fixtures which are part of its teardown process have also been finalized. This is important becausemodule fixture
might fail during “teardown”, allowing it to be properly reported.Now if the teardown sequence was:
And
module fixture
fails during teardown, which hook would we call to inform hook implementations of that, given that the teardown stage of “test_foo.py::test_second” was already completed?So the current implementation seems correct from my POV. I also understand your interpretation and it makes sense, but the current implementation also has logic behind it.
Having said that, I think you can obtain the behavior you want by using
hookwrappers
instead of plain hooks? For example using a hook wrapper aroundpytest_runtest_setup
you can get called before any fixture is even created:pytest_fixture_setup
andpytest_fixture_post_finalizer
might also be helpful.Closing for now.