Skip to content

Commit

Permalink
Merge PR #46
Browse files Browse the repository at this point in the history
This change makes pytest-forked plugin pytest 6 ready.
It replaces obsolete `tryfirst` mark, `pytest_runtest_logstart` and
`pytest_runtest_logfinish` callbacks. The main fix is update of the
`atexit` hack in our tests.

Co-Authored-By: Florian Bruhin <[email protected]>
Co-Authored-By: Ran Benita <[email protected]>
  • Loading branch information
3 people committed Jul 27, 2020
2 parents b29c386 + 223bcf6 commit 1477356
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ env:
- TOXENV=py-pytest310
- TOXENV=py-pytest46
- TOXENV=py-pytest54
- TOXENV=py-pytest60
- TOXENV=py-pytestlatest
- TOXENV=py-pytestmaster
matrix:
exclude:
- python: '2.7' # pytest 5+ does not support Python 2
env: TOXENV=py-pytest54
- python: '2.7' # pytest 5+ does not support Python 2
env: TOXENV=py-pytest60
- python: '2.7' # pytest 5+ does not support Python 2
env: TOXENV=py-pytestmaster
- python: '2.7' # Same as pytest54 for Python 2
env: TOXENV=py-pytestlatest
include:
- python: '3.8'
env: TOXENV=flakes
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v1.3.0
======

* Add support for pytest 6 (issue #45 / PR #46)
* Replace `@pytest.mark.tryfirst` with newer `@pytest.hookimpl` (PR #46)
* Invoke `pytest_runtest_logstart` and `pytest_runtest_logfinish` hooks in `runtest_protocol` (issue #31 / PR #46)

v1.2.0
======

Expand Down
9 changes: 6 additions & 3 deletions src/pytest_forked/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ def pytest_load_initial_conftests(early_config, parser, args):
)


@pytest.mark.tryfirst
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_protocol(item):
if item.config.getvalue("forked") or item.get_closest_marker("forked"):
ihook = item.ihook
ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)
reports = forked_run_report(item)
for rep in reports:
item.ihook.pytest_runtest_logreport(report=rep)
ihook.pytest_runtest_logreport(report=rep)
ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
return True


Expand Down Expand Up @@ -73,7 +76,7 @@ def runforked():


def report_process_crash(item, result):
from _pytest._code.source import getfslineno
from _pytest._code import getfslineno
path, lineno = getfslineno(item)
info = ("%s:%s: running the test CRASHED with signal %d" %
(path, lineno, result.signal))
Expand Down
5 changes: 4 additions & 1 deletion testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def _divert_atexit(request, monkeypatch):
import atexit
atexit_fns = []

def atexit_register(func, *args, **kwargs):
atexit_fns.append(lambda: func(*args, **kwargs))

def finish():
while atexit_fns:
atexit_fns.pop()()

monkeypatch.setattr(atexit, "register", atexit_fns.append)
monkeypatch.setattr(atexit, "register", atexit_register)
request.addfinalizer(finish)
11 changes: 5 additions & 6 deletions testing/test_xfail_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_xfail(is_crashing, is_strict, testdir):
session_start_title = '*==== test session starts ====*'
loaded_pytest_plugins = 'plugins: forked*'
collected_tests_num = 'collected 1 item'
expected_progress = 'test_xfail.py {expected_letter!s}'.format(**locals())
expected_progress = 'test_xfail.py {expected_letter!s}*'.format(**locals())
failures_title = '*==== FAILURES ====*'
failures_test_name = '*____ test_function ____*'
failures_test_reason = '[XPASS(strict)] The process gets terminated'
Expand Down Expand Up @@ -112,6 +112,9 @@ def test_xfail(is_crashing, is_strict, testdir):
import pytest
# The current implementation emits RuntimeWarning.
pytestmark = pytest.mark.filterwarnings('ignore:pytest-forked xfail')
@pytest.mark.xfail(
reason='The process gets terminated',
strict={is_strict!s},
Expand All @@ -123,9 +126,5 @@ def test_function():
format(**locals())
)

pytest_run_result = testdir.runpytest(
test_module,
'-ra',
'-p', 'no:warnings', # the current implementation emits RuntimeWarning
)
pytest_run_result = testdir.runpytest(test_module, '-ra')
pytest_run_result.stdout.fnmatch_lines(expected_lines)
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ deps =
pytest310: pytest~=3.10
pytest46: pytest~=4.6
pytest54: pytest~=5.4
pytest60: pytest~=6.0rc1
pytestlatest: pytest
pytestmaster: git+https://github.com/pytest-dev/pytest.git@master
platform=linux|darwin
commands=
pytest {posargs}
Expand Down

0 comments on commit 1477356

Please sign in to comment.