0
0
PyTesttesting~10 mins

Log level filtering in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to capture only WARNING level logs in pytest.

PyTest
def test_warning_logs(caplog):
    import logging
    with caplog.at_level([1]):
        logging.warning("Warning message")
    assert "Warning message" in caplog.text
Drag options to blanks, or click blank then click option'
Alogging.WARNING
Blogging.ERROR
Clogging.INFO
Dlogging.DEBUG
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.INFO or logging.DEBUG will capture too many logs.
Using logging.ERROR will miss warning logs.
2fill in blank
medium

Complete the code to assert that no ERROR logs are captured.

PyTest
def test_no_error_logs(caplog):
    import logging
    with caplog.at_level(logging.INFO):
        logging.info("Info message")
    assert not any(record.levelname == [1] for record in caplog.records)
Drag options to blanks, or click blank then click option'
A"DEBUG"
B"WARNING"
C"ERROR"
D"CRITICAL"
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for wrong level names like "WARNING" or "DEBUG".
Using logging level constants instead of string names.
3fill in blank
hard

Fix the error in the code to capture only ERROR logs.

PyTest
def test_error_logs(caplog):
    import logging
    with caplog.at_level([1]):
        logging.error("Error occurred")
    assert "Error occurred" in caplog.text
Drag options to blanks, or click blank then click option'
Alogging.DEBUG
Blogging.ERROR
Clogging.INFO
Dlogging.WARNING
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.WARNING or logging.INFO instead of logging.ERROR.
Not importing logging inside the test.
4fill in blank
hard

Fill both blanks to capture only DEBUG logs and assert their presence.

PyTest
def test_debug_logs(caplog):
    import logging
    with caplog.at_level([1]):
        logging.debug("Debug info")
    assert any(record.levelname == [2] for record in caplog.records)
Drag options to blanks, or click blank then click option'
Alogging.DEBUG
B"DEBUG"
C"INFO"
Dlogging.INFO
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.INFO instead of logging.DEBUG.
Checking for level name "INFO" instead of "DEBUG".
5fill in blank
hard

Fill all three blanks to filter WARNING logs, log a warning, and assert its presence by level name.

PyTest
def test_warning_level_filter(caplog):
    import logging
    with caplog.at_level([1]):
        logging.[2]("Warning issued")
    assert any(record.levelname == [3] for record in caplog.records)
Drag options to blanks, or click blank then click option'
Alogging.WARNING
Bwarning
C"WARNING"
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.error or logging.WARNING as method name.
Checking for level name "ERROR" instead of "WARNING".