Skip to content

Commit

Permalink
Merge pull request pytest-dev#8 from nip3o/pytest28
Browse files Browse the repository at this point in the history
pytest 2.8 compatibility
  • Loading branch information
nip3o committed Sep 26, 2015
2 parents 19e1204 + 3acc269 commit ca49932
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Please submit an issue if you have any suggestions regarding use cases
of pytest-stepwise.


Compatibility
=============

pytest-stepwise is compatible with pytest 2.2 -> 2.8.
For pytest 2.7 and earlier, ``pytest-cache`` is required as a dependency.


Changelog
=========

Expand All @@ -48,3 +55,5 @@ Changelog
* 0.3 - Fixed issue when failing tests are removed.
Fixed compatibility with ``--pdb`` option.
Stop on errors as well as on test failures.
* 0.4 - Refactoring, pytest 2.8 compatiblity. Stop test execution on
collection errors.
1 change: 1 addition & 0 deletions pytest_stepwise/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.4'
12 changes: 12 additions & 0 deletions pytest_stepwise/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

try:
from _pytest.cacheprovider import Cache
except ImportError:
from pytest_cache import Cache


if hasattr(pytest, 'hookimpl'):
tryfirst = pytest.hookimpl(tryfirst=True)
else:
tryfirst = pytest.mark.tryfirst
26 changes: 15 additions & 11 deletions pytest_stepwise.py → pytest_stepwise/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import pytest
from pytest_cache import Cache

__version__ = '0.3'
from .compat import Cache, tryfirst


def pytest_addoption(parser):
Expand All @@ -14,7 +11,7 @@ def pytest_addoption(parser):
help='ignore the first failing test but stop on the next failing test')


@pytest.mark.tryfirst
@tryfirst
def pytest_configure(config):
config.cache = Cache(config)
config.pluginmanager.register(StepwisePlugin(config), 'stepwiseplugin')
Expand All @@ -27,7 +24,7 @@ def __init__(self, config):
self.session = None

if self.active:
self.lastfailed = config.cache.get('cache/stepwise', set())
self.lastfailed = config.cache.get('cache/stepwise', None)
self.skip = config.getvalue('skip')

def pytest_sessionstart(self, session):
Expand All @@ -42,7 +39,7 @@ def pytest_collection_modifyitems(self, session, config, items):

# Make a list of all tests that has been runned before the last failing one.
for item in items:
if item.nodeid in self.lastfailed:
if item.nodeid == self.lastfailed:
found = True
break
else:
Expand All @@ -58,6 +55,10 @@ def pytest_collection_modifyitems(self, session, config, items):

config.hook.pytest_deselected(items=already_passed)

def pytest_collectreport(self, report):
if self.active and report.failed:
self.session.shouldstop = 'Error when collecting test, stopping test execution.'

def pytest_runtest_logreport(self, report):
# Skip this hook if plugin is not active or the test is xfailed.
if not self.active or 'xfail' in report.keywords:
Expand All @@ -67,22 +68,25 @@ def pytest_runtest_logreport(self, report):
if self.skip:
# Remove test from the failed ones (if it exists) and unset the skip option
# to make sure the following tests will not be skipped.
self.lastfailed.discard(report.nodeid)
if report.nodeid == self.lastfailed:
self.lastfailed = None

self.skip = False
else:
# Mark test as the last failing and interrupt the test session.
self.lastfailed.add(report.nodeid)
self.lastfailed = report.nodeid
self.session.shouldstop = 'Test failed, continuing from this test next run.'

else:
# If the test was actually run and did pass.
if report.when == 'call':
# Remove test from the failed ones, if exists.
self.lastfailed.discard(report.nodeid)
if report.nodeid == self.lastfailed:
self.lastfailed = None

def pytest_sessionfinish(self, session):
if self.active:
self.config.cache.set('cache/stepwise', self.lastfailed)
else:
# Clear the list of failing tests if the plugin is not active.
self.config.cache.set('cache/stepwise', set())
self.config.cache.set('cache/stepwise', [])
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def read(fname):

setup(
name='pytest-stepwise',
version='0.3',
version=__import__('pytest_stepwise').__version__,
author='Niclas Olofsson',
author_email='n@niclasolofsson.se',
maintainer='Niclas Olofsson',
Expand All @@ -20,13 +20,13 @@ def read(fname):
url='https://github.com/nip3o/pytest-stepwise',
description='Run a test suite one failing test at a time.',
long_description=read('README.rst'),
py_modules=['pytest_stepwise'],
install_requires=['pytest-cache >= 1.0'],
packages=['pytest_stepwise'],
install_requires=['pytest >= 2.2'],
classifiers=['Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Testing',
'Programming Language :: Python',
'Operating System :: OS Independent',
'License :: OSI Approved :: MIT License'],
entry_points={'pytest11': ['stepwise = pytest_stepwise']},
entry_points={'pytest11': ['stepwise = pytest_stepwise.plugin']},
)
File renamed without changes.
26 changes: 19 additions & 7 deletions test_pytest_stepwise.py → tests/test_pytest_stepwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ def test_success_after_fail():
return testdir


@pytest.fixture
def broken_testdir(testdir):
testdir.makepyfile(working_testfile='def test_proper(): assert 1', broken_testfile='foobar')
return testdir


def test_run_without_stepwise(stepwise_testdir):
result = stepwise_testdir.runpytest('-v', '--strict', '--fail')

assert not result.errlines
result.stdout.fnmatch_lines(['*test_success_before_fail PASSED*'])
result.stdout.fnmatch_lines(['*test_fail_on_flag FAILED*'])
result.stdout.fnmatch_lines(['*test_success_after_fail PASSED*'])
Expand All @@ -63,7 +68,7 @@ def test_run_without_stepwise(stepwise_testdir):
def test_fail_and_continue_with_stepwise(stepwise_testdir):
# Run the tests with a failing second test.
result = stepwise_testdir.runpytest('-v', '--strict', '--stepwise', '--fail')
assert not result.errlines
assert not result.stderr.str()

stdout = result.stdout.str()
# Make sure we stop after first failing test.
Expand All @@ -73,7 +78,7 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):

# "Fix" the test that failed in the last run and run it again.
result = stepwise_testdir.runpytest('-v', '--strict', '--stepwise')
assert not result.errlines
assert not result.stderr.str()

stdout = result.stdout.str()
# Make sure the latest failing test runs and then continues.
Expand All @@ -85,7 +90,7 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
def test_run_with_skip_option(stepwise_testdir):
result = stepwise_testdir.runpytest('-v', '--strict', '--stepwise', '--skip',
'--fail', '--fail-last')
assert not result.errlines
assert not result.stderr.str()

stdout = result.stdout.str()
# Make sure first fail is ignore and second fail stops the test run.
Expand All @@ -98,7 +103,7 @@ def test_run_with_skip_option(stepwise_testdir):
def test_fail_on_errors(error_testdir):
result = error_testdir.runpytest('-v', '--strict', '--stepwise')

assert not result.errlines
assert not result.stderr.str()
stdout = result.stdout.str()

assert 'test_error ERROR' in stdout
Expand All @@ -108,7 +113,7 @@ def test_fail_on_errors(error_testdir):
def test_change_testfile(stepwise_testdir):
result = stepwise_testdir.runpytest('-v', '--strict', '--stepwise', '--fail',
'test_stepwise.py')
assert not result.errlines
assert not result.stderr.str()

stdout = result.stdout.str()
assert 'test_fail_on_flag FAILED' in stdout
Expand All @@ -117,7 +122,14 @@ def test_change_testfile(stepwise_testdir):
# test to continue from does not exist in testfile_b.
result = stepwise_testdir.runpytest('-v', '--strict', '--stepwise',
'testfile_b.py')
assert not result.errlines
assert not result.stderr.str()

stdout = result.stdout.str()
assert 'test_success PASSED' in stdout


def test_stop_on_collection_errors(broken_testdir):
result = broken_testdir.runpytest('-v', '--strict', '--stepwise', 'working_testfile.py', 'broken_testfile.py')

stdout = result.stdout.str()
assert 'Error when collecting test' in stdout

0 comments on commit ca49932

Please sign in to comment.