Skip to content

Commit

Permalink
#1642 Add rootdir option
Browse files Browse the repository at this point in the history
  • Loading branch information
feuillemorte committed Jan 17, 2018
1 parent 01e37fe commit d784155
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Ned Batchelder
Neven Mundar
Nicolas Delaby
Oleg Pidsadnyi
Oleg Sushchenko
Oliver Bestwalter
Omar Kohl
Omer Hadari
Expand Down
19 changes: 19 additions & 0 deletions _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def pytest_addoption(parser):
parser.addini("testpaths", "directories to search for tests when no files or directories are given in the "
"command line.",
type="args", default=[])
parser.addini("rootdir", "define root directory for tests. If this parameter defined command argument "
"'--rootdir' will not work",
type="args", default=[])
# parser.addini("dirpatterns",
# "patterns specifying possible locations of test files",
# type="linelist", default=["**/test_*.txt",
Expand All @@ -53,6 +56,11 @@ def pytest_addoption(parser):
group._addoption("--continue-on-collection-errors", action="store_true",
default=False, dest="continue_on_collection_errors",
help="Force test execution even if collection errors occur.")
group._addoption("--rootdir", action="store",
dest="rootdir",
help="Define root directory for tests. Can be relative path: 'root_dir', './root_dir', "
"'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables: "
"'$HOME/root_dir'. If parameter 'rootdir' defined in *.ini file this argument will not work")

group = parser.getgroup("collect", "collection")
group.addoption('--collectonly', '--collect-only', action="store_true",
Expand Down Expand Up @@ -283,6 +291,17 @@ def __init__(self, config):
self.trace = config.trace.root.get("collection")
self._norecursepatterns = config.getini("norecursedirs")
self.startdir = py.path.local()

rootdir_ini = config.getini('rootdir')
self.rootdir = rootdir_ini[0] if rootdir_ini else config.option.rootdir
if self.rootdir:
rootdir_abs_path = py.path.local(self.rootdir)
if not os.path.isdir(str(rootdir_abs_path)):
raise UsageError("Directory '{}' not found. Check your '--rootdir' option.".format(rootdir_abs_path))
config.invocation_dir = rootdir_abs_path
config.rootdir = rootdir_abs_path
sys.path.append(str(rootdir_abs_path))

self.config.pluginmanager.register(self, name="session")

def _makeid(self):
Expand Down
32 changes: 32 additions & 0 deletions testing/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,35 @@ def pytest_sessionfinish():
""")
res = testdir.runpytest("--collect-only")
assert res.ret == EXIT_NOTESTSCOLLECTED


def test_rootdir_option_arg(testdir):
rootdir = testdir.mkdir("root")
rootdir.join("spoon.py").write("spoon_number = 1")
testsdir = rootdir.mkdir("tests")
testsdir.join("test_one.py").write("from spoon import spoon_number\ndef test_one():\n assert spoon_number")

result = testdir.runpytest()
result.stdout.fnmatch_lines(["*No module named*spoon*"])

result = testdir.runpytest("--rootdir=root")
result.stdout.fnmatch_lines(["*1 passed*"])


def test_rootdir_option_ini_file(testdir):
rootdir = testdir.mkdir("root")
rootdir.join("spoon.py").write("spoon_number = 1")
testsdir = rootdir.mkdir("tests")
testsdir.join("test_one.py").write("from spoon import spoon_number\ndef test_one():\n assert spoon_number")

result = testdir.runpytest()
result.stdout.fnmatch_lines(["*No module named*spoon*"])
testdir.makeini("""
[pytest]
rootdir=root
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest("--rootdir=ignored_argument")
print(result.stdout.str())
result.stdout.fnmatch_lines(["*1 passed*"])

0 comments on commit d784155

Please sign in to comment.