Skip to content Skip to sidebar Skip to footer

How To Create New Log File For Each Run Of Tests In Pytest?

I have created a pytest.ini file, addopts = --resultlog=log.txt This creates a log file, but I would like to create a new log file everytime I run the tests. I am new to the pytes

Solution 1:

Note

--result-log argument is deprecated and scheduled for removal in version 6.0 (see Deprecations and Removals: Result log). The possible replacement implementation is discussed in issue #4488, so watch out for the next major version bump - the code below will stop working with pytest==6.0.

Answer

You can modify the resultlog in the pytest_configure hookimpl. Example: put the code below in the conftest.py file in your project root dir:

import datetime


def pytest_configure(config):
    ifnotconfig.option.resultlog:
        timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H-%M-%S')
        config.option.resultlog = 'log.' + timestamp

Now if --result-log is not passed explicitly (so you have to remove addopts = --resultlog=log.txt from your pytest.ini), pytest will create a log file ending with a timestamp. Passing --result-log with a log file name will override this behaviour.

Solution 2:

Answering my own question. As hoefling mentioned --result-log is deprecated, I had to find a way to do it without using that flag. Here's how I did it,

conftest.py

from datetime import datetime
import logging

log = logging.getLogger(__name__)

defpytest_assertrepr_compare(op, left, right):
    """ This function will print log everytime the assert fails"""
    log.error('Comparing Foo instances:    vals: %s != %s \n' % (left, right))
    return ["Comparing Foo instances:", " vals: %s != %s" % (left, right)]

defpytest_configure(config):
    """ Create a log file if log_file is not mentioned in *.ini file"""ifnot config.option.log_file:
        timestamp = datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S')
        config.option.log_file = 'log.' + timestamp

pytest.ini

[pytest]log_cli = truelog_cli_level = CRITICAL
log_cli_format = %(message)s
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format=%Y-%m-%d %H:%M:%S

test_my_code.py

importlogginglog= logging.getLogger(__name__)

def test_my_code():
    ****test code

Solution 3:

You can have different pytest run logs by naming the log file the time when test execution starts.

pytest tests --log-file $(date'+%F_%H:%M:%S') 

This will create a log file for each test run. And the name of the test run would be the timestamp.

$(date '+%F_%H:%M:%S') is the bash command to get current timestamp in DATE_Hr:Min:Sec format.

Post a Comment for "How To Create New Log File For Each Run Of Tests In Pytest?"