Skip to content

Commit

Permalink
fix failing logger tests (#675)
Browse files Browse the repository at this point in the history
* fix [test_logger]

* test [test_logger] - mark frontend test

* fix [github/workflows] - tests: install pytest-logger plugin

* refactor [test_logger] - reset_logging: propagate `True`

Co-authored-by: Theodor <theodor@xanadu.ai>
  • Loading branch information
sduquemesa and thisac committed Jan 21, 2022
1 parent 2d6e3ea commit 5b0ea3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: |
python3 -m pip install --upgrade pip
pip install -r requirements-ci.txt
pip install wheel codecov pytest pytest-cov pytest-randomly pytest-mock --upgrade
pip install wheel codecov pytest pytest-cov pytest-randomly pytest-mock pytest-logger --upgrade
- name: Install Strawberry Fields
run: |
Expand Down
41 changes: 21 additions & 20 deletions tests/frontend/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@
"""

import logging

import pytest

import strawberryfields.engine as engine

from strawberryfields.logger import logging_handler_defined, default_handler, create_logger

pytestmark = pytest.mark.frontend

modules_contain_logging = [engine]


Expand All @@ -63,27 +62,29 @@ def reset_logging(pytestconfig):
logging.root.handlers = []
root_level = logging.root.level

for module in modules_contain_logging:
logger = logging.getLogger(module.__name__)
logger.handlers = []
logger.setLevel(logging.NOTSET)
logger.propagate = True

logging_plugin = pytestconfig.pluginmanager.unregister(name="logging-plugin")

yield

for module in modules_contain_logging:
logger = logging.getLogger(module.__name__)
logger.handlers = []
logger.setLevel(logging.NOTSET)
logger.propagate = True

logging.root.handlers[:] = root_handlers
logging.root.setLevel(root_level)

if logging_plugin:
pytestconfig.pluginmanager.register(logging_plugin, "logging-plugin")


@pytest.fixture(autouse=True)
def reset_logging_module():
"""Reset the logging specific configurations such as handlers or levels for
the module specific loggers."""
for module in modules_contain_logging:
logger = logging.getLogger(module.__name__)
logger.handlers = []
logger.setLevel(logging.NOTSET)


@pytest.mark.parametrize("module", modules_contain_logging)
class TestLogger:
"""Tests for the functions that are used to create a logger"""
Expand Down Expand Up @@ -120,7 +121,7 @@ class TestLoggerIntegration:
"""Tests that the SF logger integrates well with user defined logging
configurations."""

def test_custom_configuration_without_sf_logger(self, tmpdir, caplog):
def test_custom_configuration_without_sf_logger(self, tmpdir):
"""Tests that if there was no SF logger created, custom logging
configurations work as expected and no configuration details were set
incorrectly."""
Expand All @@ -134,17 +135,17 @@ def test_custom_configuration_without_sf_logger(self, tmpdir, caplog):
assert "A log entry." in test_file.read()

@pytest.mark.parametrize("module", modules_contain_logging)
def test_default_sf_logger(self, module, capsys):
def test_default_sf_logger(self, module):
"""Tests that stderr is set for the SF logger by default as stream if
there were not other configurations made."""
level = logging.DEBUG

logger = create_logger(module.__name__)
assert len(logger.handlers) == 1
assert logger.handlers[0].stream.name == "<stderr>"
# checks if stream is stderr (stream name for stderr is 8 or 9, whereas stdout 6 or 7)
assert logger.handlers[0].stream.fileno() in [8, 9]

@pytest.mark.parametrize("module", modules_contain_logging)
def test_custom_logger_before_sf_logger_with_higher_level(self, module, tmpdir, caplog):
def test_custom_logger_before_sf_logger_with_higher_level(self, module):
"""Tests that a custom logger created before an SF logger will define
the level for logging as expected and the SF logger does not overwrite
the user configuration.
Expand All @@ -158,10 +159,10 @@ def test_custom_logger_before_sf_logger_with_higher_level(self, module, tmpdir,
custom_level = logging.DEBUG
sf_level = logging.WARNING

logger = logging.getLogger(module.__name__)
logger = logging.getLogger()
logging.basicConfig(level=custom_level)

sf_logger = create_logger(module.__name__, level=sf_level)
_ = create_logger(module.__name__, level=sf_level)

assert logging_handler_defined(logger)
assert logger.getEffectiveLevel() == custom_level

0 comments on commit 5b0ea3a

Please sign in to comment.