Logging function is used as a method for monitoring events in order to identify software issues as they occur.
While Pytest excels at executing tests, it doesn’t inherently showcase the results of print statements or logs, posing a challenge when diagnosing test failures.
So, what’s the remedy? How can we effectively log and observe events in real-time during our unit testing?
The solution lies in Pytest logging, offering the capability to generate and even modify log handlers within Pytest.
The Python built-in logging module offers five standard levels that signify the severity of events:
- NOTSET (0): This level captures all messages, irrespective of their severity.
- DEBUG (10): Used for information aiding in identifying potential issues, such as variable values or program execution steps.
- INFO (20): Confirms that operations are proceeding as intended.
- WARNING (30): Indicates an unexpected event or potential issue in the near future (e.g., low disk space), but the software continues to function normally.
- ERROR (40): Signifies a significant problem that has hindered the software from executing a specific function.
- CRITICAL (50): Denotes an extremely severe error that could halt the program’s execution entirely.
Console Logger
The console logger leverages logging.StreamHandler() to output logs to the console (stdout). The logger’s level is set to INFO, meaning it will handle all log messages with a severity of INFO and above.
Below is an example of Console Logger.
import logging
def test_logging_console():
logger = logging.getLogger(__name__)
# Create a console handler
console_handler = logging.StreamHandler()
# Set the formatter for the console handler
formatter = logging.Formatter("%(asctime)s : %(levelname)s : %(name)s : %(message)s")
console_handler.setFormatter(formatter)
# Add the console handler to the logger
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
logger.debug("Debug statement is executed")
logger.info("Information statement is executed")
logger.warning("Warning mode, but test continues")
logger.error("Error has happened and failed the test")
logger.critical("Critical issue")
By default each captured log message shows the module, line number, log level and message.
If desired the log and date format can be specified to anything that the logging module supports by passing specific formatting options:
logging.Formatter("%(asctime)s : %(levelname)s : %(name)s : %(message)s")
![](https://qaautomation.expert/wp-content/uploads/2024/03/image-18.png?w=1200)
We can run the test by right clicking on the program and select Run ‘Python tests for test_logging_console’.
![](https://qaautomation.expert/wp-content/uploads/2024/03/image-16.png?w=444)
We can also run by using the below command in the terminal:
pytest test_logging_console.py -s
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2024/03/image-17.png?w=1200)
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!