pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

How to specify report name and path in conftest instead of pass via arguments

Open NazarSilchenko opened this issue 6 years ago • 6 comments

I generate folder for logs with timestamp and I want to store my html report there. Unfortunately code which generate folder is in pytest framework so I can not know name of folder when I start my test. As the result my html report stores to root folder of my framework.

Is there a way to specify(overwrite) path where to store html report in conftest file instead of specifying it via arguments? Sorry if this is a duplicate issue, I did my best to find it but w/o success

Thanks in advance.

NazarSilchenko avatar Aug 20 '18 09:08 NazarSilchenko

You could implement the pytest_configure hook to change the htmlpath option. For example:

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    config.option.htmlpath = '/path/to/report.html'

davehunt avatar Aug 23 '18 10:08 davehunt

i use this one

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if not os.path.exists('reports'):
        os.makedirs('reports')
    config.option.htmlpath = 'reports/'+datetime.now().strftime("%d-%m-%Y %H-%M-%S")+".html"

that create a report folder and create a new report for every launch

andreabisello avatar Oct 14 '19 13:10 andreabisello

I tried this and it says there's no fixture named config. I thought maybe pytest_configure(config) was a special function but when I tried to create it I got a huge error.

Code: @pytest.hookimpl(tryfirst=True) def pytest_configure(config): feature_name = os.environ.get('PYTEST_CURRENT_TEST').replace('test_', '').split(':')[-1].split('__')[0] report_dir = os.path.join(os.path.dirname(file), '../reports', feature_name) config.option.htmlpath = os.path.join(report_dir, 'report.html')

the error is too long to paste, but its not caused by any of my code because I added a breakpoint at the beginning of the function and it didnt get there. Maybe its my version of pytest?

I also tried : request.config.option.htmlpath = os.path.join(report_dir, 'report.html')

that doesn't do anything. Best solution I've found is to run a system command to move the file at the end of the test

jshtaway avatar Apr 01 '20 02:04 jshtaway

@zen-gineer try updating pytest, this is my conftest.py

it create report filename consiering also the parameter passed


import pytest
import os
from datetime import datetime
from py.xml import html

def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.h1("Web Services Soap Test Suite")])
    prefix.extend([html.img(src="header.png", style="display:block; margin-left:auto; margin-right:auto;")])



def pytest_configure(config):
    # questo fa sparire la sezione environment

    if not os.path.exists('reports'):
        os.makedirs('reports')

    file_name = datetime.now().strftime("%d-%m-%Y %H-%M-%S") + ".html"

    moduli = config.option.file_or_dir

    if moduli is not None:
        for modulo in moduli:
            print("modulo -> " + modulo)
            if modulo is not None and ".py" in modulo:
                nome_modulo = modulo.split("/").pop()[:-3]
                file_name = nome_modulo+"_"+file_name

    parameters = config.option.testconfig

    if parameters is not None:
        for param in parameters:
            if param is not None:
                print("parametro -> " + param)
            if param is not None and ".ini" in param:
                configurazione = param.split("/").pop()[:-4]
                file_name = configurazione+"_"+file_name

    file_name = file_name.replace(":","")

    config.option.htmlpath = 'reports/' + file_name



def pytest_report_header(config):
    return "Suite di test dei webservices"


def pytest_runtest_makereport(item, call):
    if "incremental" in item.keywords:
        if call.excinfo is not None:
            parent = item.parent
            parent._previousfailed = item


def pytest_runtest_setup(item):
    if "incremental" in item.keywords:
        previousfailed = getattr(item.parent, "_previousfailed", None)
        if previousfailed is not None:
            pytest.xfail("previous test failed ({})".format(previousfailed.name))



andreabisello avatar Apr 01 '20 07:04 andreabisello

i use this one

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if not os.path.exists('reports'):
        os.makedirs('reports')
    config.option.htmlpath = 'reports/'+datetime.now().strftime("%d-%m-%Y %H-%M-%S")+".html"

that create a report folder and create a new report for every launch

Hi Thank you for the code snippet. I am new to python. I want to use this piece of code to generate my reports, but finding difficulty. I want to replace "VCMS_TestAutomationReport.html" this hard code file name.Please help meto to run this from command line using - pytest tests -v -s --html=reports/VCMS_TestAutomationReport.html --self-contained-html --capture=tee-sys

Pallavi-Sharma17 avatar Mar 10 '21 09:03 Pallavi-Sharma17

i use this one

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if not os.path.exists('reports'):
        os.makedirs('reports')
    config.option.htmlpath = 'reports/'+datetime.now().strftime("%d-%m-%Y %H-%M-%S")+".html"

that create a report folder and create a new report for every launch

Hi Thank you for the code snippet. I am new to python. I want to use this piece of code to generate my reports, but finding difficulty. I want to replace "VCMS_TestAutomationReport.html" this hard code file name.Please help meto to run this from command line using - pytest tests -v -s --html=reports/VCMS_TestAutomationReport.html --self-contained-html --capture=tee-sys

You should create a pytest.ini file in the root of your project execution and include all your flags like this:

[pytest]
addopts = --verbose --html=<report name> --self-contained-html

This way you can execute with a simple pytest <filename.py> command. Much cleaner

sethorpe avatar Jun 02 '21 21:06 sethorpe