Skip to content

fix assertion in tests, add new test for blockon usage in fixture #26

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

Merged
merged 3 commits into from
Apr 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
27 changes: 8 additions & 19 deletions pytest_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def in_reactor(d, f, *args):


@pytest.fixture(scope="session", autouse=True)
def twisted_greenlet(request, reactor):
def twisted_greenlet(request):
request.addfinalizer(stop_twisted_greenlet)
return _instances.gr_twisted

Expand All @@ -144,7 +144,7 @@ def init_default_reactor():
)


def init_qt5_reactor(qapp):
def init_qt5_reactor():
import qt5reactor

_install_reactor(
Expand All @@ -153,18 +153,12 @@ def init_qt5_reactor(qapp):
)


_reactor_fixtures = {
_reactor_installers = {
'default': init_default_reactor,
'qt5reactor': init_qt5_reactor,
}


def _init_reactor():
import twisted.internet.reactor
_instances.reactor = twisted.internet.reactor
init_twisted_greenlet()


def _install_reactor(reactor_installer, reactor_type):
try:
reactor_installer()
Expand All @@ -177,24 +171,19 @@ def _install_reactor(reactor_installer, reactor_type):
type(twisted.internet.reactor),
)
)
_init_reactor()
import twisted.internet.reactor
_instances.reactor = twisted.internet.reactor
init_twisted_greenlet()


def pytest_addoption(parser):
group = parser.getgroup('twisted')
group.addoption(
'--reactor',
default='default',
choices=tuple(_reactor_fixtures.keys()),
choices=tuple(_reactor_installers.keys()),
)


def pytest_configure(config):
reactor_fixture = _reactor_fixtures[config.getoption('reactor')]

class ReactorPlugin(object):
reactor = staticmethod(
pytest.fixture(scope='session', autouse=True)(reactor_fixture)
)

config.pluginmanager.register(ReactorPlugin())
_reactor_installers[config.getoption('reactor')]()
110 changes: 71 additions & 39 deletions testing/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def assert_outcomes(run_result, outcomes):
formatted_output = format_run_result_output_for_assert(run_result)

try:
outcomes = run_result.parseoutcomes()
result_outcomes = run_result.parseoutcomes()
except ValueError:
assert False, formatted_output

for name, value in outcomes.items():
assert outcomes.get(name) == value, formatted_output
assert result_outcomes.get(name) == value, formatted_output


def format_run_result_output_for_assert(run_result):
Expand All @@ -42,7 +42,12 @@ def skip_if_reactor_not(expected_reactor):
)


def test_fail_later(testdir):
@pytest.fixture
def cmd_opts(request):
return '--reactor={}'.format(request.config.getoption('reactor')),


def test_fail_later(testdir, cmd_opts):
testdir.makepyfile("""
from twisted.internet import reactor, defer

Expand All @@ -57,11 +62,11 @@ def doit():
reactor.callLater(0.01, doit)
return d
""")
rr = testdir.run(sys.executable, "-m", "pytest")
rr = testdir.run(sys.executable, "-m", "pytest", *cmd_opts)
assert_outcomes(rr, {'failed': 1})


def test_succeed_later(testdir):
def test_succeed_later(testdir, cmd_opts):
testdir.makepyfile("""
from twisted.internet import reactor, defer

Expand All @@ -70,31 +75,31 @@ def test_succeed():
reactor.callLater(0.01, d.callback, 1)
return d
""")
rr = testdir.run(sys.executable, "-m", "pytest")
rr = testdir.run(sys.executable, "-m", "pytest", *cmd_opts)
assert_outcomes(rr, {'passed': 1})


def test_non_deferred(testdir):
def test_non_deferred(testdir, cmd_opts):
testdir.makepyfile("""
from twisted.internet import reactor, defer

def test_succeed():
return 42
""")
rr = testdir.run(sys.executable, "-m", "pytest")
rr = testdir.run(sys.executable, "-m", "pytest", *cmd_opts)
assert_outcomes(rr, {'passed': 1})


def test_exception(testdir):
def test_exception(testdir, cmd_opts):
testdir.makepyfile("""
def test_more_fail():
raise RuntimeError("foo")
""")
rr = testdir.run(sys.executable, "-m", "pytest")
rr = testdir.run(sys.executable, "-m", "pytest", *cmd_opts)
assert_outcomes(rr, {'failed': 1})


def test_inlineCallbacks(testdir):
def test_inlineCallbacks(testdir, cmd_opts):
testdir.makepyfile("""
from twisted.internet import reactor, defer
import pytest
Expand All @@ -112,11 +117,11 @@ def test_succeed(foo):
if foo == "web":
raise RuntimeError("baz")
""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert_outcomes(rr, {'passed': 2, 'failed': 1})


def test_twisted_greenlet(testdir):
def test_twisted_greenlet(testdir, cmd_opts):
testdir.makepyfile("""
import pytest, greenlet

Expand All @@ -134,23 +139,50 @@ def test_MAIN():
assert MAIN is greenlet.getcurrent()

""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert_outcomes(rr, {'passed': 1})


def test_blockon_in_fixture(testdir, cmd_opts):
testdir.makepyfile("""
from twisted.internet import reactor, defer
import pytest
import pytest_twisted

@pytest.fixture(scope="module",
params=["fs", "imap", "web"])
def foo(request):
d1, d2 = defer.Deferred(), defer.Deferred()
reactor.callLater(0.01, d1.callback, 1)
reactor.callLater(0.02, d2.callback, request.param)
pytest_twisted.blockon(d1)
return d2


@pytest_twisted.inlineCallbacks
def test_succeed(foo):
x = yield foo
if x == "web":
raise RuntimeError("baz")
""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
# assert not rr
assert_outcomes(rr, {'passed': 2, 'failed': 1})


@skip_if_reactor_not('default')
def test_blockon_in_hook(testdir):
def test_blockon_in_hook(testdir, cmd_opts):
testdir.makeconftest("""
import pytest_twisted as pt
from twisted.internet import reactor, defer

def pytest_configure(config):
pt.init_default_reactor()
d, d2 = defer.Deferred(), defer.Deferred()
reactor.callLater(0.01, d.callback, 1)
d1, d2 = defer.Deferred(), defer.Deferred()
reactor.callLater(0.01, d1.callback, 1)
reactor.callLater(0.02, d2.callback, 1)
pt.blockon(d)
pt.blockon(d)
pt.blockon(d1)
pt.blockon(d2)
""")
testdir.makepyfile("""
from twisted.internet import reactor, defer
Expand All @@ -160,36 +192,35 @@ def test_succeed():
reactor.callLater(0.01, d.callback, 1)
return d
""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert_outcomes(rr, {'passed': 1})


@skip_if_reactor_not('default')
def test_wrong_reactor(testdir):
testdir.makepyfile("""
def test_wrong_reactor(testdir, cmd_opts):
testdir.makeconftest("""
def pytest_addhooks():
import twisted.internet.reactor
twisted.internet.reactor = None

""")
testdir.makepyfile("""
def test_succeed():
pass
""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
assert 'WrongReactorAlreadyInstalledError' in rr.stdout.str()
assert_outcomes(rr, {'error': 1})
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert 'WrongReactorAlreadyInstalledError' in rr.stderr.str()


@skip_if_reactor_not('qt5reactor')
def test_blockon_in_hook_with_qt5reactor(testdir):
def test_blockon_in_hook_with_qt5reactor(testdir, cmd_opts):
testdir.makeconftest("""
import pytest_twisted as pt
import pytestqt
from twisted.internet import defer


def pytest_configure(config):
qapp = pytestqt.plugin.qapp(pytestqt.plugin.qapp_args())

pt.init_qt5_reactor(qapp)
pt.init_qt5_reactor()
d = defer.Deferred()

from twisted.internet import reactor
Expand All @@ -204,26 +235,27 @@ def test_succeed():
reactor.callLater(0.01, d.callback, 1)
return d
""")
rr = testdir.run(sys.executable, "-m", "pytest", "-v")
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert_outcomes(rr, {'passed': 1})


@skip_if_reactor_not('qt5reactor')
def test_wrong_reactor_with_qt5reactor(testdir):
testdir.makepyfile("""
def test_wrong_reactor_with_qt5reactor(testdir, cmd_opts):
testdir.makeconftest("""
def pytest_addhooks():
import twisted.internet.default
twisted.internet.default.install()

""")
testdir.makepyfile("""
def test_succeed():
pass
""")
rr = testdir.run(
sys.executable, "-m", "pytest", "-v", "--reactor=qt5reactor"
)
assert 'WrongReactorAlreadyInstalledError' in rr.stdout.str()
assert_outcomes(rr, {'error': 1})
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
assert 'WrongReactorAlreadyInstalledError' in rr.stderr.str()
# assert_outcomes(rr, {'error': 1})


@skip_if_reactor_not('default')
def test_pytest_from_reactor_thread(testdir):
testdir.makepyfile("""
import pytest
Expand Down