Skip to content

Commit

Permalink
Merge branch 'master' into njsmith-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Dec 15, 2017
2 parents 8588e45 + a30ffb1 commit bfb949e
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 79 deletions.
142 changes: 142 additions & 0 deletions pytest_trio/_tests/test_async_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import pytest


def test_single_async_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
@pytest.fixture
async def fix1():
await trio.sleep(0)
return 'fix1'
@pytest.mark.trio
async def test_simple(fix1):
assert fix1 == 'fix1'
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=1)


def test_async_fixture_recomputed_for_each_test(testdir):

testdir.makepyfile(
"""
import pytest
import trio
counter = 0
@pytest.fixture
async def fix1():
global counter
await trio.sleep(0)
counter += 1
return counter
@pytest.mark.trio
async def test_first(fix1):
assert fix1 == 1
@pytest.mark.trio
async def test_second(fix1):
assert fix1 == 2
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=2)


def test_nested_async_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
@pytest.fixture
async def fix1():
await trio.sleep(0)
return 'fix1'
@pytest.fixture
async def fix2(fix1):
await trio.sleep(0)
return 'fix2(%s)' % fix1
@pytest.mark.trio
async def test_simple(fix2):
assert fix2 == 'fix2(fix1)'
@pytest.mark.trio
async def test_both(fix1, fix2):
assert fix1 == 'fix1'
assert fix2 == 'fix2(fix1)'
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=2)


def test_async_within_sync_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
@pytest.fixture
async def async_fix():
await trio.sleep(0)
return 42
@pytest.fixture
def sync_fix(async_fix):
return async_fix
@pytest.mark.trio
async def test_simple(sync_fix):
assert sync_fix == 42
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=1)


# In pytest, ERROR status occurs when an exception is raised in fixture code.
# The trouble is our async fixtures must be run whithin a trio context, hence
# they are actually run just before the test, providing no way to make the
# difference between an exception comming from the real test or from an
# async fixture...
@pytest.mark.xfail(reason='Not implemented yet')
def test_raise_in_async_fixture_cause_pytest_error(testdir):

testdir.makepyfile(
"""
import pytest
@pytest.fixture
async def fix1():
raise ValueError('Ouch !')
@pytest.mark.trio
async def test_base(fix1):
pass # Crash should have occures before arriving here
"""
)

result = testdir.runpytest()

result.assert_outcomes(error=1)
226 changes: 226 additions & 0 deletions pytest_trio/_tests/test_async_yield_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import sys
import pytest


@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_single_async_yield_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix1():
events.append('fix1 setup')
await trio.sleep(0)
yield 'fix1'
await trio.sleep(0)
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == ['fix1 setup']
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix1 setup',
'fix1 teardown',
]
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=3)


@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_nested_async_yield_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
async def fix1(fix2):
events.append('fix1 setup')
await trio.sleep(0)
yield 'fix1'
await trio.sleep(0)
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
'fix1 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix1 setup',
'fix1 teardown',
'fix2 teardown',
]
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=3)


@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_async_yield_fixture_within_sync_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
def fix1(fix2):
return 'fix1'
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix2 teardown',
]
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=3)


@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_async_yield_fixture_within_sync_yield_fixture(testdir):

testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
def fix1(fix2):
events.append('fix1 setup')
yield 'fix1'
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
'fix1 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix1 setup',
'fix1 teardown',
'fix2 teardown',
]
"""
)

result = testdir.runpytest()

result.assert_outcomes(passed=3)


@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_async_yield_fixture_with_multiple_yields(testdir):

testdir.makepyfile(
"""
import pytest
import trio
@pytest.fixture
async def fix1():
await trio.sleep(0)
yield 'good'
await trio.sleep(0)
yield 'bad'
@pytest.mark.trio
async def test_actual_test(fix1):
pass
"""
)

result = testdir.runpytest()

# TODO: should trigger error instead of failure
# result.assert_outcomes(error=1)
result.assert_outcomes(failed=1)
Loading

0 comments on commit bfb949e

Please sign in to comment.