Reputation: 183
So I have a testing folder structure (that tests a module src
not shown here) like this:
├── tests
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_main.py
And have the __init__.py
configure my logging as shown:
import structlog, logging, sys
from structlog.testing import LogCapture
logging.basicConfig(filename="test.log", filemode="w")
structlog.configure(
processors=[LogCapture()],
context_class=structlog.threadlocal.wrap_dict(dict),
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True
)
And my test_main.py
as shown:
import logging, structlog, pytest
from src.factories.factories import MyFactory
import src.main
from pathlib import Path
from datetime import datetime
@pytest.mark.parametrize(
"factory",
[
MyFactory(),
],
)
def test_main(path_to_results_folder_fixture_in_conftest, factory):
structlog.get_logger().info("test")
no_of_files_function = lambda: sum(1 for _ in Path(path_to_results_folder_fixture_in_conftest).iterdir())
len_before = no_of_files_function()
src.main.main("sony", factory, path_to_results_folder)
assert no_of_files_function() == len_before + 1
Then I simply run pytest test_main.py
. I was expecting the line structlog.get_logger().info("test")
to produce the line test
in the test.log
. However, there's only an empty test.log
generated. What am I doing wrong? I have configured logging, but structlog seem to not see it. For more information, nothing is also outputted to stdout/console.
Upvotes: 0
Views: 1591
Reputation: 1
By default, pytest captures all output and only displays it for failing tests. Setting capture=no or -s for short might solve your issue.
Try pytest -s test_main.py
or if using pytest.ini
[pytest]
addopts = -s
Upvotes: 0
Reputation: 4166
If you configure structlog
to use LogCapture
, it captures the log entries instead of printing them – that's the whole purpose of it. See also https://www.structlog.org/en/latest/testing.html
Upvotes: 3