-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add @pytest_trio.trio_fixture for explicitly marking a fixture as being a trio fixture - Make the nursery fixture a @trio_fixture - Refactor Trio fixture classes into one class - Check for trio marker instead of trio keyword (fixes gh-43) - This also raises the minimum pytest version to 3.6 - Raise an error if a Trio fixture is used with a non-function scope (fixes gh-18) - Raise an error if a Trio fixture is used with a non-Trio test I think this also closes gh-10's discussion, though we still need to convince pytest-asyncio to fix their side of things.
- Loading branch information
Showing
4 changed files
with
135 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
"""Top-level package for pytest-trio.""" | ||
|
||
from ._version import __version__ | ||
from .plugin import trio_fixture | ||
|
||
__all__ = ["trio_fixture"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
from pytest_trio import trio_fixture | ||
|
||
|
||
def test_trio_fixture_with_non_trio_test(testdir): | ||
testdir.makepyfile( | ||
""" | ||
import trio | ||
from pytest_trio import trio_fixture | ||
import pytest | ||
@trio_fixture | ||
def trio_time(): | ||
return trio.current_time() | ||
@pytest.fixture | ||
def indirect_trio_time(trio_time): | ||
return trio_time + 1 | ||
@pytest.mark.trio | ||
async def test_async(mock_clock, trio_time, indirect_trio_time): | ||
assert trio_time == 0 | ||
assert indirect_trio_time == 1 | ||
def test_sync(trio_time): | ||
pass | ||
def test_sync_indirect(indirect_trio_time): | ||
pass | ||
""" | ||
) | ||
|
||
result = testdir.runpytest() | ||
|
||
result.assert_outcomes(passed=1, error=2) | ||
result.stdout.fnmatch_lines( | ||
["*Trio fixtures can only be used by Trio tests*"] | ||
) | ||
|
||
|
||
def test_trio_fixture_with_wrong_scope(testdir): | ||
# There's a trick here: when you have a non-function-scope fixture, it's | ||
# not instantiated for any particular function (obviously). So... when our | ||
# pytest_fixture_setup hook tries to check for marks, it can't normally | ||
# see @pytest.mark.trio. So... it's actually almost impossible to have an | ||
# async fixture get treated as a Trio fixture *and* have it be | ||
# non-function-scope. But, class-scoped fixtures can see marks on the | ||
# class, so this is one way (the only way?) it can happen: | ||
testdir.makepyfile( | ||
""" | ||
import pytest | ||
import pytest_trio | ||
@pytest.fixture(scope="class") | ||
async def async_class_fixture(): | ||
pass | ||
@pytest.mark.trio | ||
class TestFoo: | ||
async def test_foo(self, async_class_fixture): | ||
pass | ||
""" | ||
) | ||
|
||
result = testdir.runpytest() | ||
|
||
result.assert_outcomes(error=1) | ||
result.stdout.fnmatch_lines(["*must be function-scope*"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters