Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving only failed tests #15

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pytest-session2file
======================

pytest-session2file is a py.test plugin that save failure or test session information to a file pass that can be
pytest-session2file is a py.test plugin that save failures or test session information to a file pass that can be
invoked as at command line when launching py.test run. It put in a file exactly what pytest return to stdout.

**Installation**
Expand All @@ -19,13 +19,15 @@ Once the plugin is installed edit your `conftest.py` and insert in the top of th

pytest_plugins = 'pytest_session2file'

Then you can launch your test with the new option `--session2file=` like this :
Then you can launch your test with one of the two options as follows:

py.test --session2file=FILENAME
py.test --session2file=FILENAME --failures-only

If you don't want to edit your `conftest.py` you can invoque py.test like this :
If you don't want to edit your `conftest.py` you can invoke py.test like this:

py.test -p pytest_session2file --session2file=FILENAME
py.test -p pytest_session2file --session2file=FILENAME --failures-only

At the end of the test execution you should obtain a text file with the content of stdout of py.test under the filename
provided that look like this :
Expand All @@ -48,11 +50,10 @@ provided that look like this :

**Platforms:** All

**Version:** 0.1.11
**Version:** 0.1.12

**Date:** 2021-01-26 13:08:31

**License:** LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

Copyright (C) 2015 Richard Vézina <[email protected]>

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
pytest-session2file
======================

pytest-session2file is a py.test plugin that save failure or test session information to a file pass that can be
pytest-session2file is a py.test plugin that save failures or test session information to a file pass that can be
invoked as at command line when launching py.test run. It put in a file exactly what pytest return to stdout.

**Installation**


Install it with pip as follows :

pip install pytest-session2file
Expand All @@ -19,13 +18,15 @@ Once the plugin is installed edit your `conftest.py` and insert in the top of th

pytest_plugins = 'pytest_session2file'

Then you can launch your test with the new option `--session2file=` like this :
Then you can launch your test with one of the two options as follows:

py.test --session2file=FILENAME
py.test --session2file=FILENAME --failures-only

If you don't want to edit your `conftest.py` you can invoke py.test like this :
If you don't want to edit your `conftest.py` you can invoke py.test like this:

py.test -p pytest_session2file --session2file=FILENAME
py.test -p pytest_session2file --session2file=FILENAME --failures-only

At the end of the test execution you should obtain a text file with the content of stdout of py.test under the filename
provided that look like this :
Expand All @@ -48,11 +49,10 @@ provided that look like this :

**Platforms:** All

**Version:** 0.1.11
**Version:** 0.1.12

**Date:** 2021-01-26 13:08:31

**License:** LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

Copyright (C) 2015 Richard Vézina <[email protected]>

42 changes: 26 additions & 16 deletions pytest_session2file/pytest_session2file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
# -*- coding: utf-8 -*-

"""
Pytest Plugin that save failure or test session information to a file pass as a command line argument to pytest.
Pytest Plugin that saves failures or test session information to a file pass as a command line argument to pytest.

It put in a file exactly what pytest return to the stdout.
It put in a file exactly what pytest return to the stdout, depends on the flag provided.

To use it :
Put this file in the root of tests/ edit your conftest and insert in the top of the file :

pytest_plugins = 'pytest_session2file'

Then you can launch your test with the new option --session2file= like this :
Then you can launch your test with one of the two options as follows:

py.test --session2file=FILENAME
Or :
py.test -p pytest_session2file --session2file=FILENAME

py.test --session2file=FILENAME --failures-only

Inspire by _pytest.pastebin
Ref: https://github.com/pytest-dev/pytest/blob/master/_pytest/pastebin.py
Expand All @@ -26,17 +24,22 @@
Copyright (C) 2015 Richard Vézina <ml.richard.vezinar @ gmail.com>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
"""

import pytest
import re
import sys
import tempfile

import pytest


FAILURES_PATTERN = "=+ FAILURES =+"


def pytest_addoption(parser):
group = parser.getgroup("terminal reporting")
group._addoption('--session2file', action='store', metavar='path', default=None,
help="Save to file the pytest session information")
return
group._addoption('--failures-only', action='store_true', default=False,
help="Save to file only failed tests from pytest session")


@pytest.hookimpl(trylast=True)
Expand All @@ -56,7 +59,6 @@ def tee_write(s, **kwargs):
else:
config._pytestsessionfile.write(s.encode('utf8'))
tr._tw.write = tee_write
return


def pytest_unconfigure(config):
Expand All @@ -71,17 +73,25 @@ def pytest_unconfigure(config):
del tr._tw.__dict__['write']
# write summary
create_new_file(config=config, contents=sessionlog)
return


def create_new_file(config, contents):
"""
Creates a new file with pytest session contents.
Creates a new file with pytest session contents, depends on the flag.
:contents: pytest stdout contents
"""
path = config.option.session2file
path=config.option.session2file
only_failures=config.option.failures_only

# keep full session information as default
session2file_content = contents
if path is not None:
with open(path, 'w') as f:
f.writelines(contents)
return

# handle failed tests only
if only_failures:
failure_pattern = re.compile(FAILURES_PATTERN)
m_obj=re.search(failure_pattern, contents)
if m_obj is not None:
session2file_content=contents[m_obj.start():]
# write chosen information
f.writelines(session2file_content)
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

setup(
name="pytest-session2file",
version='0.1.11',
version='0.1.12',
author='Richard Vézina',
author_email='[email protected]',
maintainer='Harel Keres',
maintainer_email='[email protected]',
license='LGPLv3 (http://www.gnu.org/licenses/lgpl.html)',
packages=[],
py_modules=['pytest_session2file.pytest_session2file'],
Expand All @@ -40,5 +42,6 @@
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.10'
],
)