1
votes

I'm using pytest to do unit testing. I have the logging code on the top of the calc_test.py source code:

module_logger = logging.getLogger(__name__)
module_logger.warning(f"Start module:{__name__}, the source file: {__file__}")

I expect that every time I run pytest -vv -s calc_test.py, the above logging info would show near the very beginning of the logs.

To my surprise, this logging never shows up. Even the loggings in conftest.py were successfully shown. I dug into the official documentation, to find any explantion of the execution order for each test file. However there is no mentioning of the loggings at the beginning of each file. My guess is that pytest will put on hold each testing files and first run all the stuff in conftest.py. But this isn't confirmed by the documentation. Very weird, could you explain this behavior?

I think this is covered in the documentation. - larsks
Especially check the part about live logs - you need something like --log-cli-level=WARNING. - MrBean Bremen
I've gone through the live logs section, but it doesn't seems to be the cause. And I already am using the warning level. I guess it might has something to do with the location of this conftest.py residing inside a package, where the __init__.py must be loaded first. I'll keep digging... - Jinghui Niu
Note that if you don't use --log-cli-level (or the respective setting in pytest.ini) nothing will be logged for logging calls. -s is only to show stdout. - MrBean Bremen