-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Ensure that a module within a namespace package can be found by --pyargs #1568
Changes from 5 commits
7be0506
36b99a4
19e16f7
09b2492
11b04e0
811f837
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
import sys | ||
|
||
import _pytest._code | ||
|
@@ -557,6 +558,67 @@ def join_pythonpath(what): | |
"*not*found*test_hello*", | ||
]) | ||
|
||
def test_cmdline_python_namespace_package(self, testdir, monkeypatch): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nitpick: add a docstring which a comment pointing to the issue: def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
"""
test --pyargs option with namespace packages (#1567)
""" |
||
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: would you mind using a keyword argument here ( |
||
|
||
search_path = [] | ||
for dirname in "hello", "world": | ||
d = testdir.mkdir(dirname) | ||
search_path.append(d) | ||
ns = d.mkdir("ns_pkg") | ||
ns.join("__init__.py").write( | ||
"__import__('pkg_resources').declare_namespace(__name__)") | ||
lib = ns.mkdir(dirname) | ||
lib.ensure("__init__.py") | ||
lib.join("test_{0}.py".format(dirname)). \ | ||
write("def test_{0}(): pass\n" | ||
"def test_other():pass".format(dirname)) | ||
|
||
# The structure of the test directory is now: | ||
# . | ||
# ├── hello | ||
# │ └── ns_pkg | ||
# │ ├── __init__.py | ||
# │ └── hello | ||
# │ ├── __init__.py | ||
# │ └── test_hello.py | ||
# └── world | ||
# └── ns_pkg | ||
# ├── __init__.py | ||
# └── world | ||
# ├── __init__.py | ||
# └── test_world.py | ||
|
||
def join_pythonpath(*dirs): | ||
cur = py.std.os.environ.get('PYTHONPATH') | ||
if cur: | ||
dirs += (cur,) | ||
return ':'.join(str(p) for p in dirs) | ||
monkeypatch.setenv('PYTHONPATH', join_pythonpath(*search_path)) | ||
for p in search_path: | ||
monkeypatch.syspath_prepend(p) | ||
|
||
# mixed module and filenames: | ||
result = testdir.runpytest("--pyargs", "ns_pkg.hello", "world/ns_pkg") | ||
assert result.ret == 0 | ||
result.stdout.fnmatch_lines([ | ||
"*4 passed*" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to run pytest with result.stdout.fnmatch_lines([
"*test_hello.py::test_hello*PASSED",
"*test_hello.py::test_other*PASSED",
"*test_world.py::test_world*PASSED",
"*test_world.py::test_other*PASSED",
]) (the same comment apply to the next checks done here) |
||
]) | ||
|
||
# specify tests within a module | ||
result = testdir.runpytest("--pyargs", "ns_pkg.world.test_world::test_other") | ||
assert result.ret == 0 | ||
result.stdout.fnmatch_lines([ | ||
"*1 passed*" | ||
]) | ||
|
||
# namespace package | ||
result = testdir.runpytest("--pyargs", "ns_pkg") | ||
assert result.ret | ||
result.stderr.fnmatch_lines([ | ||
"ERROR:*Cannot*uniquely*resolve*package*directory:*ns_pkg", | ||
]) | ||
|
||
def test_cmdline_python_package_not_exists(self, testdir): | ||
result = testdir.runpytest("--pyargs", "tpkgwhatv") | ||
assert result.ret | ||
|
@@ -697,4 +759,3 @@ def test_setup_function(self, testdir): | |
* setup *test_1* | ||
* call *test_1* | ||
""") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please put a comment explaining why do you check for the file size at this point?