streamline tests so that they work wit pytest-2.8

This commit is contained in:
holger krekel
2015-05-06 13:34:33 +02:00
parent eb53a5f8a0
commit faf2e0861f
5 changed files with 51 additions and 36 deletions

View File

@@ -4,6 +4,9 @@
- fix issue594: properly report errors when the test collection - fix issue594: properly report errors when the test collection
is random. Thanks Bruno Oliveira. is random. Thanks Bruno Oliveira.
- some internal test suite adaptation (to become forward
compatible with the upcoming pytest-2.8)
1.11 1.11
------------------------- -------------------------

View File

@@ -113,7 +113,7 @@ class TestDistribution:
import py import py
assert tmpdir.relto(py.path.local(%r)), tmpdir assert tmpdir.relto(py.path.local(%r)), tmpdir
""" % str(testdir.tmpdir)) """ % str(testdir.tmpdir))
result = testdir.runpytest(p1, "-n1") result = testdir.runpytest_subprocess(p1, "-n1")
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*1 passed*", "*1 passed*",
@@ -243,7 +243,7 @@ class TestDistribution:
print ("s2call-finished") print ("s2call-finished")
""") """)
args = ["-n1", "--debug"] args = ["-n1", "--debug"]
result = testdir.runpytest(*args) result = testdir.runpytest_subprocess(*args)
s = result.stdout.str() s = result.stdout.str()
assert result.ret == 2 assert result.ret == 2
assert 's2call' in s assert 's2call' in s
@@ -256,9 +256,8 @@ class TestDistribution:
import time import time
time.sleep(10) time.sleep(10)
""") """)
child = testdir.spawn_pytest("-n1") child = testdir.spawn_pytest("-n1 -v")
py.std.time.sleep(0.1) child.expect(".*test_sleep.*")
child.expect(".*test session starts.*")
child.kill(2) # keyboard interrupt child.kill(2) # keyboard interrupt
child.expect(".*KeyboardInterrupt.*") child.expect(".*KeyboardInterrupt.*")
#child.expect(".*seconds.*") #child.expect(".*seconds.*")
@@ -271,7 +270,7 @@ class TestDistEach:
def test_hello(): def test_hello():
pass pass
""") """)
result = testdir.runpytest("--debug", "--dist=each", "--tx=2*popen") result = testdir.runpytest_subprocess("--debug", "--dist=each", "--tx=2*popen")
assert not result.ret assert not result.ret
result.stdout.fnmatch_lines(["*2 pass*"]) result.stdout.fnmatch_lines(["*2 pass*"])
@@ -408,7 +407,7 @@ def test_funcarg_teardown_failure(testdir):
def test_hello(myarg): def test_hello(myarg):
pass pass
""") """)
result = testdir.runpytest("--debug", p) # , "-n1") result = testdir.runpytest_subprocess("--debug", p) # , "-n1")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*ValueError*42*", "*ValueError*42*",
"*1 passed*1 error*", "*1 passed*1 error*",
@@ -455,7 +454,7 @@ def test_issue34_pluginloading_in_subprocess(testdir):
def test_hello(): def test_hello():
assert pytest.sample_variable == "testing" assert pytest.sample_variable == "testing"
""") """)
result = testdir.runpytest("-n1", "-p", "plugin123") result = testdir.runpytest_subprocess("-n1", "-p", "plugin123")
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*1 passed*", "*1 passed*",

View File

@@ -1,10 +1,29 @@
import py import py
import pytest
import execnet import execnet
@pytest.fixture(scope="session", autouse=True)
def _ensure_imports():
# we import some modules because pytest-2.8's testdir fixture
# will unload all modules after each test and this cause
# (unknown) problems with execnet.Group()
execnet.Group
execnet.makegateway
pytest_plugins = "pytester" pytest_plugins = "pytester"
#rsyncdirs = ['.', '../xdist', py.path.local(execnet.__file__).dirpath()] #rsyncdirs = ['.', '../xdist', py.path.local(execnet.__file__).dirpath()]
@pytest.fixture(autouse=True)
def _divert_atexit(request, monkeypatch):
import atexit
l = []
def finish():
while l:
l.pop()()
monkeypatch.setattr(atexit, "register", l.append)
request.addfinalizer(finish)
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption('--gx', parser.addoption('--gx',
action="append", dest="gspecs", action="append", dest="gspecs",
@@ -13,6 +32,13 @@ def pytest_addoption(parser):
def pytest_funcarg__specssh(request): def pytest_funcarg__specssh(request):
return getspecssh(request.config) return getspecssh(request.config)
@pytest.fixture
def testdir(testdir):
# pytest before 2.8 did not have a runpytest_subprocess
if not hasattr(testdir, "runpytest_subprocess"):
testdir.runpytest_subprocess = testdir.runpytest
return testdir
# configuration information for tests # configuration information for tests
def getgspecs(config): def getgspecs(config):
return [execnet.XSpec(spec) return [execnet.XSpec(spec)

View File

@@ -1,29 +1,25 @@
import py import py
import pytest import pytest
import execnet import execnet
from _pytest.pytester import HookRecorder
from xdist import slavemanage from xdist import slavemanage
from xdist.slavemanage import HostRSync, NodeManager from xdist.slavemanage import HostRSync, NodeManager
pytest_plugins = "pytester", pytest_plugins = "pytester",
def pytest_funcarg__hookrecorder(request): def pytest_funcarg__hookrecorder(request, config):
_pytest = request.getfuncargvalue('_pytest') hookrecorder = HookRecorder(config.pluginmanager)
config = request.getfuncargvalue('config') request.addfinalizer(hookrecorder.finish_recording)
return _pytest.gethookrecorder(config.hook) return hookrecorder
def pytest_funcarg__config(request): def pytest_funcarg__config(testdir):
testdir = request.getfuncargvalue("testdir") return testdir.parseconfig()
config = testdir.parseconfig()
return config
def pytest_funcarg__mysetup(request): def pytest_funcarg__mysetup(tmpdir):
class mysetup: class mysetup:
def __init__(self, request): source = tmpdir.mkdir("source")
temp = request.getfuncargvalue("tmpdir") dest = tmpdir.mkdir("dest")
self.source = temp.mkdir("source") return mysetup()
self.dest = temp.mkdir("dest")
request.getfuncargvalue("_pytest")
return mysetup(request)
@pytest.fixture @pytest.fixture
def slavecontroller(monkeypatch): def slavecontroller(monkeypatch):
@@ -45,8 +41,7 @@ class TestNodeManagerPopen:
for spec in NodeManager(config, l, defaultchdir="abc").specs: for spec in NodeManager(config, l, defaultchdir="abc").specs:
assert spec.chdir == "abc" assert spec.chdir == "abc"
def test_popen_makegateway_events(self, config, def test_popen_makegateway_events(self, config, hookrecorder, slavecontroller):
hookrecorder, _pytest, slavecontroller):
hm = NodeManager(config, ["popen"] * 2) hm = NodeManager(config, ["popen"] * 2)
hm.setup_nodes(None) hm.setup_nodes(None)
call = hookrecorder.popcall("pytest_xdist_setupnodes") call = hookrecorder.popcall("pytest_xdist_setupnodes")
@@ -114,14 +109,6 @@ class TestNodeManagerPopen:
call = hookrecorder.popcall("pytest_xdist_rsyncfinish") call = hookrecorder.popcall("pytest_xdist_rsyncfinish")
class TestHRSync: class TestHRSync:
def pytest_funcarg__mysetup(self, request):
class mysetup:
def __init__(self, request):
tmp = request.getfuncargvalue('tmpdir')
self.source = tmp.mkdir("source")
self.dest = tmp.mkdir("dest")
return mysetup(request)
def test_hrsync_filter(self, mysetup): def test_hrsync_filter(self, mysetup):
source, _ = mysetup.source, mysetup.dest # noqa source, _ = mysetup.source, mysetup.dest # noqa
source.ensure("dir", "file.txt") source.ensure("dir", "file.txt")
@@ -151,7 +138,7 @@ class TestHRSync:
class TestNodeManager: class TestNodeManager:
@py.test.mark.xfail @py.test.mark.xfail(run=False)
def test_rsync_roots_no_roots(self, testdir, mysetup): def test_rsync_roots_no_roots(self, testdir, mysetup):
mysetup.source.ensure("dir1", "file1").write("hello") mysetup.source.ensure("dir1", "file1").write("hello")
config = testdir.parseconfig(mysetup.source) config = testdir.parseconfig(mysetup.source)

View File

@@ -58,8 +58,8 @@ def pytest_cmdline_main(config):
looponfail_main(config) looponfail_main(config)
return 2 # looponfail only can get stop with ctrl-C anyway return 2 # looponfail only can get stop with ctrl-C anyway
def pytest_configure(config, __multicall__): @pytest.mark.trylast
__multicall__.execute() def pytest_configure(config):
if config.getoption("dist") != "no": if config.getoption("dist") != "no":
from xdist.dsession import DSession from xdist.dsession import DSession
session = DSession(config) session = DSession(config)