documentation of caplog fixture
@@ -259,6 +259,40 @@ which does assert that units are equal, and calls `numpy.testing.assert_equal` f
.. _dev_random:
+
+
+Caplog fixture
+--------------
+
+Inside tests, we have the possibility to change the log level for the captured log messages using the ``caplog`` fixture which allow you to access and control log capturing.
+When logging is part of your function and you want to verify the right message is logged with the expected logging level:
+
+
+.. testcode::
+
+ import pytest
+ import logging
+
+ @pytest.fixture
+ def my_caplog(caplog):
+ yield caplog
+ assert len(caplog.get_records(when='call')) > 0 # passes
+
+
+ def test_nowarn(my_caplog):
+ logging.getLogger().warning('Hello gammapy user')
+ assert len(my_caplog.records) > 0 # passes
+
+ def test_something(caplog):
+ assert caplog.records[-1].levelname == "WARNING"
+ assert "warning message" in caplog.records[-1].message
+
+
+
+
+
+
+
Random numbers
--------------
@@ -343,6 +377,8 @@ informing the user about what they are doing.
Gammapy uses the Python standard library `logging` module. This module is extremely flexible,
but also quite complex. But our logging needs are very modest, so it's actually quite simple ...
+It is worth mentioning that important logs returned to the user should be captured and tested using caplog fixture, see the section Caplog fixture above
+
Generating log messages
+++++++++++++++++++++++
