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

Implement Trio mode #49

Merged
merged 1 commit into from
Jul 25, 2018
Merged
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
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
44 changes: 44 additions & 0 deletions pytest_trio/_tests/test_trio_mode.py
Original file line number Diff line number Diff line change
@@ -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)
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