Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.INFO or logging.DEBUG will capture too many logs.
Using logging.ERROR will miss warning logs.
✗ Incorrect
Use logging.WARNING to capture warning level logs.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for wrong level names like "WARNING" or "DEBUG".
Using logging level constants instead of string names.
✗ Incorrect
We check that no log record has level name "ERROR".
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.WARNING or logging.INFO instead of logging.ERROR.
Not importing logging inside the test.
✗ Incorrect
Use logging.ERROR to capture error level logs.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
logging.INFO instead of logging.DEBUG.Checking for level name "INFO" instead of "DEBUG".
✗ Incorrect
Use logging.DEBUG to capture debug logs and check for level name "DEBUG".
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
logging.error or logging.WARNING as method name.Checking for level name "ERROR" instead of "WARNING".
✗ Incorrect
Set level to logging.WARNING, log with logging.warning, and check for level name "WARNING".