Skip to content
7 changes: 2 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Documentation build configuration file, created by
# sphinx-quickstart on Sat Jan 21 19:11:14 2017.
#
Expand Down Expand Up @@ -101,7 +98,7 @@ def setup(app):
# built documents.
#
# The short X.Y version.
import pytest_trio # noqa: E402
import pytest_trio

version = pytest_trio.__version__
# The full version, including alpha/beta/rc tags.
Expand Down Expand Up @@ -143,7 +140,7 @@ def setup(app):
# We have to set this ourselves, not only because it's useful for local
# testing, but also because if we don't then RTD will throw away our
# html_theme_options.
import sphinx_rtd_theme # noqa: E402
import sphinx_rtd_theme

html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
Expand Down
177 changes: 177 additions & 0 deletions pytest_trio/_tests/test_autouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""Test to reproduce the autouse async fixture bug."""
import pytest


def test_autouse_async_fixture_with_trio_mark(testdir):
"""Test that autouse=True async fixture runs with @pytest.mark.trio."""
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []

@pytest.fixture(autouse=True)
async def autouse_fixture():
await trio.sleep(0)
autouse_ran.append(True)

@pytest.mark.trio
async def test_with_autouse():
assert autouse_ran == [True]
"""
)

result = testdir.runpytest("-v", "-s")
print("STDOUT:", result.stdout.str())
print("STDERR:", result.stderr.str())
result.assert_outcomes(passed=1)


def test_autouse_async_fixture_in_trio_mode(testdir):
"""Test that autouse=True async fixture runs in trio_mode."""
testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\n")
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []

@pytest.fixture(autouse=True)
async def autouse_fixture():
await trio.sleep(0)
autouse_ran.append(True)

async def test_with_autouse():
assert autouse_ran == [True]
"""
)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_multiple_autouse_async_fixtures(testdir):
"""Test that multiple autouse=True async fixtures all run."""
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []

@pytest.fixture(autouse=True)
async def autouse_fixture1():
await trio.sleep(0)
autouse_ran.append("fixture1")

@pytest.fixture(autouse=True)
async def autouse_fixture2():
await trio.sleep(0)
autouse_ran.append("fixture2")

@pytest.mark.trio
async def test_with_autouse():
assert set(autouse_ran) == {"fixture1", "fixture2"}
"""
)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_autouse_async_fixture_with_dependency(testdir):
"""Test that autouse async fixture can depend on other fixtures."""
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []
regular_ran = []

@pytest.fixture
async def regular_fixture():
await trio.sleep(0)
regular_ran.append("regular")
return "regular_value"

@pytest.fixture(autouse=True)
async def autouse_fixture(regular_fixture):
await trio.sleep(0)
autouse_ran.append(regular_fixture)

@pytest.mark.trio
async def test_with_autouse():
assert regular_ran == ["regular"]
assert autouse_ran == ["regular_value"]
"""
)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_autouse_async_yield_fixture(testdir):
"""Test that autouse=True async yield fixture runs setup and teardown."""
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []

@pytest.fixture(autouse=True)
async def autouse_fixture():
await trio.sleep(0)
autouse_ran.append("setup")
yield
autouse_ran.append("teardown")

@pytest.mark.trio
async def test_with_autouse():
assert autouse_ran == ["setup"]
"""
)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_autouse_async_fixture_with_regular_test_dependency(testdir):
"""Test that autouse async fixture works when test also has regular fixture deps."""
testdir.makepyfile(
"""
import pytest
import trio

autouse_ran = []
regular_ran = []

@pytest.fixture
def regular_fixture():
regular_ran.append("regular")
return "regular_value"

@pytest.fixture(autouse=True)
async def autouse_fixture():
await trio.sleep(0)
autouse_ran.append("autouse")

@pytest.mark.trio
async def test_with_both(regular_fixture):
assert regular_ran == ["regular"]
assert autouse_ran == ["autouse"]
assert regular_fixture == "regular_value"
"""
)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


if __name__ == "__main__":
# Run with pytest directly for debugging
import sys
sys.exit(pytest.main([__file__, "-v"]))
5 changes: 3 additions & 2 deletions pytest_trio/_tests/test_contextvars.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import contextvars

import pytest
from pytest_trio import trio_fixture

import contextvars
from pytest_trio import trio_fixture

cv = contextvars.ContextVar("cv", default=None)

Expand Down
1 change: 1 addition & 0 deletions pytest_trio/_tests/test_fixture_mistakes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest # noqa: F401

from pytest_trio import trio_fixture # noqa: F401

from .helpers import enable_trio_mode
Expand Down
3 changes: 2 additions & 1 deletion pytest_trio/_tests/test_fixture_names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from pytest_trio import trio_fixture
import trio

from pytest_trio import trio_fixture


@trio_fixture
def fixture_with_unique_name(nursery):
Expand Down
3 changes: 2 additions & 1 deletion pytest_trio/_tests/test_hypothesis_interaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import trio
from hypothesis import given, settings, strategies as st
from hypothesis import given, settings
from hypothesis import strategies as st

from pytest_trio.plugin import _trio_test_runner_factory

Expand Down
Loading
Loading