#1642 Add rootdir option

This commit is contained in:
feuillemorte 2018-01-17 23:02:31 +03:00
parent 01e37fe892
commit d784155fd2
3 changed files with 52 additions and 0 deletions

View File

@ -138,6 +138,7 @@ Ned Batchelder
Neven Mundar Neven Mundar
Nicolas Delaby Nicolas Delaby
Oleg Pidsadnyi Oleg Pidsadnyi
Oleg Sushchenko
Oliver Bestwalter Oliver Bestwalter
Omar Kohl Omar Kohl
Omer Hadari Omer Hadari

View File

@ -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 " parser.addini("testpaths", "directories to search for tests when no files or directories are given in the "
"command line.", "command line.",
type="args", default=[]) 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", # parser.addini("dirpatterns",
# "patterns specifying possible locations of test files", # "patterns specifying possible locations of test files",
# type="linelist", default=["**/test_*.txt", # type="linelist", default=["**/test_*.txt",
@ -53,6 +56,11 @@ def pytest_addoption(parser):
group._addoption("--continue-on-collection-errors", action="store_true", group._addoption("--continue-on-collection-errors", action="store_true",
default=False, dest="continue_on_collection_errors", default=False, dest="continue_on_collection_errors",
help="Force test execution even if collection errors occur.") 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 = parser.getgroup("collect", "collection")
group.addoption('--collectonly', '--collect-only', action="store_true", group.addoption('--collectonly', '--collect-only', action="store_true",
@ -283,6 +291,17 @@ class Session(nodes.FSCollector):
self.trace = config.trace.root.get("collection") self.trace = config.trace.root.get("collection")
self._norecursepatterns = config.getini("norecursedirs") self._norecursepatterns = config.getini("norecursedirs")
self.startdir = py.path.local() 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") self.config.pluginmanager.register(self, name="session")
def _makeid(self): def _makeid(self):

View File

@ -253,3 +253,35 @@ def test_sessionfinish_with_start(testdir):
""") """)
res = testdir.runpytest("--collect-only") res = testdir.runpytest("--collect-only")
assert res.ret == EXIT_NOTESTSCOLLECTED 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*"])