Repository: FAIRmat-NFDI/nomad-measurements (language: python)

Issue:
Looks for errors in the structlogger in tests
When running the test for `nomad.client.normalize_all`, the errors that might occur are handled by the `structlogger`. This allows the `pytest` to pass successfully.

Having a fixture that looks for the keyword `ERROR` in the `structlogger` can resolve this, i.e., failing the tests when there are errors in the normalize step.


Discussion / hints:

Fix patch:
diff --git a/pyproject.toml b/pyproject.toml
index 54342525..8d935503 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -51,7 +51,8 @@ dev = [
     "pytest",
     "ruff",
     "structlog",
-    "pynxtools", 
+    "pynxtools",
+    "python-logstash>=0.4.6",
 ]
 
 [tool.ruff]


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 00000000..d83f9c16
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,36 @@
+import logging
+
+import pytest
+import structlog
+from nomad.utils import structlogging
+from structlog.testing import LogCapture
+
+structlogging.ConsoleFormatter.short_format = True
+setattr(logging, 'Formatter', structlogging.ConsoleFormatter)
+
+
+@pytest.fixture(
+    name='caplog',
+    scope='function',
+)
+def fixture_caplog(request):
+    """
+    Extracts log messages from the logger and raises an assertion error if the specified
+    log levels in the `request.param` are found.
+    """
+    caplog = LogCapture()
+    processors = structlog.get_config()['processors']
+    old_processors = processors.copy()
+
+    try:
+        processors.clear()
+        processors.append(caplog)
+        structlog.configure(processors=processors)
+        yield caplog
+        for record in caplog.entries:
+            if record['log_level'] in request.param:
+                assert False, record
+    finally:
+        processors.clear()
+        processors.extend(old_processors)
+        structlog.configure(processors=processors)
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 34e93a13..0fe096e3 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -22,6 +22,7 @@
 
 
 @pytest.fixture(
+    name='parsed_archive',
     params=[
         'XRD-918-16_10.xrdml',
         'm54313_om2th_10.xrdml',
@@ -31,9 +32,9 @@
         'Omega-2Theta_scan_high_temperature.rasx',
         'RSM_111_sdd=350.rasx',
         'TwoTheta_scan_powder.rasx',
-    ]
+    ],
 )
-def parsed_archive(request):
+def fixture_parsed_archive(request):
     """
     Sets up data for testing and cleans up after the test.
     """
@@ -51,9 +52,21 @@ def parsed_archive(request):
         os.remove(measurement)
 
 
-def test_normalize_all(parsed_archive):
+@pytest.mark.parametrize(
+    'caplog',
+    ['error', 'critical'],
+    indirect=True,
+)
+def test_normalize_all(parsed_archive, caplog):
+    """
+    Tests the normalization of the parsed archive.
+
+    Args:
+        parsed_archive (pytest.fixture): Fixture to handle the parsing of archive.
+        caplog (pytest.fixture): Fixture to capture errors from the logger.
+    """
     normalize_all(parsed_archive)
-    print(parsed_archive.data)
+
     assert parsed_archive.data.xrd_settings.source.xray_tube_material == 'Cu'
     assert parsed_archive.data.results[
         0