From 53b8aa065c5c05438918eca524fcbabb19b1a269 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 2 Mar 2019 11:17:43 -0300 Subject: [PATCH 1/2] Show testpaths option in the header if it has been used for collection Fix #4875 --- changelog/4875.feature.rst | 3 +++ src/_pytest/terminal.py | 7 ++++++- testing/test_terminal.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 changelog/4875.feature.rst diff --git a/changelog/4875.feature.rst b/changelog/4875.feature.rst new file mode 100644 index 000000000..6d91fb465 --- /dev/null +++ b/changelog/4875.feature.rst @@ -0,0 +1,3 @@ +The `testpaths `__ configuration option is now displayed next +to the ``rootdir`` and ``inifile`` lines in the pytest header if the option is in effect, i.e., directories or file names were +not explicitly passed in the command line. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index eda0c0905..fc78f4de4 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -586,8 +586,13 @@ class TerminalReporter(object): inifile = "" if config.inifile: inifile = " " + config.rootdir.bestrelpath(config.inifile) - lines = ["rootdir: %s, inifile:%s" % (config.rootdir, inifile)] + line = "rootdir: %s, inifile:%s" % (config.rootdir, inifile) + testpaths = config.getini("testpaths") + if testpaths and config.args == testpaths: + rel_paths = [config.rootdir.bestrelpath(x) for x in testpaths] + line += ", testpaths: {}".format(", ".join(rel_paths)) + lines = [line] plugininfo = config.pluginmanager.list_plugin_distinfo() if plugininfo: diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 798e8c16a..abe9549e2 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -567,6 +567,26 @@ class TestTerminalFunctional(object): if request.config.pluginmanager.list_plugin_distinfo(): result.stdout.fnmatch_lines(["plugins: *"]) + def test_header(self, testdir, request): + testdir.tmpdir.join("tests").ensure_dir() + testdir.tmpdir.join("gui").ensure_dir() + result = testdir.runpytest() + result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile:"]) + + testdir.makeini( + """ + [pytest] + testpaths = tests gui + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines( + ["rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui"] + ) + + result = testdir.runpytest("tests") + result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile: tox.ini"]) + def test_showlocals(self, testdir): p1 = testdir.makepyfile( """ From 0deb7b1696bdc97f4d6dcc132bf665f794204448 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 2 Mar 2019 11:31:09 -0300 Subject: [PATCH 2/2] Do not show "inifile:" string if there's no configuration file --- changelog/4875.feature.rst | 2 ++ src/_pytest/terminal.py | 16 ++++++++-------- testing/test_config.py | 6 +++--- testing/test_session.py | 2 +- testing/test_terminal.py | 22 +++++++++++----------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/changelog/4875.feature.rst b/changelog/4875.feature.rst index 6d91fb465..d9fb65ca5 100644 --- a/changelog/4875.feature.rst +++ b/changelog/4875.feature.rst @@ -1,3 +1,5 @@ The `testpaths `__ configuration option is now displayed next to the ``rootdir`` and ``inifile`` lines in the pytest header if the option is in effect, i.e., directories or file names were not explicitly passed in the command line. + +Also, ``inifile`` is only displayed if there's a configuration file, instead of an empty ``inifile:`` string. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index fc78f4de4..0868bd751 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -583,21 +583,21 @@ class TerminalReporter(object): self.write_line(line) def pytest_report_header(self, config): - inifile = "" - if config.inifile: - inifile = " " + config.rootdir.bestrelpath(config.inifile) + line = "rootdir: %s" % config.rootdir + + if config.inifile: + line += ", inifile: " + config.rootdir.bestrelpath(config.inifile) - line = "rootdir: %s, inifile:%s" % (config.rootdir, inifile) testpaths = config.getini("testpaths") if testpaths and config.args == testpaths: rel_paths = [config.rootdir.bestrelpath(x) for x in testpaths] line += ", testpaths: {}".format(", ".join(rel_paths)) - lines = [line] + result = [line] + plugininfo = config.pluginmanager.list_plugin_distinfo() if plugininfo: - - lines.append("plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) - return lines + result.append("plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) + return result def pytest_collection_finish(self, session): if self.config.getoption("collectonly"): diff --git a/testing/test_config.py b/testing/test_config.py index c5c0ca939..3dd707bfa 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -697,9 +697,9 @@ def test_invalid_options_show_extra_information(testdir): ["-v", "dir2", "dir1"], ], ) -def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args): +def test_consider_args_after_options_for_rootdir(testdir, args): """ - Consider all arguments in the command-line for rootdir and inifile + Consider all arguments in the command-line for rootdir discovery, even if they happen to occur after an option. #949 """ # replace "dir1" and "dir2" from "args" into their real directory @@ -713,7 +713,7 @@ def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args): args[i] = d2 with root.as_cwd(): result = testdir.runpytest(*args) - result.stdout.fnmatch_lines(["*rootdir: *myroot, inifile:"]) + result.stdout.fnmatch_lines(["*rootdir: *myroot"]) @pytest.mark.skipif("sys.platform == 'win32'") diff --git a/testing/test_session.py b/testing/test_session.py index 6b185f76b..e5eb081d4 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -332,7 +332,7 @@ def test_rootdir_option_arg(testdir, monkeypatch, path): result = testdir.runpytest("--rootdir={}".format(path)) result.stdout.fnmatch_lines( [ - "*rootdir: {}/root, inifile:*".format(testdir.tmpdir), + "*rootdir: {}/root".format(testdir.tmpdir), "root/test_rootdir_option_arg.py *", "*1 passed*", ] diff --git a/testing/test_terminal.py b/testing/test_terminal.py index abe9549e2..fe67a6aec 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -570,9 +570,17 @@ class TestTerminalFunctional(object): def test_header(self, testdir, request): testdir.tmpdir.join("tests").ensure_dir() testdir.tmpdir.join("gui").ensure_dir() - result = testdir.runpytest() - result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile:"]) + # no ini file + result = testdir.runpytest() + result.stdout.fnmatch_lines(["rootdir: *test_header0"]) + + # with inifile + testdir.makeini("""[pytest]""") + result = testdir.runpytest() + result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile: tox.ini"]) + + # with testpaths option, and not passing anything in the command-line testdir.makeini( """ [pytest] @@ -584,6 +592,7 @@ class TestTerminalFunctional(object): ["rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui"] ) + # with testpaths option, passing directory in command-line: do not show testpaths then result = testdir.runpytest("tests") result.stdout.fnmatch_lines(["rootdir: *test_header0, inifile: tox.ini"]) @@ -1219,15 +1228,6 @@ def test_summary_stats(exp_line, exp_color, stats_arg): assert color == exp_color -def test_no_trailing_whitespace_after_inifile_word(testdir): - result = testdir.runpytest("") - assert "inifile:\n" in result.stdout.str() - - testdir.makeini("[pytest]") - result = testdir.runpytest("") - assert "inifile: tox.ini\n" in result.stdout.str() - - class TestClassicOutputStyle(object): """Ensure classic output style works as expected (#3883)"""