From 2a825169b22ef62a078f2cbf127d9b0f454ebd16 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 17 Nov 2010 18:24:28 +0100 Subject: [PATCH] fix doctest IDs, also fix tree traversal and remove dead code --- _pytest/doctest.py | 9 ++------ _pytest/session.py | 5 ++--- testing/acceptance_test.py | 19 ++++++++++++++++ testing/test_collection.py | 44 ++++++++++++++++++++++++++++++++++---- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 6f09fb0eb..05699ca4c 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -33,11 +33,6 @@ class ReprFailDoctest(TerminalRepr): self.reprlocation.toterminal(tw) class DoctestItem(pytest.Item): - def __init__(self, path, parent): - name = self.__class__.__name__ + ":" + path.basename - super(DoctestItem, self).__init__(name=name, parent=parent) - self.fspath = path - def repr_failure(self, excinfo): if excinfo.errisinstance(py.std.doctest.DocTestFailure): doctestfailure = excinfo.value @@ -67,13 +62,13 @@ class DoctestItem(pytest.Item): def reportinfo(self): return self.fspath, None, "[doctest]" -class DoctestTextfile(DoctestItem): +class DoctestTextfile(DoctestItem, pytest.File): def runtest(self): failed, tot = py.std.doctest.testfile( str(self.fspath), module_relative=False, raise_on_error=True, verbose=0) -class DoctestModule(DoctestItem): +class DoctestModule(DoctestItem, pytest.File): def runtest(self): module = self.fspath.pyimport() failed, tot = py.std.doctest.testmod( diff --git a/_pytest/session.py b/_pytest/session.py index 25f4ae7e3..8c2da87e7 100644 --- a/_pytest/session.py +++ b/_pytest/session.py @@ -482,7 +482,8 @@ class Session(FSCollector): resultnodes = [] for node in matching: if isinstance(node, pytest.Item): - resultnodes.append(node) + if not names: + resultnodes.append(node) continue assert isinstance(node, pytest.Collector) node.ihook.pytest_collectstart(collector=node) @@ -508,5 +509,3 @@ class Session(FSCollector): for x in self.genitems(subnode): yield x node.ihook.pytest_collectreport(report=rep) - -Session = Session diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 6d85cfcf5..4bf08c5d2 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -372,3 +372,22 @@ class TestInvocationVariants: """) reprec = testdir.inline_run(testpath) reprec.assertoutcome(passed=1) + + def test_doctest_id(self, testdir): + testdir.makefile('.txt', """ + >>> x=3 + >>> x + 4 + """) + result = testdir.runpytest("-rf") + lines = result.stdout.str().splitlines() + for line in lines: + if line.startswith("FAIL "): + testid = line[5:].strip() + break + result = testdir.runpytest(testid, '-rf') + result.stdout.fnmatch_lines([ + line, + "*1 failed*", + ]) + diff --git a/testing/test_collection.py b/testing/test_collection.py index 819f53b4d..e26925bef 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -472,10 +472,6 @@ class TestSession: item2b, = newcol.perform_collect([item.nodeid], genitems=False) assert item2b == item2 -def getargnode(collection, arg): - argpath = arg.relto(collection.fspath) - return collection.perform_collect([argpath], genitems=False)[0] - class Test_getinitialnodes: def test_global_file(self, testdir, tmpdir): x = tmpdir.ensure("x.py") @@ -552,3 +548,43 @@ class Test_genitems: s = items[0].getmodpath(stopatmodule=False) assert s.endswith("test_example_items1.testone") print(s) + +def test_matchnodes_two_collections_same_file(testdir): + testdir.makeconftest(""" + import pytest + def pytest_configure(config): + config.pluginmanager.register(Plugin2()) + + class Plugin2: + def pytest_collect_file(self, path, parent): + if path.ext == ".abc": + return MyFile2(path, parent) + + def pytest_collect_file(path, parent): + if path.ext == ".abc": + return MyFile1(path, parent) + + class MyFile1(pytest.Item, pytest.File): + def runtest(self): + pass + class MyFile2(pytest.File): + def collect(self): + return [Item2("hello", parent=self)] + + class Item2(pytest.Item): + def runtest(self): + pass + """) + p = testdir.makefile(".abc", "") + result = testdir.runpytest() + assert result.ret == 0 + result.stdout.fnmatch_lines([ + "*2 passed*", + ]) + res = testdir.runpytest("%s::hello" % p.basename) + res.stdout.fnmatch_lines([ + "*1 passed*", + ]) + + +