Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - specify log level from the CLI command running the tests

Tags:

my team and I are using Pytest + Jenkins to automate our product testing. we have been using the standard Logging lib of python to get proper log messages during testing, before and after each test etc. we have multiple layers of logging, we log out ERROR, WARNING, INFO and DEBUG. the default value for our logger is INFO. we create the logger object in the primary setup of the tests, and pass it down to each object created, so all our logs go to the same logger.

so far when we are developing a new feature or test, we are working in DEBUG mode locally, and change it back to INFO when submitting new code to our SVN, but i am trying to add option to change logging level using the CLI, but i haven't found anything easy to implement. I've considered using Fixtures, but from what i understand those are only for the tests themselves, and not for the setup/tear-down phases, and the log is created regard less of the tests. any hack or idea on how to add a Pytest option to the CLI command to support changing logging level?

like image 957
Avishay Cohen Avatar asked May 11 '17 14:05

Avishay Cohen


People also ask

How do I change the log level in pytest?

By setting the log_cli configuration option to true , pytest will output logging records as they are emitted directly into the console. You can specify the logging level for which log records with equal or higher level are printed to the console by passing --log-cli-level .

How do I run a specific test in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How do I enable pytest logs?

Live logging is disabled by default; to enable it, set log_cli = 1 in the pyproject. toml 1 or pytest. ini 2 config.


1 Answers

Try --log-cli-level=INFO

like:

pytest -vv -s --log-cli-level=INFO --log-cli-format="%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)" --log-cli-date-format="%Y-%m-%d %H:%M:%S" ./test_file.py 
like image 61
EXLsunshine Avatar answered Sep 20 '22 20:09

EXLsunshine