Skip to content

Commit

Permalink
Implement Trio mode
Browse files Browse the repository at this point in the history
Fixes gh-9
Documentation in gh-47
  • Loading branch information
njsmith committed Jul 23, 2018
1 parent a972dc6 commit a1cdac7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pytest_trio/_tests/test_hypothesis_interaction.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 45 additions & 0 deletions pytest_trio/_tests/test_trio_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
7 changes: 7 additions & 0 deletions pytest_trio/enable_trio_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__all__ = ["pytest_collection_modifyitems"]

from .plugin import automark


def pytest_collection_modifyitems(items):
automark(items)
29 changes: 29 additions & 0 deletions pytest_trio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
################################################################
Expand Down

0 comments on commit a1cdac7

Please sign in to comment.