numirias / pytest-json-report

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

Cannot create report file if target directory doesn't exist #41

Closed shmarlovsky closed 5 years ago

shmarlovsky commented 5 years ago

Python: 3.7.3 Pytest: 4.6.3 pytest-json-report: 1.0.4

Repro: Try to run some tests with parameter --json-report-file and save result to directory which does not exists, e.g "results/report.json".

Traceback (most recent call last): File "C:\Program Files\Python37-32\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "C:\Program Files\Python37-32\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Program Files\Python37-32\lib\site-packages\pytest.py", line 102, in raise SystemExit(pytest.main()) File "C:\Program Files\Python37-32\lib\site-packages_pytest\config__init.py", line 82, in main return config.hook.pytest_cmdline_main(config=config) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\hooks.py", line 289, in call return self._hookexec(self, self.get_hookimpls(), kwargs) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\manager.py", line 87, in _hookexec return self._inner_hookexec(hook, methods, kwargs) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\manager.py", line 81, in firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 208, in _multicall return outcome.get_result() File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 80, in get_result raise ex[1].with_traceback(ex[2]) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 187, in _multicall res = hook_impl.function(*args) File "C:\Program Files\Python37-32\lib\site-packages_pytest\main.py", line 243, in pytest_cmdline_main return wrap_session(config, _main) File "C:\Program Files\Python37-32\lib\site-packages_pytest\main.py", line 236, in wrap_session session=session, exitstatus=session.exitstatus File "C:\Program Files\Python37-32\lib\site-packages\pluggy\hooks.py", line 289, in call__ return self._hookexec(self, self.get_hookimpls(), kwargs) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\manager.py", line 87, in _hookexec return self._inner_hookexec(hook, methods, kwargs) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\manager.py", line 81, in firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 203, in _multicall gen.send(outcome) File "C:\Program Files\Python37-32\lib\site-packages_pytest\terminal.py", line 662, in pytest_sessionfinish outcome.get_result() File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 80, in get_result raise ex[1].with_traceback(ex[2]) File "C:\Program Files\Python37-32\lib\site-packages\pluggy\callers.py", line 187, in _multicall res = hook_impl.function(*args) File "C:\Program Files\Python37-32\lib\site-packages\pytest_jsonreport\plugin.py", line 191, in pytest_sessionfinish self.save_report(path) File "C:\Program Files\Python37-32\lib\site-packages\pytest_jsonreport\plugin.py", line 201, in save_report with open(path, 'w') as f: FileNotFoundError: [Errno 2] No such file or directory: 'c:\jenkins\workspace\ZAWorkflow\TESTS\results\tests-za-09_Win10_19H1_x86-4383.json'

numirias commented 5 years ago

Fixed in #42 and released with v1.0.5. The target directory tree is now created if non-existing. Let me know in case there are issues with the fix.