From 910d79c62194260bc9ec9f05e060806e5748f25f Mon Sep 17 00:00:00 2001 From: "Nathaniel J. Smith" Date: Sun, 22 Jul 2018 23:48:42 -0700 Subject: [PATCH] Implement Trio mode Fixes gh-9 Documentation in gh-47 --- .../_tests/test_hypothesis_interaction.py | 7 +-- pytest_trio/_tests/test_trio_mode.py | 44 +++++++++++++++++++ pytest_trio/enable_trio_mode.py | 7 +++ pytest_trio/plugin.py | 29 ++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 pytest_trio/_tests/test_trio_mode.py create mode 100644 pytest_trio/enable_trio_mode.py diff --git a/pytest_trio/_tests/test_hypothesis_interaction.py b/pytest_trio/_tests/test_hypothesis_interaction.py index 2cf969f..26fe612 100644 --- a/pytest_trio/_tests/test_hypothesis_interaction.py +++ b/pytest_trio/_tests/test_hypothesis_interaction.py @@ -1,9 +1,10 @@ import pytest from hypothesis import given, settings, strategies as st -# To avoid unpredictable warnings/errors when CI happens to be slow -# Example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296 -our_settings = settings(deadline=None) +# deadline=None avoids unpredictable warnings/errors when CI happens to be +# slow (example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296) +# max_examples=5 speeds things up a bit +our_settings = settings(deadline=None, max_examples=5) @our_settings diff --git a/pytest_trio/_tests/test_trio_mode.py b/pytest_trio/_tests/test_trio_mode.py new file mode 100644 index 0000000..764f5da --- /dev/null +++ b/pytest_trio/_tests/test_trio_mode.py @@ -0,0 +1,44 @@ +import pytest + +test_text = """ +import pytest +import trio +from hypothesis import given, settings, strategies + +async def test_pass(): + await trio.sleep(0) + +async def test_fail(): + await trio.sleep(0) + assert False + +@settings(deadline=None, max_examples=5) +@given(strategies.binary()) +async def test_hypothesis_pass(b): + await trio.sleep(0) + assert isinstance(b, bytes) + +@settings(deadline=None, max_examples=5) +@given(strategies.binary()) +async def test_hypothesis_fail(b): + await trio.sleep(0) + assert isinstance(b, int) +""" + + +def test_trio_mode_pytest_ini(testdir): + testdir.makepyfile(test_text) + + testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\n") + + result = testdir.runpytest() + result.assert_outcomes(passed=2, failed=2) + + +def test_trio_mode_conftest(testdir): + testdir.makepyfile(test_text) + + testdir.makeconftest("from pytest_trio.enable_trio_mode import *") + + result = testdir.runpytest() + result.assert_outcomes(passed=2, failed=2) diff --git a/pytest_trio/enable_trio_mode.py b/pytest_trio/enable_trio_mode.py new file mode 100644 index 0000000..6f05933 --- /dev/null +++ b/pytest_trio/enable_trio_mode.py @@ -0,0 +1,7 @@ +__all__ = ["pytest_collection_modifyitems"] + +from .plugin import automark + + +def pytest_collection_modifyitems(items): + automark(items) diff --git a/pytest_trio/plugin.py b/pytest_trio/plugin.py index 68632c7..59d79e0 100644 --- a/pytest_trio/plugin.py +++ b/pytest_trio/plugin.py @@ -20,6 +20,15 @@ ORDERED_DICTS = False +def pytest_addoption(parser): + parser.addini( + "trio_mode", + "should pytest-trio handle all async functions?", + type="bool", + default=False, + ) + + def pytest_configure(config): # So that it shows up in 'pytest --markers' output: config.addinivalue_line( @@ -264,6 +273,26 @@ def pytest_fixture_setup(fixturedef, request): return _install_async_fixture_if_needed(fixturedef, request) +################################################################ +# Trio mode +################################################################ + + +def automark(items): + for item in items: + if hasattr(item.obj, "hypothesis"): + test_func = item.obj.hypothesis.inner_test + else: + test_func = item.obj + if iscoroutinefunction(test_func): + item.add_marker(pytest.mark.trio) + + +def pytest_collection_modifyitems(config, items): + if config.getini("trio_mode"): + automark(items) + + ################################################################ # Built-in fixtures ################################################################