numirias / pytest-json-report

🗒️ A pytest plugin to report test results as JSON
MIT License
147 stars 39 forks source link

Change value to --json-report-file dynamically during test set-up #26

Closed swaingankar closed 5 years ago

swaingankar commented 5 years ago

I need to have the json report loaded to Elasticsearch and thus need to save the JSON with specific test run name, however this is not possible with the current option.

Can you please help

numirias commented 5 years ago

Right now, you can use hooks to access the report after it's finished and just save it yourself.

The plugin offers pytest_json_modifyreport to do something with the final report. E.g., put this in your conftest.py:

import json

def pytest_json_modifyreport(json_report):
    filename = '{}.json'.format(json_report['created'])
    with open(filename, 'w') as f:
        json.dump(json_report, f, default=str)

You can also employ the builtin pytest_sessionfinish hook to access the report object (same idea):

def pytest_sessionfinish(session):
    # Once the session has finished, the report will be available here
    report = session.config._json_report.report
    # Do something with the report...

To prevent the report from being saved twice, you could then run the plugin with --json-report-file /dev/null.

Right now, I'm making major changes to the plugin and this will probably also become easier. So if you can wait a few days, I might have an even more straightforward approach for you.

swaingankar commented 5 years ago

Thanks a lot for your response, will use the available hooks to get my problem resolved.

Eagerly waiting for your next release, closing the issue.

numirias commented 5 years ago

Just to let you know, a new version has been released. (Be aware that there are some changes, such as minor adjustments in the JSON report format.)

I recommend the following to save your report to a custom path:

swaingankar commented 5 years ago

Hello,

Thanks a lot for such a quick turn-around, was away from my work, thus some delay in response.

Will test further and revert in case of any questions.