some fixes for --pyargs situations and the docs, remove wrongly added test

This commit is contained in:
holger krekel 2010-11-07 00:22:16 +01:00
parent d0ac4135a2
commit 885c7ce281
6 changed files with 39 additions and 17 deletions

View File

@ -12,6 +12,18 @@ You can set the :confval:`norecursedirs` option in an ini-file, for example your
This would tell py.test to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory. This would tell py.test to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory.
always try to interpret arguments as Python packages
-----------------------------------------------------
You can use the ``--pyargs`` option to make py.test try
interpreting arguments as python package names, deriving
their file system path and then running the test. Through
an ini-file and the :confval:`addopts` option you can make
this change more permanently::
# content of setup.cfg or tox.ini
[pytest]
addopts = --pyargs
finding out what is collected finding out what is collected
----------------------------------------------- -----------------------------------------------

View File

@ -36,7 +36,7 @@ Several test run options::
Import 'pkg' and use its filesystem location to find and run tests:: Import 'pkg' and use its filesystem location to find and run tests::
py.test --testpkg=pypkg # run all tests found below directory of pypkg py.test --pyargs pkg # run all tests found below directory of pypkg
calling pytest through ``python -m pytest`` calling pytest through ``python -m pytest``
----------------------------------------------------- -----------------------------------------------------

View File

@ -366,9 +366,10 @@ class Collection(FSCollector):
self.trace.root.indent += 1 self.trace.root.indent += 1
self._notfound = [] self._notfound = []
self._initialpaths = set() self._initialpaths = set()
self._initialargs = args self._initialparts = []
for arg in args: for arg in args:
parts = self._parsearg(arg) parts = self._parsearg(arg)
self._initialparts.append(parts)
self._initialpaths.add(parts[0]) self._initialpaths.add(parts[0])
self.ihook.pytest_collectstart(collector=self) self.ihook.pytest_collectstart(collector=self)
rep = self.ihook.pytest_make_collect_report(collector=self) rep = self.ihook.pytest_make_collect_report(collector=self)
@ -376,7 +377,7 @@ class Collection(FSCollector):
self.trace.root.indent -= 1 self.trace.root.indent -= 1
if self._notfound: if self._notfound:
for arg, exc in self._notfound: for arg, exc in self._notfound:
line = "no name %r in any of %r" % (exc.args[1], exc.args[0]) line = "(no name %r in any of %r)" % (arg, exc.args[0])
raise pytest.UsageError("not found: %s\n%s" %(arg, line)) raise pytest.UsageError("not found: %s\n%s" %(arg, line))
if not genitems: if not genitems:
return rep.result return rep.result
@ -388,8 +389,9 @@ class Collection(FSCollector):
return items return items
def collect(self): def collect(self):
for arg in self._initialargs: for parts in self._initialparts:
self.trace("processing arg", arg) arg = "::".join(map(str, parts))
self.trace("processing argument", arg)
self.trace.root.indent += 1 self.trace.root.indent += 1
try: try:
for x in self._collect(arg): for x in self._collect(arg):
@ -417,6 +419,7 @@ class Collection(FSCollector):
def _collectfile(self, path): def _collectfile(self, path):
ihook = self.gethookproxy(path) ihook = self.gethookproxy(path)
if not self.isinitpath(path):
if ihook.pytest_ignore_collect(path=path, config=self.config): if ihook.pytest_ignore_collect(path=path, config=self.config):
return () return ()
return ihook.pytest_collect_file(path=path, parent=self) return ihook.pytest_collect_file(path=path, parent=self)
@ -439,6 +442,8 @@ class Collection(FSCollector):
p = py.path.local(mod.__file__) p = py.path.local(mod.__file__)
if p.purebasename == "__init__": if p.purebasename == "__init__":
p = p.dirpath() p = p.dirpath()
else:
p = p.new(basename=p.purebasename+".py")
return p return p
def _parsearg(self, arg): def _parsearg(self, arg):

View File

@ -310,7 +310,8 @@ class TestInvocationVariants:
out, err = capsys.readouterr() out, err = capsys.readouterr()
assert "--myopt" in out assert "--myopt" in out
def test_cmdline_python_package(self, testdir): def test_cmdline_python_package(self, testdir, monkeypatch):
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE')
path = testdir.mkpydir("tpkg") path = testdir.mkpydir("tpkg")
path.join("test_hello.py").write("def test_hello(): pass") path.join("test_hello.py").write("def test_hello(): pass")
path.join("test_world.py").write("def test_world(): pass") path.join("test_world.py").write("def test_world(): pass")

View File

@ -161,15 +161,6 @@ def test_functional(testdir):
result = testdir.runpytest("--no-assert") result = testdir.runpytest("--no-assert")
assert "3 == 4" not in result.stdout.str() assert "3 == 4" not in result.stdout.str()
def test_AssertionErrorIdentity(testdir):
testdir.makepyfile("""
def test_hello():
import exceptions
assert AssertionError is exceptions.AssertionError
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
def test_triple_quoted_string_issue113(testdir): def test_triple_quoted_string_issue113(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def test_hello(): def test_hello():

View File

@ -213,6 +213,19 @@ class TestCustomConftests:
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines(["*1 passed*"]) result.stdout.fnmatch_lines(["*1 passed*"])
def test_ignore_collect_not_called_on_argument(self, testdir):
testdir.makeconftest("""
def pytest_ignore_collect(path, config):
return True
""")
p = testdir.makepyfile("def test_hello(): pass")
result = testdir.runpytest(p)
assert result.ret == 0
assert "1 passed" in result.stdout.str()
result = testdir.runpytest()
assert result.ret == 0
assert "1 passed" not in result.stdout.str()
def test_collectignore_exclude_on_option(self, testdir): def test_collectignore_exclude_on_option(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
collect_ignore = ['hello', 'test_world.py'] collect_ignore = ['hello', 'test_world.py']