numirias / pytest-json-report

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

Attribute error '_json_report' #37

Closed Raglar closed 5 years ago

Raglar commented 5 years ago

Hello! Following the documentation I have declared in my conftest file:

def pytest_sessionfinish(session):
    report = session.config._json_report
    print(report['exitcode'])

but is throwing me this error:

AttributeError: 'Config' object has no attribute '_json_report'

My end-goal here is to raise an Exception after the json report has been created if any of the tests failed

numirias commented 5 years ago

Thanks for reporting.

Did you run pytest with --json-report? If you run pytest without that flag, it doesn't actually load the plugin and _json_report will remain undefined.

Also, unrelated, I realized the readme example has a small bug. The line should read:

report = session.config._json_report.report

instead of

report = session.config._json_report

If that doesn't work for you, please let me know which parameters you are running pytest with.

Raglar commented 5 years ago

Thanks for the response. This is the way I'm running pytest and the json report plugin (I'm running it like this because I can run the test file as a simple python script instead of calling pytest or any other command to run them):

    plugin = JSONReport()

    pytest.main(['-s', 
                 '-v', 
                 '--json-report-summary',
                 'test_gold.py', 
                 '--jsons_dir', jsons_dir, 
                 '--output_dir', output_dir,
                 ], plugins=[plugin]
    )
    reportPath=os.path.join(output_dir, 'ut_report.json')
    plugin.save_report(reportPath)

Your plugin works like a charm and this code creates the report summary json but I can't access it using pytest_sessionfinish(session)

numirias commented 5 years ago

Thanks for your help looking into this. I fixed the bug and made a new release. Let me know if you still get issues.

It was related to some quirks that occur when running pytest directly from code. Generally, I (and the pytest authors) advise against evoking pytest via pytest.main() because there are a few things that can go wrong e.g. when plugins are imported more than once or the state doesn't get reset when you run main() twice. If you still want to run it from your own Python script, you may want to use subprocesses instead.