Skip to content

Latest commit

 

History

History
66 lines (50 loc) · 2.41 KB

test_logs.md

File metadata and controls

66 lines (50 loc) · 2.41 KB

How to test logs

Contents

Why Test via Logs?

Sometimes code can be hard to test because it doesn't return anything. If you can run the code, adding logging is a relatively safe thing to do. Then if you can capture those logs in your tests, you can start to safely refactor and even modify existing behavior with confidence.

Testing Logs

There are 2 apis for testing logs, although they both work in the same manner.

1. with

This easiest way to test with simple logger is to use the with verify_simple_logger: which will capture everything logged in the follow context and verify it.

Example:

def test_variable():
    with verify_simple_logger():
        SimpleLogger.variable("dalmatians", 101, show_types=True)
        SimpleLogger.variable("dalmatians", 101, show_types=False)

snippet source | anchor

which produces:

variable: dalmatians = 101 <int>
variable: dalmatians = 101

snippet source | anchor

2. Log To String

Alternatively, you can achive the same result by logging to a string and then verifing the result

Example:

def test_variable_explict():
    output = SimpleLogger.log_to_string()
    SimpleLogger.variable("dalmatians", 101, show_types=True)
    SimpleLogger.variable("dalmatians", 101, show_types=False)
    verify(output)

snippet source | anchor