From d784155fd2d306157bca115cb87c8eb3c488b746 Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Wed, 17 Jan 2018 23:02:31 +0300 Subject: [PATCH] #1642 Add rootdir option --- AUTHORS | 1 + _pytest/main.py | 19 +++++++++++++++++++ testing/test_session.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/AUTHORS b/AUTHORS index 862378be9..d4447462c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -138,6 +138,7 @@ Ned Batchelder Neven Mundar Nicolas Delaby Oleg Pidsadnyi +Oleg Sushchenko Oliver Bestwalter Omar Kohl Omer Hadari diff --git a/_pytest/main.py b/_pytest/main.py index fce4f35f3..7fa86db17 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -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", @@ -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", @@ -283,6 +291,17 @@ class Session(nodes.FSCollector): 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): diff --git a/testing/test_session.py b/testing/test_session.py index 9ec13f523..a8c5a408d 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -253,3 +253,35 @@ def test_sessionfinish_with_start(testdir): """) 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*"])