Skip to content

Commit 410de4c

Browse files
committed
be a package
1 parent 6618f46 commit 410de4c

File tree

7 files changed

+107
-65
lines changed

7 files changed

+107
-65
lines changed

pytest_twisted/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pytest_twisted.core import (
2+
async_fixture,
3+
async_yield_fixture,
4+
blockon,
5+
ensureDeferred,
6+
init_default_reactor,
7+
inlineCallbacks,
8+
)

pytest_twisted.py renamed to pytest_twisted/core.py

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
from twisted.internet.threads import blockingCallFromThread
1515
from twisted.python import failure
1616

17+
if sys.version_info[0] == 3:
18+
from pytest_twisted.three import _async_pytest_fixture_setup, _async_pytest_pyfunc_call
19+
elif sys.version_info[0] == 2:
20+
from pytest_twisted.two import _async_pytest_pyfunc_call
21+
1722

1823
class WrongReactorAlreadyInstalledError(Exception):
1924
pass
@@ -244,6 +249,7 @@ def _set_mark(o, mark):
244249

245250

246251
def _marked_async_fixture(mark):
252+
print(pytest)
247253
@functools.wraps(pytest.fixture)
248254
@_optional_arguments()
249255
def fixture(f, *args, **kwargs):
@@ -308,39 +314,6 @@ def finalizer():
308314
return finalizer
309315

310316

311-
@defer.inlineCallbacks
312-
def _async_pytest_fixture_setup(fixturedef, request, mark):
313-
"""Setup an async or async yield fixture."""
314-
fixture_function = fixturedef.func
315-
316-
kwargs = {
317-
name: request.getfixturevalue(name)
318-
for name in fixturedef.argnames
319-
}
320-
321-
if mark == 'async_fixture':
322-
arg_value = yield defer.ensureDeferred(
323-
fixture_function(**kwargs)
324-
)
325-
elif mark == 'async_yield_fixture':
326-
coroutine = fixture_function(**kwargs)
327-
328-
request.addfinalizer(
329-
_create_async_yield_fixture_finalizer(coroutine=coroutine),
330-
)
331-
332-
arg_value = yield defer.ensureDeferred(coroutine.__anext__())
333-
else:
334-
raise UnrecognizedCoroutineMarkError.from_mark(mark=mark)
335-
336-
fixturedef.cached_result = (arg_value, fixturedef.cache_key(request), None)
337-
338-
if sys.version_info >= (3,):
339-
return arg_value
340-
else:
341-
defer.returnValue(arg_value)
342-
343-
344317
@defer.inlineCallbacks
345318
def _tear_it_down(deferred):
346319
"""Tear down a specific async yield fixture."""
@@ -405,28 +378,6 @@ def inner_test(**kwargs):
405378
return result
406379

407380

408-
@defer.inlineCallbacks
409-
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
410-
"""Run test function."""
411-
fixture_kwargs = {
412-
name: value
413-
for name, value in pyfuncitem.funcargs.items()
414-
if name in pyfuncitem._fixtureinfo.argnames
415-
}
416-
kwargs.update(fixture_kwargs)
417-
418-
maybe_mark = _get_mark(f)
419-
if maybe_mark == 'async_test':
420-
result = yield defer.ensureDeferred(f(**kwargs))
421-
elif maybe_mark == 'inline_callbacks_test':
422-
result = yield f(**kwargs)
423-
else:
424-
# TODO: maybe deprecate this
425-
result = yield f(**kwargs)
426-
427-
return result
428-
429-
430381
@pytest.fixture(scope="session", autouse=True)
431382
def twisted_greenlet():
432383
"""Provide the twisted greenlet in fixture form."""

pytest_twisted/three.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from twisted.internet import defer
2+
3+
4+
@defer.inlineCallbacks
5+
def _async_pytest_fixture_setup(fixturedef, request, mark):
6+
"""Setup an async or async yield fixture."""
7+
from pytest_twisted.core import _create_async_yield_fixture_finalizer
8+
9+
fixture_function = fixturedef.func
10+
11+
kwargs = {
12+
name: request.getfixturevalue(name)
13+
for name in fixturedef.argnames
14+
}
15+
16+
if mark == 'async_fixture':
17+
arg_value = yield defer.ensureDeferred(
18+
fixture_function(**kwargs)
19+
)
20+
elif mark == 'async_yield_fixture':
21+
coroutine = fixture_function(**kwargs)
22+
23+
request.addfinalizer(
24+
_create_async_yield_fixture_finalizer(coroutine=coroutine),
25+
)
26+
27+
arg_value = yield defer.ensureDeferred(coroutine.__anext__())
28+
else:
29+
raise UnrecognizedCoroutineMarkError.from_mark(mark=mark)
30+
31+
fixturedef.cached_result = (arg_value, fixturedef.cache_key(request), None)
32+
33+
return arg_value
34+
35+
36+
@defer.inlineCallbacks
37+
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
38+
"""Run test function."""
39+
from pytest_twisted.core import _get_mark
40+
41+
fixture_kwargs = {
42+
name: value
43+
for name, value in pyfuncitem.funcargs.items()
44+
if name in pyfuncitem._fixtureinfo.argnames
45+
}
46+
kwargs.update(fixture_kwargs)
47+
48+
maybe_mark = _get_mark(f)
49+
if maybe_mark == 'async_test':
50+
result = yield defer.ensureDeferred(f(**kwargs))
51+
elif maybe_mark == 'inline_callbacks_test':
52+
result = yield f(**kwargs)
53+
else:
54+
# TODO: maybe deprecate this
55+
result = yield f(**kwargs)
56+
57+
return result

pytest_twisted/two.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from twisted.internet import defer
2+
3+
4+
@defer.inlineCallbacks
5+
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
6+
"""Run test function."""
7+
from pytest_twisted.core import _get_mark
8+
9+
fixture_kwargs = {
10+
name: value
11+
for name, value in pyfuncitem.funcargs.items()
12+
if name in pyfuncitem._fixtureinfo.argnames
13+
}
14+
kwargs.update(fixture_kwargs)
15+
16+
maybe_mark = _get_mark(f)
17+
if maybe_mark == 'async_test':
18+
result = yield defer.ensureDeferred(f(**kwargs))
19+
elif maybe_mark == 'inline_callbacks_test':
20+
result = yield f(**kwargs)
21+
else:
22+
# TODO: maybe deprecate this
23+
result = yield f(**kwargs)
24+
25+
return result

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
"Programming Language :: Python :: Implementation :: CPython",
4444
"Programming Language :: Python :: Implementation :: PyPy",
4545
],
46-
entry_points={"pytest11": ["twisted = pytest_twisted"]},
46+
entry_points={"pytest11": ["twisted = pytest_twisted.core"]},
4747
)

testing/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
2-
import pytest_twisted
2+
import pytest_twisted.core
33

44

55
pytest_plugins = "pytester"
66

77

88
@pytest.hookimpl(tryfirst=True)
99
def pytest_configure(config):
10-
pytest_twisted._use_asyncio_selector_if_required(config=config)
10+
pytest_twisted.core._use_asyncio_selector_if_required(config=config)

testing/test_basic.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ def format_run_result_output_for_assert(run_result):
6262
def _default_conftest(testdir):
6363
testdir.makeconftest(textwrap.dedent("""
6464
import pytest
65-
import pytest_twisted
65+
import pytest_twisted.core
6666
6767
6868
@pytest.hookimpl(tryfirst=True)
6969
def pytest_configure(config):
70-
pytest_twisted._use_asyncio_selector_if_required(config=config)
70+
pytest_twisted.core._use_asyncio_selector_if_required(config=config)
7171
"""))
7272

7373

@@ -954,11 +954,12 @@ def test_blockon_in_hook_with_asyncio(testdir, cmd_opts, request):
954954
conftest_file = """
955955
import pytest
956956
import pytest_twisted
957+
import pytest_twisted.core
957958
from twisted.internet import defer
958959
959960
@pytest.hookimpl(tryfirst=True)
960961
def pytest_configure(config):
961-
pytest_twisted._use_asyncio_selector_if_required(config=config)
962+
pytest_twisted.core._use_asyncio_selector_if_required(config=config)
962963
963964
pytest_twisted.init_asyncio_reactor()
964965
d = defer.Deferred()
@@ -986,12 +987,12 @@ def test_wrong_reactor_with_asyncio(testdir, cmd_opts, request):
986987
skip_if_reactor_not(request, "asyncio")
987988
conftest_file = """
988989
import pytest
989-
import pytest_twisted
990+
import pytest_twisted.core
990991
991992
992993
@pytest.hookimpl(tryfirst=True)
993994
def pytest_configure(config):
994-
pytest_twisted._use_asyncio_selector_if_required(config=config)
995+
pytest_twisted.core._use_asyncio_selector_if_required(config=config)
995996
996997
def pytest_addhooks():
997998
import twisted.internet.default
@@ -1142,12 +1143,12 @@ async def test_self_isinstance(self, foo):
11421143
def test_import_pytest_twisted_in_conftest_py_not_a_problem(testdir, cmd_opts):
11431144
conftest_file = """
11441145
import pytest
1145-
import pytest_twisted
1146+
import pytest_twisted.core
11461147
11471148
11481149
@pytest.hookimpl(tryfirst=True)
11491150
def pytest_configure(config):
1150-
pytest_twisted._use_asyncio_selector_if_required(config=config)
1151+
pytest_twisted.core._use_asyncio_selector_if_required(config=config)
11511152
"""
11521153
testdir.makeconftest(conftest_file)
11531154
test_file = """

0 commit comments

Comments
 (0)