preface
Generally speaking, there are two ways to save logs commonly used in work.
① The pytest testing framework has its own log management switch. (if the log function of pytest test framework is enabled, the log information will also be printed on the Terminal terminal when running the test case in the pytest command line.)
[note] both the conditions of ① enabling the log function of the pytest test framework and ② using the python log library are met. The log format and log level printed by the Terminal terminal are the formats set in the log module.
log_cli parameter is true:
log_cli parameter is false:
② Use python's log library. (this method is generally used to record the log during use case execution)
[note] when the test case of a single module is run separately, the log content will be printed or saved according to the log format and level set in the log configuration module (for example, if the log level is error, it will be printed to the console, and if the log level is info, it will be saved to the log file). When running in the command line mode,
Generally speaking, during the execution of test cases, log records are not output in real time or saved to log files. It seems that the execution results of test cases are not authoritative enough. (distribute logs to different terminals according to the level of logs)
[note] Application in the project: ① log setting module; ② Pytest test framework log_cli parameter is true. (this ensures that the corresponding log information can be viewed on the console or Terminal terminal no matter whether the test case of a module is run alone or in the pytest command line.)
The command line parameters of the pytest test framework for log are as follows:
--no-print-logs disable printing caught logs on failed tests. --log-level=LOG_LEVEL logging level used by the logging module --log-format=LOG_FORMAT log format as used by the logging module. --log-date-format=LOG_DATE_FORMAT log date format as used by the logging module. --log-cli-level=LOG_CLI_LEVEL cli logging level. --log-cli-format=LOG_CLI_FORMAT log format as used by the logging module. --log-cli-date-format=LOG_CLI_DATE_FORMAT log date format as used by the logging module. --log-file=LOG_FILE path to a file when logging will be written to. --log-file-level=LOG_FILE_LEVEL log file logging level. --log-file-format=LOG_FILE_FORMAT log format as used by the logging module. --log-file-date-format=LOG_FILE_DATE_FORMAT log date format as used by the logging module.
Another line:
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
Specific operation
① First, the pytest testing framework reads logs from pytest.ini_ CLI configuration is off by default, that is, false. [if the log_cli parameter is true, it means that the pytest test test framework's own log function is enabled, and if it is false, it means that it is closed.]
② Create a new pytest.ini or tox.ini or setup.cfg file in the root directory of the file, and then write the relevant logs as follows:
[pytest] log_cli=true ; # Open diary log_level=NOTSET ; # Log level log_format = %(asctime)s %(levelname)s %(message)s # Journal date log_date_format = %Y-%m-%d %H:%M:%S # Diary time addopts = -vs # Commands executed by log log_file = ./test.log # Log storage place log_file_level = info # Logging level log_file_format = %(asctime)s %(levelname)s %(message)s # Same as recording time log_file_date_format = %Y-%m-%d %H:%M:%S
③ You can use pytest -o to override (that is, overwrite the command line parameters related to log in the ini file); This function is implemented after pytest version 3.4, as follows:
pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO