Running Pytest report generation only without running test procedures

64 views Asked by At

I develop a testing framework that uses Pytest for running tests. I have test procedures and reporter function separated. The test cases look like this: def test_testcase001(). Every test case has it own reporter function that looks like this: def reporter_testcase001(meas_file).

I want to implement a functionality that is able to regenerate html report using the pytest-html library by only specifying the measurement folder and running only the reporter_ functions but not test test_ functions.

@pytest.fixture(autouse=True, scope='function')
def _intermediate_setup_teardown(request, overall_setup_teardown, load_parameter_set):
    
    request.config.test_params = None
    
    try: request.config.test_params = request.node.callspec.params 
    except: pass
    
    testname = request.function.__name__ if request.config.test_params is None else f'{request.function.__name__}_{"_".join(map(str, request.config.test_params.values()))}'
    testdir = os.path.join(request.config.result_folder, request.node.cls.__name__, testname )
    os.mkdir(testdir)
    
    request.config.measurement_folder = testdir
    setup_test = request.node.cls.setup_test
    if params:=request.config.model_params: setup_test(params)
    else: setup_test() 
    
    yield
    
    meas_file = os.path.join(testdir, 'meas.mat')
    teardown_test = request.node.cls.teardown_test
    teardown_test(meas_file)
    marker = request.node.get_closest_marker('report')
    
    if marker:
        report_name = marker.args[0]
        reporter = getattr(request.node.cls, f'report_{report_name}')
        reporter(meas_file) if request.config.test_params is None else reporter(meas_file, request)

And I have this hook to add plots to the html reports:

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'teardown':
        
        for dirpath, dnames, files in os.walk(item._request.config.measurement_folder):
            for f in files:
                if f.endswith('.png'):
                    plot64 = encode_image_as_base64(os.path.join(dirpath, f))
                    png_html = f'<img src="data:image/png;base64,{plot64}" width="500" height="500" alt="My Image">'
                    extra.append(html.extras.html(png_html))
        report.extra = extra

I struggle to make this work. I tried by marking the reporters and using the -m option to specify only the reporters and added an option called --report path/to/measurement/folder, so that conftest.py knows when only a report generation is needed.

How can I call pytest in a way that I do not run the tests but only their reporters?

0

There are 0 answers