How to store pytest-html-reporter generated report to a custom location

2.4k views Asked by At

I am using pytest-html-reporter 0.2.4 plugin to generate execution reports. when i pass the command line argument "--html-report=./report/report.html" directly i am able to get the report in the given location.

I want to store the report in a seperate location with execution date(YYYYMMDD) as folder name and inside report_HHMM.html as the report name

This is the logic i am currently using

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    time_var = datetime.datetime.now()
    # create report target dir
    reports_dir = Path('reports', time_var.strftime('%Y%m%d'))
    reports_dir.mkdir(parents=True, exist_ok=True)
    # line to be add for addopts
    args = f"--html-report=./{reports_dir}/report_{time_var.strftime('%H%M')}.html"
    config.addinivalue_line("addopts", args)
    print("inside config function",config.getini("addopts"))

this is my pytest.ini file

[pytest]
python_files = test_*
python_functions = test_*
python_classes = *Tests
addopts =

using config.getini("addopts") i am able to see the report path getting added to the addopts,but there are no reports getting added to the created directory

2

There are 2 answers

0
Prashanth Sams On

If you wish to put the pytest-html-reporter report under folderA, you can just try this

pytest tests/ --html-report=./folderA/report.html
pytest tests/ --html-report=./folderA

or you can place them directly under the pytest.ini file

[pytest]
addopts = -vs -rf --html-report=./folderA
1
dave233 On
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if not os.path.exists('reports'):
        os.makedirs('reports')
    config.option.htmlpath = 'reports/report_' + datetime.now().strftime("%d-%m-%Y %H-%M-%S")+".html"

source : https://github.com/pytest-dev/pytest-html/issues/176