Search code examples
pythonpytestpytest-html

Is there a way to know when pytest-html create the actual html file?


I would like to know when the html and asset folder are created, after running a test via pytest. Is there a way to know when the file is actually created? I have tried to use breakpoints in pycharm but I can't get the time when the file is actually created.

The whole point of knowing when the file is created, is because I want to copy it in a zip file.


Solution

  • The HTML report is written in the pytest_sessionfinish hookimpl:

    def pytest_sessionfinish(self, session):
        report_content = self._generate_report(session)
        self._save_report(report_content)
    

    Source.

    If you want to manipulate the report file in your own test run, you can do that by adding your own pytest_sessionfinish hookimpl, e.g.

    import pathlib
    import zipfile
    
    
    def pytest_sessionfinish(session):
        htmlfile = session.config.getoption('htmlpath')
        if htmlfile is None:  # html report not wanted by user
            return
        htmlzip = pathlib.Path(htmlfile).with_suffix('.zip')
        with zipfile.ZipFile(htmlzip, 'w') as zip:
            zip.write(htmlfile)
            zip.write('assets/style.css')