
I'm happy to bring you a new release of logassert, a simple logging assertion mechanism for Python unittests. Because we all know that we must test the logging calls in our programs, right? With `logassert` this is very easy. This new release brings the functionality of logassert as a pytest fixture, with totally renewed semantics (for "classic unittest" structures, logassert is not changing much, to be backwards compatibile). So, for pytest all you need to do is to declare `logs` in your test arguments (it works just like any other fixture), and then just check (using `assert`, as usual with *pytest*) if a specific line is in the logs for a specific level. See this example, with several log lines, and a specific assertion: logger.info("Starting system") places = ['/tmp/', '~/temp'] logger.debug("Checking for config XYZ in all these places %s", places) logger.warning("bad config XYZ") assert "bad config XYZ" in logs.debug See how the test failure message is super helpful: assert for regex 'bad config XYZ' check in DEBUG, failed; logged lines: INFO 'Starting system' DEBUG "Checking for config XYZ in all these places ['/tmp/', '~/temp']" WARNING 'bad config XYZ' You can install logassert from PyPI: https://pypi.org/project/logassert/ The project is in Github: https://github.com/facundobatista/logassert Interested on more detail about how this work? As I said above, you include the `logs` fixture and just assert. Example: def test_bleh(logs) (...) assert "The meaning of life is 42" in logs.debug Actually, the line you write is a regular expression, so you can totally do (in case you're not exactly sure which the meaning of life is): assert "The meaning of life is \d+" in logs.debug The indicated string is searched to be inside the log lines, it doesn't need to be exact whole line. If you want that, just indicate it as with any regular expression: assert "^The meaning of life is \d+$" in logs.debug In a similar way you can also express the desire to check if it's at the beginning or at the end of the log lines. **NOTE**: the message checked is the final one, after the logging system replaced all the indicated parameters in the indicated string. If you want to verify that a text was logged, no matter at which level, just do: assert "The meaning of life is 42" in logs.any_level To verify that some text was NOT logged, just juse the Python's syntax! For example: assert "A problem happened" not in logs.error If you don't like regexes just import `Exact` from `logassert` and wrap the string with that. For example, in this case the `..` means exactly two dots, no regex semantics at all: assert Exact("The meaning of life is ..") in logs.any_level If you need help, or have any question, or found any issue, please open a ticket [here](https://github.com/facundobatista/logassert/issues/new). Thanks in advance for your time. -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org.ar/ Twitter: @facundobatista
participants (1)
-
Facundo Batista