From afb8a4e35d0bf3393668a90bb4a30e55091543a2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 6 Jan 2018 13:31:38 -0200 Subject: [PATCH 01/50] Document bootstrap and initialization hooks Fix #2616 --- _pytest/hookspec.py | 15 +++++++++++++-- doc/en/writing_plugins.rst | 13 ++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/_pytest/hookspec.py b/_pytest/hookspec.py index f004dd097d8..c3d55018997 100644 --- a/_pytest/hookspec.py +++ b/_pytest/hookspec.py @@ -85,8 +85,7 @@ def pytest_configure(config): # ------------------------------------------------------------------------- # Bootstrapping hooks called for plugins registered early enough: -# internal and 3rd party plugins as well as directly -# discoverable conftest.py local plugins. +# internal and 3rd party plugins. # ------------------------------------------------------------------------- @@ -96,6 +95,9 @@ def pytest_cmdline_parse(pluginmanager, args): Stops at first non-None result, see :ref:`firstresult` + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager :param list[str] args: list of arguments passed on the command line """ @@ -107,6 +109,9 @@ def pytest_cmdline_preparse(config, args): This hook is considered deprecated and will be removed in a future pytest version. Consider using :func:`pytest_load_initial_conftests` instead. + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + :param _pytest.config.Config config: pytest config object :param list[str] args: list of arguments passed on the command line """ @@ -117,6 +122,9 @@ def pytest_cmdline_main(config): """ called for performing the main command line action. The default implementation will invoke the configure hooks and runtest_mainloop. + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + Stops at first non-None result, see :ref:`firstresult` :param _pytest.config.Config config: pytest config object @@ -127,6 +135,9 @@ def pytest_load_initial_conftests(early_config, parser, args): """ implements the loading of initial conftest files ahead of command line option parsing. + .. note:: + This hook will not be called for ``conftest.py`` files, only for setuptools plugins. + :param _pytest.config.Config early_config: pytest config object :param list[str] args: list of arguments passed on the command line :param _pytest.config.Parser parser: to add command line options diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index 35fc7010df1..753b300cd91 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -583,11 +583,22 @@ pytest hook reference Initialization, command line and configuration hooks ---------------------------------------------------- +Bootstrapping hooks +~~~~~~~~~~~~~~~~~~~ + +Bootstrapping hooks called for plugins registered early enough (internal and setuptools plugins). + .. autofunction:: pytest_load_initial_conftests .. autofunction:: pytest_cmdline_preparse .. autofunction:: pytest_cmdline_parse -.. autofunction:: pytest_addoption .. autofunction:: pytest_cmdline_main + +Initialization hooks +~~~~~~~~~~~~~~~~~~~~ + +Initialization hooks called for plugins and ``conftest.py`` files. + +.. autofunction:: pytest_addoption .. autofunction:: pytest_configure .. autofunction:: pytest_unconfigure From cb6b851780ac010990c1404776c44841d01a8b84 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 4 Jan 2018 22:48:41 -0200 Subject: [PATCH 02/50] Update deprecation/removal docs to point to labels/milestones instead Using milestones and proper issues are a much saner way to handle these topics than keeping them in sync in a separate document --- doc/en/backwards-compatibility.rst | 89 +----------------------------- 1 file changed, 2 insertions(+), 87 deletions(-) diff --git a/doc/en/backwards-compatibility.rst b/doc/en/backwards-compatibility.rst index 84f2c43edaa..55506e7c338 100644 --- a/doc/en/backwards-compatibility.rst +++ b/doc/en/backwards-compatibility.rst @@ -15,91 +15,6 @@ We will only remove deprecated functionality in major releases (e.g. if we depre Deprecation Roadmap ------------------- -This page lists deprecated features and when we plan to remove them. It is important to list the feature, the version where it got deprecated and the version we plan to remove it. +We track deprecation and removal of features using milestones and the `deprecation `_ and `removal `_ labels on GitHub. -Following our deprecation policy, we should aim to keep features for *at least* two minor versions after it was considered deprecated. - - -Future Releases -~~~~~~~~~~~~~~~ - -3.4 -^^^ - -**Old style classes** - -Issue: `#2147 `_. - -Deprecated in ``3.2``. - -4.0 -^^^ - -**Yield tests** - -Deprecated in ``3.0``. - -**pytest-namespace hook** - -deprecated in ``3.2``. - -**Marks in parameter sets** - -Deprecated in ``3.2``. - -**--result-log** - -Deprecated in ``3.0``. - -See `#830 `_ for more information. Suggested alternative: `pytest-tap `_. - -**metafunc.addcall** - -Issue: `#2876 `_. - -Deprecated in ``3.3``. - -**pytest_plugins in non-toplevel conftests** - -There is a deep conceptual confusion as ``conftest.py`` files themselves are activated/deactivated based on path, but the plugins they depend on aren't. - -Issue: `#2639 `_. - -Not yet officially deprecated. - -**passing a single string to pytest.main()** - -Pass a list of strings to ``pytest.main()`` instead. - -Deprecated in ``3.1``. - -**[pytest] section in setup.cfg** - -Use ``[tool:pytest]`` instead for compatibility with other tools. - -Deprecated in ``3.0``. - -Past Releases -~~~~~~~~~~~~~ - -3.0 -^^^ - -* The following deprecated commandline options were removed: - - * ``--genscript``: no longer supported; - * ``--no-assert``: use ``--assert=plain`` instead; - * ``--nomagic``: use ``--assert=plain`` instead; - * ``--report``: use ``-r`` instead; - -* Removed all ``py.test-X*`` entry points. The versioned, suffixed entry points - were never documented and a leftover from a pre-virtualenv era. These entry - points also created broken entry points in wheels, so removing them also - removes a source of confusion for users. - - - -3.3 -^^^ - -* Dropped support for EOL Python 2.6 and 3.3. \ No newline at end of file +Following our deprecation policy, after starting issuing deprecation warnings we keep features for *at least* two minor versions before considering removal. From bd1d17e8de30f6d52f9c4b61df4c6403f8f4f61c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 26 Dec 2017 19:47:26 -0800 Subject: [PATCH 03/50] Replace py.std with stdlib imports --- _pytest/_code/code.py | 10 ++++---- _pytest/_code/source.py | 8 +++---- changelog/3067.trivial | 1 + doc/en/example/assertion/failure_demo.py | 4 ++-- doc/en/example/reportingdemo.rst | 4 ++-- extra/get_issues.py | 1 - testing/acceptance_test.py | 13 ++++++----- testing/code/test_excinfo.py | 18 ++++++++------- testing/code/test_source.py | 8 +++---- testing/python/collect.py | 5 ++-- testing/python/metafunc.py | 4 ++-- testing/test_argcomplete.py | 19 ++++++++-------- testing/test_assertrewrite.py | 5 ++-- testing/test_cache.py | 12 +++++----- testing/test_collection.py | 19 ++++++++-------- testing/test_config.py | 10 ++++---- testing/test_conftest.py | 6 ++--- testing/test_parseopt.py | 3 ++- testing/test_pluginmanager.py | 20 ++++++++-------- testing/test_recwarn.py | 29 ++++++++++++------------ testing/test_runner.py | 18 ++++++++------- testing/test_terminal.py | 6 ++--- 22 files changed, 117 insertions(+), 106 deletions(-) create mode 100644 changelog/3067.trivial diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py index 3fb232bd430..84627a435f4 100644 --- a/_pytest/_code/code.py +++ b/_pytest/_code/code.py @@ -1,5 +1,7 @@ from __future__ import absolute_import, division, print_function +import inspect import sys +import traceback from inspect import CO_VARARGS, CO_VARKEYWORDS import re from weakref import ref @@ -422,7 +424,7 @@ def getrepr(self, showlocals=False, style="long", """ if style == 'native': return ReprExceptionInfo(ReprTracebackNative( - py.std.traceback.format_exception( + traceback.format_exception( self.type, self.value, self.traceback[0]._rawentry, @@ -556,7 +558,7 @@ def repr_locals(self, locals): # else: # self._line("%-10s =\\" % (name,)) # # XXX - # py.std.pprint.pprint(value, stream=self.excinfowriter) + # pprint.pprint(value, stream=self.excinfowriter) return ReprLocals(lines) def repr_traceback_entry(self, entry, excinfo=None): @@ -669,7 +671,7 @@ def repr_excinfo(self, excinfo): else: # fallback to native repr if the exception doesn't have a traceback: # ExceptionInfo objects require a full traceback to work - reprtraceback = ReprTracebackNative(py.std.traceback.format_exception(type(e), e, None)) + reprtraceback = ReprTracebackNative(traceback.format_exception(type(e), e, None)) reprcrash = None repr_chain += [(reprtraceback, reprcrash, descr)] @@ -886,7 +888,7 @@ def getrawcode(obj, trycall=True): obj = getattr(obj, 'f_code', obj) obj = getattr(obj, '__code__', obj) if trycall and not hasattr(obj, 'co_firstlineno'): - if hasattr(obj, '__call__') and not py.std.inspect.isclass(obj): + if hasattr(obj, '__call__') and not inspect.isclass(obj): x = getrawcode(obj.__call__, trycall=False) if hasattr(x, 'co_firstlineno'): return x diff --git a/_pytest/_code/source.py b/_pytest/_code/source.py index 322c72afb2c..409961d9aeb 100644 --- a/_pytest/_code/source.py +++ b/_pytest/_code/source.py @@ -3,6 +3,7 @@ import ast from ast import PyCF_ONLY_AST as _AST_FLAG from bisect import bisect_right +import linecache import sys import six import inspect @@ -191,7 +192,7 @@ def compile(self, filename=None, mode='exec', if flag & _AST_FLAG: return co lines = [(x + "\n") for x in self.lines] - py.std.linecache.cache[filename] = (1, None, lines, filename) + linecache.cache[filename] = (1, None, lines, filename) return co # @@ -223,8 +224,7 @@ def getfslineno(obj): code = _pytest._code.Code(obj) except TypeError: try: - fn = (py.std.inspect.getsourcefile(obj) or - py.std.inspect.getfile(obj)) + fn = inspect.getsourcefile(obj) or inspect.getfile(obj) except TypeError: return "", -1 @@ -248,7 +248,7 @@ def getfslineno(obj): def findsource(obj): try: - sourcelines, lineno = py.std.inspect.findsource(obj) + sourcelines, lineno = inspect.findsource(obj) except py.builtin._sysex: raise except: # noqa diff --git a/changelog/3067.trivial b/changelog/3067.trivial new file mode 100644 index 00000000000..2b7185100ed --- /dev/null +++ b/changelog/3067.trivial @@ -0,0 +1 @@ +Replace py.std with stdlib imports. diff --git a/doc/en/example/assertion/failure_demo.py b/doc/en/example/assertion/failure_demo.py index d31fba2adaa..423bbeb931d 100644 --- a/doc/en/example/assertion/failure_demo.py +++ b/doc/en/example/assertion/failure_demo.py @@ -159,10 +159,10 @@ def func1(self): def test_dynamic_compile_shows_nicely(): src = 'def foo():\n assert 1 == 0\n' name = 'abc-123' - module = py.std.imp.new_module(name) + module = imp.new_module(name) code = _pytest._code.compile(src, name, 'exec') py.builtin.exec_(code, module.__dict__) - py.std.sys.modules[name] = module + sys.modules[name] = module module.foo() diff --git a/doc/en/example/reportingdemo.rst b/doc/en/example/reportingdemo.rst index 9edc02b3cd4..9964d67f289 100644 --- a/doc/en/example/reportingdemo.rst +++ b/doc/en/example/reportingdemo.rst @@ -413,10 +413,10 @@ get on the terminal - we are working on that):: def test_dynamic_compile_shows_nicely(): src = 'def foo():\n assert 1 == 0\n' name = 'abc-123' - module = py.std.imp.new_module(name) + module = imp.new_module(name) code = _pytest._code.compile(src, name, 'exec') py.builtin.exec_(code, module.__dict__) - py.std.sys.modules[name] = module + sys.modules[name] = module > module.foo() failure_demo.py:166: diff --git a/extra/get_issues.py b/extra/get_issues.py index 2a8f8c31606..99378b2f5a7 100644 --- a/extra/get_issues.py +++ b/extra/get_issues.py @@ -1,6 +1,5 @@ import json import py -import textwrap issues_url = "https://api.github.com/repos/pytest-dev/pytest/issues" diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 661aaa09ba9..e80049d5ca7 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function import os import sys +import types import six @@ -398,7 +399,7 @@ def test_plugins_given_as_strings(self, tmpdir, monkeypatch): p = tmpdir.join('test_test_plugins_given_as_strings.py') p.write('def test_foo(): pass') - mod = py.std.types.ModuleType("myplugin") + mod = types.ModuleType("myplugin") monkeypatch.setitem(sys.modules, 'myplugin', mod) assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0 @@ -492,17 +493,17 @@ def test_hello(): def test_python_minus_m_invocation_ok(self, testdir): p1 = testdir.makepyfile("def test_hello(): pass") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 def test_python_minus_m_invocation_fail(self, testdir): p1 = testdir.makepyfile("def test_fail(): 0/0") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 1 def test_python_pytest_package(self, testdir): p1 = testdir.makepyfile("def test_pass(): pass") - res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1)) + res = testdir.run(sys.executable, "-m", "pytest", str(p1)) assert res.ret == 0 res.stdout.fnmatch_lines(["*1 passed*"]) @@ -560,7 +561,7 @@ def test_cmdline_python_package(self, testdir, monkeypatch): ]) def join_pythonpath(what): - cur = py.std.os.environ.get('PYTHONPATH') + cur = os.environ.get('PYTHONPATH') if cur: return str(what) + os.pathsep + cur return what @@ -618,7 +619,7 @@ def test_cmdline_python_namespace_package(self, testdir, monkeypatch): # └── test_world.py def join_pythonpath(*dirs): - cur = py.std.os.environ.get('PYTHONPATH') + cur = os.environ.get('PYTHONPATH') if cur: dirs += (cur,) return os.pathsep.join(str(p) for p in dirs) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 58b1b1b67e1..6b4adf0017e 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -2,6 +2,8 @@ from __future__ import absolute_import, division, print_function import operator +import os +import sys import _pytest import py import pytest @@ -472,7 +474,7 @@ def test_repr_source_not_existing(self): excinfo = _pytest._code.ExceptionInfo() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_many_line_source_not_existing(self): @@ -487,7 +489,7 @@ def test_repr_many_line_source_not_existing(self): excinfo = _pytest._code.ExceptionInfo() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" def test_repr_source_failing_fullsource(self): @@ -545,13 +547,13 @@ class FakeRawTB(object): fail = IOError() repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" fail = py.error.ENOENT # noqa repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" def test_repr_local(self): @@ -738,7 +740,7 @@ def entry(): repr = p.repr_excinfo(excinfo) assert repr.reprtraceback assert len(repr.reprtraceback.reprentries) == len(reprtb.reprentries) - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert repr.chain[0][0] assert len(repr.chain[0][0].reprentries) == len(reprtb.reprentries) assert repr.reprcrash.path.endswith("mod.py") @@ -758,7 +760,7 @@ def entry(): def raiseos(): raise OSError(2) - monkeypatch.setattr(py.std.os, 'getcwd', raiseos) + monkeypatch.setattr(os, 'getcwd', raiseos) assert p._makepath(__file__) == __file__ p.repr_traceback(excinfo) @@ -816,10 +818,10 @@ def entry(): for style in ("short", "long", "no"): for showlocals in (True, False): repr = excinfo.getrepr(style=style, showlocals=showlocals) - if py.std.sys.version_info[0] < 3: + if sys.version_info[0] < 3: assert isinstance(repr, ReprExceptionInfo) assert repr.reprtraceback.style == style - if py.std.sys.version_info[0] >= 3: + if sys.version_info[0] >= 3: assert isinstance(repr, ExceptionChainRepr) for repr in repr.chain: assert repr[0].style == style diff --git a/testing/code/test_source.py b/testing/code/test_source.py index fcce3fa9614..ee731ed4f85 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -2,6 +2,7 @@ # disable flake check on this file because some constructs are strange # or redundant on purpose and can't be disable on a line-by-line basis from __future__ import absolute_import, division, print_function +import inspect import sys import _pytest._code @@ -187,9 +188,9 @@ def f(): def f(): raise ValueError() """) - source1 = py.std.inspect.getsource(co1) + source1 = inspect.getsource(co1) assert 'KeyError' in source1 - source2 = py.std.inspect.getsource(co2) + source2 = inspect.getsource(co2) assert 'ValueError' in source2 def test_getstatement(self): @@ -373,7 +374,6 @@ def f(): c = '''while True: pass ''' - import inspect lines = deindent(inspect.getsource(f).splitlines()) assert lines == ["def f():", " c = '''while True:", " pass", "'''"] @@ -461,7 +461,7 @@ class A(object): fspath, lineno = getfslineno(A) - _, A_lineno = py.std.inspect.findsource(A) + _, A_lineno = inspect.findsource(A) assert fspath.basename == "test_source.py" assert lineno == A_lineno diff --git a/testing/python/collect.py b/testing/python/collect.py index 16c2154b8c1..3e00ae9614a 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -4,7 +4,6 @@ from textwrap import dedent import _pytest._code -import py import pytest from _pytest.main import ( Collector, @@ -25,7 +24,7 @@ def test_import_duplicate(self, testdir): b = testdir.mkdir("b") p = a.ensure("test_whatever.py") p.pyimport() - del py.std.sys.modules['test_whatever'] + del sys.modules['test_whatever'] b.ensure("test_whatever.py") result = testdir.runpytest() result.stdout.fnmatch_lines([ @@ -754,7 +753,7 @@ def test_fail(): assert 0 assert fn1 == fn2 assert fn1 != modcol - if py.std.sys.version_info < (3, 0): + if sys.version_info < (3, 0): assert cmp(fn1, fn2) == 0 assert hash(fn1) == hash(fn2) diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 2ffb7bb5da2..a173f77392f 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -730,7 +730,7 @@ class TestMetafuncFunctional(object): def test_attributes(self, testdir): p = testdir.makepyfile(""" # assumes that generate/provide runs in the same process - import py, pytest + import sys, pytest def pytest_generate_tests(metafunc): metafunc.addcall(param=metafunc) @@ -749,7 +749,7 @@ class TestClass(object): def test_method(self, metafunc, pytestconfig): assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ - if py.std.sys.version_info > (3, 0): + if sys.version_info > (3, 0): unbound = TestClass.test_method else: unbound = TestClass.test_method.im_func diff --git a/testing/test_argcomplete.py b/testing/test_argcomplete.py index c9261257743..7a5e25d69bb 100644 --- a/testing/test_argcomplete.py +++ b/testing/test_argcomplete.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, division, print_function -import py +import subprocess +import sys import pytest # test for _argcomplete but not specific for any application @@ -23,21 +24,21 @@ def equal_with_bash(prefix, ffc, fc, out=None): def _wrapcall(*args, **kargs): try: - if py.std.sys.version_info > (2, 7): - return py.std.subprocess.check_output(*args, **kargs).decode().splitlines() + if sys.version_info > (2, 7): + return subprocess.check_output(*args, **kargs).decode().splitlines() if 'stdout' in kargs: raise ValueError('stdout argument not allowed, it will be overridden.') - process = py.std.subprocess.Popen( - stdout=py.std.subprocess.PIPE, *args, **kargs) + process = subprocess.Popen( + stdout=subprocess.PIPE, *args, **kargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kargs.get("args") if cmd is None: cmd = args[0] - raise py.std.subprocess.CalledProcessError(retcode, cmd) + raise subprocess.CalledProcessError(retcode, cmd) return output.decode().splitlines() - except py.std.subprocess.CalledProcessError: + except subprocess.CalledProcessError: return [] @@ -83,7 +84,7 @@ def test_compare_with_compgen(self): ffc = FastFilesCompleter() fc = FilesCompleter() for x in ['/', '/d', '/data', 'qqq', '']: - assert equal_with_bash(x, ffc, fc, out=py.std.sys.stdout) + assert equal_with_bash(x, ffc, fc, out=sys.stdout) @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_remove_dir_prefix(self): @@ -94,4 +95,4 @@ def test_remove_dir_prefix(self): ffc = FastFilesCompleter() fc = FilesCompleter() for x in '/usr/'.split(): - assert not equal_with_bash(x, ffc, fc, out=py.std.sys.stdout) + assert not equal_with_bash(x, ffc, fc, out=sys.stdout) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 77cfd2900d1..2a3dbc2ecc3 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -5,6 +5,7 @@ import py_compile import stat import sys +import textwrap import zipfile import py import pytest @@ -911,7 +912,7 @@ def test_read_pyc(self, tmpdir): def test_reload_is_same(self, testdir): # A file that will be picked up during collecting. testdir.tmpdir.join("file.py").ensure() - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" + testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(""" [pytest] python_files = *.py """)) @@ -997,7 +998,7 @@ def test_simple(self, testdir): def test_simple_failure(): assert 1 + 1 == 3 """) - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" + testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(""" [pytest] python_files = tests/**.py """)) diff --git a/testing/test_cache.py b/testing/test_cache.py index a37170cdd2b..e3fa2ebb1ad 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -410,8 +410,8 @@ def test_a2(): def test_lastfailed_collectfailure(self, testdir, monkeypatch): testdir.makepyfile(test_maybe=""" - import py - env = py.std.os.environ + import os + env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): @@ -439,8 +439,8 @@ def rlf(fail_import, fail_run): def test_lastfailed_failure_subset(self, testdir, monkeypatch): testdir.makepyfile(test_maybe=""" - import py - env = py.std.os.environ + import os + env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): @@ -448,8 +448,8 @@ def test_hello(): """) testdir.makepyfile(test_maybe2=""" - import py - env = py.std.os.environ + import os + env = os.environ if '1' == env['FAILIMPORT']: raise ImportError('fail') def test_hello(): diff --git a/testing/test_collection.py b/testing/test_collection.py index 563ed0439c0..f2d542c62dd 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, division, print_function +import pprint +import sys import pytest -import py import _pytest._code from _pytest.main import Session, EXIT_NOTESTSCOLLECTED, _in_venv @@ -36,7 +37,7 @@ def test_fail(): assert 0 assert fn1 == fn2 assert fn1 != modcol - if py.std.sys.version_info < (3, 0): + if sys.version_info < (3, 0): assert cmp(fn1, fn2) == 0 assert hash(fn1) == hash(fn2) @@ -128,7 +129,7 @@ def test_ignored_certain_directories(self, testdir): ("activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1")) def test_ignored_virtualenvs(self, testdir, fname): - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" testdir.tmpdir.ensure("virtual", bindir, fname) testfile = testdir.tmpdir.ensure("virtual", "test_invenv.py") testfile.write("def test_hello(): pass") @@ -147,7 +148,7 @@ def test_ignored_virtualenvs(self, testdir, fname): ("activate", "activate.csh", "activate.fish", "Activate", "Activate.bat", "Activate.ps1")) def test_ignored_virtualenvs_norecursedirs_precedence(self, testdir, fname): - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" # norecursedirs takes priority testdir.tmpdir.ensure(".virtual", bindir, fname) testfile = testdir.tmpdir.ensure(".virtual", "test_invenv.py") @@ -163,7 +164,7 @@ def test_ignored_virtualenvs_norecursedirs_precedence(self, testdir, fname): "Activate", "Activate.bat", "Activate.ps1")) def test__in_venv(self, testdir, fname): """Directly test the virtual env detection function""" - bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" + bindir = "Scripts" if sys.platform.startswith("win") else "bin" # no bin/activate, not a virtualenv base_path = testdir.tmpdir.mkdir('venv') assert _in_venv(base_path) is False @@ -436,7 +437,7 @@ def test_collect_protocol_single_function(self, testdir): assert item.name == "test_func" newid = item.nodeid assert newid == id - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) topdir = testdir.tmpdir # noqa hookrec.assert_contains([ ("pytest_collectstart", "collector.fspath == topdir"), @@ -486,7 +487,7 @@ def pytest_collect_file(path, parent): id = p.basename items, hookrec = testdir.inline_genitems(id) - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) assert len(items) == 2 hookrec.assert_contains([ ("pytest_collectstart", @@ -508,7 +509,7 @@ def test_collect_subdir_event_ordering(self, testdir): items, hookrec = testdir.inline_genitems() assert len(items) == 1 - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) hookrec.assert_contains([ ("pytest_collectstart", "collector.fspath == test_aaa"), ("pytest_pycollect_makeitem", "name == 'test_func'"), @@ -529,7 +530,7 @@ def test_collect_two_commandline_args(self, testdir): items, hookrec = testdir.inline_genitems(id) assert len(items) == 2 - py.std.pprint.pprint(hookrec.calls) + pprint.pprint(hookrec.calls) hookrec.assert_contains([ ("pytest_collectstart", "collector.fspath == test_aaa"), ("pytest_pycollect_makeitem", "name == 'test_func'"), diff --git a/testing/test_config.py b/testing/test_config.py index 44b8c317a28..5202b21b98e 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1,6 +1,6 @@ from __future__ import absolute_import, division, print_function import sys -import py +import textwrap import pytest import _pytest._code @@ -57,7 +57,7 @@ def test_tox_ini_wrong_version(self, testdir): ('pytest', 'pytest.ini')], ) def test_ini_names(self, testdir, name, section): - testdir.tmpdir.join(name).write(py.std.textwrap.dedent(""" + testdir.tmpdir.join(name).write(textwrap.dedent(""" [{section}] minversion = 1.0 """.format(section=section))) @@ -66,11 +66,11 @@ def test_ini_names(self, testdir, name, section): def test_toxini_before_lower_pytestini(self, testdir): sub = testdir.tmpdir.mkdir("sub") - sub.join("tox.ini").write(py.std.textwrap.dedent(""" + sub.join("tox.ini").write(textwrap.dedent(""" [pytest] minversion = 2.0 """)) - testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent(""" + testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(""" [pytest] minversion = 1.5 """)) @@ -731,7 +731,7 @@ def test_with_specific_inifile(self, tmpdir): class TestOverrideIniArgs(object): @pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split()) def test_override_ini_names(self, testdir, name): - testdir.tmpdir.join(name).write(py.std.textwrap.dedent(""" + testdir.tmpdir.join(name).write(textwrap.dedent(""" [pytest] custom = 1.0""")) testdir.makeconftest(""" diff --git a/testing/test_conftest.py b/testing/test_conftest.py index f0f187606c2..6566f752ab3 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -232,7 +232,7 @@ def test_fixture_dependency(testdir, monkeypatch): ct1.write("") sub = testdir.mkdir("sub") sub.join("__init__.py").write("") - sub.join("conftest.py").write(py.std.textwrap.dedent(""" + sub.join("conftest.py").write(dedent(""" import pytest @pytest.fixture @@ -249,7 +249,7 @@ def bar(foo): """)) subsub = sub.mkdir("subsub") subsub.join("__init__.py").write("") - subsub.join("test_bar.py").write(py.std.textwrap.dedent(""" + subsub.join("test_bar.py").write(dedent(""" import pytest @pytest.fixture @@ -265,7 +265,7 @@ def test_event_fixture(bar): def test_conftest_found_with_double_dash(testdir): sub = testdir.mkdir("sub") - sub.join("conftest.py").write(py.std.textwrap.dedent(""" + sub.join("conftest.py").write(dedent(""" def pytest_addoption(parser): parser.addoption("--hello-world", action="store_true") """)) diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index 92159257019..55983bbb1f8 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -1,4 +1,5 @@ from __future__ import absolute_import, division, print_function +import argparse import sys import os import py @@ -189,7 +190,7 @@ def defaultget(option): assert option.no is False def test_drop_short_helper(self): - parser = py.std.argparse.ArgumentParser(formatter_class=parseopt.DropShorterLongHelpFormatter) + parser = argparse.ArgumentParser(formatter_class=parseopt.DropShorterLongHelpFormatter) parser.add_argument('-t', '--twoword', '--duo', '--two-word', '--two', help='foo').map_long_option = {'two': 'two-word'} # throws error on --deux only! diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 6192176e8a8..503ba8454c1 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -1,8 +1,10 @@ # encoding: UTF-8 from __future__ import absolute_import, division, print_function import pytest -import py import os +import re +import sys +import types from _pytest.config import get_config, PytestPluginManager from _pytest.main import EXIT_NOTESTSCOLLECTED, Session @@ -208,14 +210,14 @@ def test_traceback(): expected_message = '.*Error importing plugin "qwe": Not possible to import: .' expected_traceback = ".*in test_traceback" - assert py.std.re.match(expected_message, str(excinfo.value)) - assert py.std.re.match(expected_traceback, str(excinfo.traceback[-1])) + assert re.match(expected_message, str(excinfo.value)) + assert re.match(expected_traceback, str(excinfo.traceback[-1])) class TestPytestPluginManager(object): def test_register_imported_modules(self): pm = PytestPluginManager() - mod = py.std.types.ModuleType("x.y.pytest_hello") + mod = types.ModuleType("x.y.pytest_hello") pm.register(mod) assert pm.is_registered(mod) values = pm.get_plugins() @@ -226,8 +228,8 @@ def test_register_imported_modules(self): assert pm.get_plugins() == values def test_canonical_import(self, monkeypatch): - mod = py.std.types.ModuleType("pytest_xyz") - monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod) + mod = types.ModuleType("pytest_xyz") + monkeypatch.setitem(sys.modules, 'pytest_xyz', mod) pm = PytestPluginManager() pm.import_plugin('pytest_xyz') assert pm.get_plugin('pytest_xyz') == mod @@ -237,7 +239,7 @@ def test_consider_module(self, testdir, pytestpm): testdir.syspathinsert() testdir.makepyfile(pytest_p1="#") testdir.makepyfile(pytest_p2="#") - mod = py.std.types.ModuleType("temp") + mod = types.ModuleType("temp") mod.pytest_plugins = ["pytest_p1", "pytest_p2"] pytestpm.consider_module(mod) assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1" @@ -245,12 +247,12 @@ def test_consider_module(self, testdir, pytestpm): def test_consider_module_import_module(self, testdir): pytestpm = get_config().pluginmanager - mod = py.std.types.ModuleType("x") + mod = types.ModuleType("x") mod.pytest_plugins = "pytest_a" aplugin = testdir.makepyfile(pytest_a="#") reprec = testdir.make_hook_recorder(pytestpm) # syspath.prepend(aplugin.dirpath()) - py.std.sys.path.insert(0, str(aplugin.dirpath())) + sys.path.insert(0, str(aplugin.dirpath())) pytestpm.consider_module(mod) call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name) assert call.plugin.__name__ == "pytest_a" diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 31e70460fe4..0c80c169eeb 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -1,7 +1,6 @@ from __future__ import absolute_import, division, print_function import warnings import re -import py import pytest from _pytest.recwarn import WarningsRecorder @@ -24,9 +23,9 @@ def test_recording(self): rec = WarningsRecorder() with rec: assert not rec.list - py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13) + warnings.warn_explicit("hello", UserWarning, "xyz", 13) assert len(rec.list) == 1 - py.std.warnings.warn(DeprecationWarning("hello")) + warnings.warn(DeprecationWarning("hello")) assert len(rec.list) == 2 warn = rec.pop() assert str(warn.message) == "hello" @@ -64,14 +63,14 @@ class TestDeprecatedCall(object): def dep(self, i, j=None): if i == 0: - py.std.warnings.warn("is deprecated", DeprecationWarning, - stacklevel=1) + warnings.warn("is deprecated", DeprecationWarning, + stacklevel=1) return 42 def dep_explicit(self, i): if i == 0: - py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning, - filename="hello", lineno=3) + warnings.warn_explicit("dep_explicit", category=DeprecationWarning, + filename="hello", lineno=3) def test_deprecated_call_raises(self): with pytest.raises(AssertionError) as excinfo: @@ -86,16 +85,16 @@ def test_deprecated_call_ret(self): assert ret == 42 def test_deprecated_call_preserves(self): - onceregistry = py.std.warnings.onceregistry.copy() - filters = py.std.warnings.filters[:] - warn = py.std.warnings.warn - warn_explicit = py.std.warnings.warn_explicit + onceregistry = warnings.onceregistry.copy() + filters = warnings.filters[:] + warn = warnings.warn + warn_explicit = warnings.warn_explicit self.test_deprecated_call_raises() self.test_deprecated_call() - assert onceregistry == py.std.warnings.onceregistry - assert filters == py.std.warnings.filters - assert warn is py.std.warnings.warn - assert warn_explicit is py.std.warnings.warn_explicit + assert onceregistry == warnings.onceregistry + assert filters == warnings.filters + assert warn is warnings.warn + assert warn_explicit is warnings.warn_explicit def test_deprecated_explicit_call_raises(self): with pytest.raises(AssertionError): diff --git a/testing/test_runner.py b/testing/test_runner.py index a8dc8dfbde1..0c82cd474b3 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -2,10 +2,12 @@ from __future__ import absolute_import, division, print_function import _pytest._code +import inspect import os import py import pytest import sys +import types from _pytest import runner, main, outcomes @@ -392,10 +394,10 @@ class TestClass(object): @pytest.mark.parametrize('reporttype', reporttypes, ids=[x.__name__ for x in reporttypes]) def test_report_extra_parameters(reporttype): - if hasattr(py.std.inspect, 'signature'): - args = list(py.std.inspect.signature(reporttype.__init__).parameters.keys())[1:] + if hasattr(inspect, 'signature'): + args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:] else: - args = py.std.inspect.getargspec(reporttype.__init__)[0][1:] + args = inspect.getargspec(reporttype.__init__)[0][1:] basekw = dict.fromkeys(args, []) report = reporttype(newthing=1, **basekw) assert report.newthing == 1 @@ -564,10 +566,10 @@ def f(): importorskip("asdlkj") try: - sys = importorskip("sys") - assert sys == py.std.sys + sysmod = importorskip("sys") + assert sysmod is sys # path = pytest.importorskip("os.path") - # assert path == py.std.os.path + # assert path == os.path excinfo = pytest.raises(pytest.skip.Exception, f) path = py.path.local(excinfo.getrepr().reprcrash.path) # check that importorskip reports the actual call @@ -575,7 +577,7 @@ def f(): assert path.purebasename == "test_runner" pytest.raises(SyntaxError, "pytest.importorskip('x y z')") pytest.raises(SyntaxError, "pytest.importorskip('x=y')") - mod = py.std.types.ModuleType("hello123") + mod = types.ModuleType("hello123") mod.__version__ = "1.3" monkeypatch.setitem(sys.modules, "hello123", mod) pytest.raises(pytest.skip.Exception, """ @@ -595,7 +597,7 @@ def test_importorskip_imports_last_module_part(): def test_importorskip_dev_module(monkeypatch): try: - mod = py.std.types.ModuleType("mockmodule") + mod = types.ModuleType("mockmodule") mod.__version__ = '0.13.0.dev-43290' monkeypatch.setitem(sys.modules, 'mockmodule', mod) mod2 = pytest.importorskip('mockmodule', minversion='0.12.0') diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 97c2f71fb66..a515fc703f4 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -326,7 +326,7 @@ def test_repr_python_version(monkeypatch): try: monkeypatch.setattr(sys, 'version_info', (2, 5, 1, 'final', 0)) assert repr_pythonversion() == "2.5.1-final-0" - py.std.sys.version_info = x = (2, 3) + sys.version_info = x = (2, 3) assert repr_pythonversion() == str(x) finally: monkeypatch.undo() # do this early as pytest can get confused @@ -475,11 +475,11 @@ def test_passes(): pass """) result = testdir.runpytest() - verinfo = ".".join(map(str, py.std.sys.version_info[:3])) + verinfo = ".".join(map(str, sys.version_info[:3])) result.stdout.fnmatch_lines([ "*===== test session starts ====*", "platform %s -- Python %s*pytest-%s*py-%s*pluggy-%s" % ( - py.std.sys.platform, verinfo, + sys.platform, verinfo, pytest.__version__, py.__version__, pluggy.__version__), "*test_header_trailer_info.py .*", "=* 1 passed*in *.[0-9][0-9] seconds *=", From 820ea6d68f96ed320af5df14ecedbf20a86ed5b2 Mon Sep 17 00:00:00 2001 From: "Per A. Brodtkorb" Date: Wed, 10 Jan 2018 16:44:26 +0100 Subject: [PATCH 04/50] Update Freezing pytest description in simple.rst I have trouble using third party plugins in my frozen program and discovered that other people have experienced it as well: https://mail.python.org/pipermail//pytest-dev/2015-June/003015.html The problem is that the mechanism for plugin discovery used by pytest (setupttools entry points) doesn't work with frozen executables so pytest can't find any plugins. The solution seems to be to import the third party plugins explicitly as shown here: https://mail.python.org/pipermail//pytest-dev/2015-June/003018.html This is not mentioned anywhere in the documentaion. --- doc/en/example/simple.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 9c773aaedbd..5dbf0a5198b 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -826,15 +826,20 @@ Instead of freezing the pytest runner as a separate executable, you can make your frozen program work as the pytest runner by some clever argument handling during program startup. This allows you to have a single executable, which is usually more convenient. +Please note that the mechanism for plugin discovery used by pytest +(setupttools entry points) doesn't work with frozen executables so pytest +can't find any third party plugins automatically. To include third party plugins +like ``pytest-timeout`` they must be imported explicitly and passed on to pytest.main. .. code-block:: python # contents of app_main.py import sys + import pytest_timeout # Third party plugin if len(sys.argv) > 1 and sys.argv[1] == '--pytest': import pytest - sys.exit(pytest.main(sys.argv[2:])) + sys.exit(pytest.main(sys.argv[2:], plugins=[pytest_timeout])) else: # normal application execution: at this point argv can be parsed # by your argument-parsing library of choice as usual @@ -845,3 +850,4 @@ This allows you to execute tests using the frozen application with standard ``pytest`` command-line options:: ./app_main --pytest --verbose --tb=long --junitxml=results.xml test-suite/ + From ee6c9f50a25e8bf2425968afe6d7ed0f359a6d17 Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 10 Jan 2018 15:13:42 -0800 Subject: [PATCH 05/50] optimize fixtures.reorder_items --- _pytest/fixtures.py | 90 +++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index a2b6f7d9460..d8938341903 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -4,7 +4,7 @@ import inspect import sys import warnings -from collections import OrderedDict +from collections import OrderedDict, deque, defaultdict import attr import py @@ -163,62 +163,56 @@ def get_parametrized_fixture_keys(item, scopenum): def reorder_items(items): argkeys_cache = {} + items_by_argkey = {} for scopenum in range(0, scopenum_function): argkeys_cache[scopenum] = d = {} + items_by_argkey[scopenum] = item_d = defaultdict(list) for item in items: keys = OrderedDict.fromkeys(get_parametrized_fixture_keys(item, scopenum)) if keys: d[item] = keys - return reorder_items_atscope(items, set(), argkeys_cache, 0) + for key in keys: + item_d[key].append(item) + + return list(reorder_items_atscope(items, set(), argkeys_cache, items_by_argkey, 0)) + - -def reorder_items_atscope(items, ignore, argkeys_cache, scopenum): +def reorder_items_atscope(items, ignore, argkeys_cache, items_by_argkey, scopenum): if scopenum >= scopenum_function or len(items) < 3: return items - items_done = [] - while 1: - items_before, items_same, items_other, newignore = \ - slice_items(items, ignore, argkeys_cache[scopenum]) - items_before = reorder_items_atscope( - items_before, ignore, argkeys_cache, scopenum + 1) - if items_same is None: - # nothing to reorder in this scope - assert items_other is None - return items_done + items_before - items_done.extend(items_before) - items = items_same + items_other - ignore = newignore - - -def slice_items(items, ignore, scoped_argkeys_cache): - # we pick the first item which uses a fixture instance in the - # requested scope and which we haven't seen yet. We slice the input - # items list into a list of items_nomatch, items_same and - # items_other - if scoped_argkeys_cache: # do we need to do work at all? - it = iter(items) - # first find a slicing key - for i, item in enumerate(it): - argkeys = scoped_argkeys_cache.get(item) - if argkeys is not None: - newargkeys = OrderedDict.fromkeys(k for k in argkeys if k not in ignore) - if newargkeys: # found a slicing key - slicing_argkey, _ = newargkeys.popitem() - items_before = items[:i] - items_same = [item] - items_other = [] - # now slice the remainder of the list - for item in it: - argkeys = scoped_argkeys_cache.get(item) - if argkeys and slicing_argkey in argkeys and \ - slicing_argkey not in ignore: - items_same.append(item) - else: - items_other.append(item) - newignore = ignore.copy() - newignore.add(slicing_argkey) - return (items_before, items_same, items_other, newignore) - return items, None, None, None + items = deque(items) + items_done = OrderedDict() + scoped_items_by_argkey = items_by_argkey[scopenum] + scoped_argkeys_cache = argkeys_cache[scopenum] + while items: + + no_argkey_group = OrderedDict() + slicing_argkey = None + + while items: + item = items.popleft() + if item in items_done: + continue + + argkeys = OrderedDict.fromkeys(k for k in scoped_argkeys_cache.get(item, ()) if k not in ignore) + if not argkeys: + no_argkey_group[item] = None + + else: + slicing_argkey, _ = argkeys.popitem() + #we don't have to remove relevant items from later in the deque because they'll just be ignored + items.extendleft(reversed(scoped_items_by_argkey[slicing_argkey])) + break + + if no_argkey_group: + no_argkey_group = reorder_items_atscope( + no_argkey_group, set(), argkeys_cache, items_by_argkey, scopenum + 1) + for item in no_argkey_group: + items_done[item] = None + + ignore.add(slicing_argkey) + + return items_done def fillfixtures(function): From 4a704bbb55191ccd10b22c4ea4cf8737e63f8e51 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 11 Jan 2018 10:25:02 -0800 Subject: [PATCH 06/50] fix reorder_items_atscope ordering --- _pytest/fixtures.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index d8938341903..04a5e8e8565 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -173,45 +173,40 @@ def reorder_items(items): d[item] = keys for key in keys: item_d[key].append(item) - + items = OrderedDict.fromkeys(items) return list(reorder_items_atscope(items, set(), argkeys_cache, items_by_argkey, 0)) - + def reorder_items_atscope(items, ignore, argkeys_cache, items_by_argkey, scopenum): if scopenum >= scopenum_function or len(items) < 3: return items - items = deque(items) + items_deque = deque(items) items_done = OrderedDict() scoped_items_by_argkey = items_by_argkey[scopenum] scoped_argkeys_cache = argkeys_cache[scopenum] - while items: - + while items_deque: no_argkey_group = OrderedDict() slicing_argkey = None - - while items: - item = items.popleft() - if item in items_done: + while items_deque: + item = items_deque.popleft() + if item in items_done or item in no_argkey_group: continue - - argkeys = OrderedDict.fromkeys(k for k in scoped_argkeys_cache.get(item, ()) if k not in ignore) + argkeys = OrderedDict.fromkeys(k for k in scoped_argkeys_cache.get(item, []) if k not in ignore) if not argkeys: no_argkey_group[item] = None - else: slicing_argkey, _ = argkeys.popitem() - #we don't have to remove relevant items from later in the deque because they'll just be ignored - items.extendleft(reversed(scoped_items_by_argkey[slicing_argkey])) + # we don't have to remove relevant items from later in the deque because they'll just be ignored + for i in reversed(scoped_items_by_argkey[slicing_argkey]): + if i in items: + items_deque.appendleft(i) break - if no_argkey_group: no_argkey_group = reorder_items_atscope( no_argkey_group, set(), argkeys_cache, items_by_argkey, scopenum + 1) for item in no_argkey_group: items_done[item] = None - ignore.add(slicing_argkey) - return items_done From 3d289b803d24d21424b94333c10975748e00e0bb Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 11 Jan 2018 12:25:41 -0800 Subject: [PATCH 07/50] update AUTHORS and changelog --- AUTHORS | 1 + changelog/3107.feature | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/3107.feature diff --git a/AUTHORS b/AUTHORS index 862378be9f5..f677a6b5f65 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,6 +3,7 @@ merlinux GmbH, Germany, office at merlinux eu Contributors include:: +Aaron Coleman Abdeali JK Abhijeet Kasurde Ahn Ki-Wook diff --git a/changelog/3107.feature b/changelog/3107.feature new file mode 100644 index 00000000000..6692fad7876 --- /dev/null +++ b/changelog/3107.feature @@ -0,0 +1 @@ +Optimize reorder_items in fixtures.py. \ No newline at end of file From d314691fd3c8bb5b10d96d28777fa2e5ee506bcc Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 12 Jan 2018 10:29:18 -0800 Subject: [PATCH 08/50] more descriptive changelog message --- changelog/3107.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3107.feature b/changelog/3107.feature index 6692fad7876..3a2e4e89242 100644 --- a/changelog/3107.feature +++ b/changelog/3107.feature @@ -1 +1 @@ -Optimize reorder_items in fixtures.py. \ No newline at end of file +Improve performance when collecting tests using many fixtures. \ No newline at end of file From 4a3863c2e28df69b59315a14b3a2ae4767d07ef9 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Sun, 14 Jan 2018 23:00:23 +0200 Subject: [PATCH 09/50] use flush in order to avoid hanging on mac --- testing/test_pdb.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/test_pdb.py b/testing/test_pdb.py index a565cbd0df2..8618473bb15 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -402,5 +402,4 @@ def test_foo(): child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1)) child.expect('custom set_trace>') - if child.isalive(): - child.wait() + self.flush(child) From 0a0d97aeb5eac43101b6f3f8a4b7d93f27464568 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Sun, 14 Jan 2018 23:14:59 +0200 Subject: [PATCH 10/50] added changelog news fragment --- changelog/2022.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2022.bugfix diff --git a/changelog/2022.bugfix b/changelog/2022.bugfix new file mode 100644 index 00000000000..67c9c8f7731 --- /dev/null +++ b/changelog/2022.bugfix @@ -0,0 +1 @@ +Fixed hanging pexpect test on MacOS by using flush() instead of wait(). \ No newline at end of file From 1f4831a23fdfab060006f65bb4fa891167374f2f Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Mon, 15 Jan 2018 12:28:21 -0800 Subject: [PATCH 11/50] Update getting-started.rst --- doc/en/getting-started.rst | 86 ++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 50 deletions(-) diff --git a/doc/en/getting-started.rst b/doc/en/getting-started.rst index 64b0108262d..d4847d59b38 100644 --- a/doc/en/getting-started.rst +++ b/doc/en/getting-started.rst @@ -7,32 +7,34 @@ Installation and Getting Started **PyPI package name**: `pytest `_ -**dependencies**: `py `_, +**Dependencies**: `py `_, `colorama (Windows) `_, -**documentation as PDF**: `download latest `_ +**Documentation as PDF**: `download latest `_ + +``pytest`` is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. Get started in minutes with a small unit test or complex functional test for your application or library. .. _`getstarted`: -.. _installation: +.. _`installpytest`: -Installation +Install ``pytest`` ---------------------------------------- -Installation:: +1. Run the following command in your command line:: pip install -U pytest -To check your installation has installed the correct version:: +2. Check that you installed the correct version:: $ pytest --version This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py -.. _`simpletest`: +.. _`createyourfirsttest`: -Our first test run +Create your first test ---------------------------------------------------------- -Let's create a first test file with a simple test function:: +Create a simple test function with just four lines of code:: # content of test_sample.py def func(x): @@ -41,7 +43,7 @@ Let's create a first test file with a simple test function:: def test_answer(): assert func(3) == 5 -That's it. You can execute the test function now:: +That’s it. You can now execute the test function:: $ pytest =========================== test session starts ============================ @@ -62,30 +64,26 @@ That's it. You can execute the test function now:: test_sample.py:5: AssertionError ========================= 1 failed in 0.12 seconds ========================= -We got a failure report because our little ``func(3)`` call did not return ``5``. +This test returns a failure report because ``func(3)`` does not return ``5``. .. note:: - You can simply use the ``assert`` statement for asserting test - expectations. pytest's :ref:`assert introspection` will intelligently - report intermediate values of the assert expression freeing - you from the need to learn the many names of `JUnit legacy methods`_. + You can use the ``assert`` statement to verify test expectations. pytest’s :ref:`Advanced assertion introspection` will intelligently report intermediate values of the assert expression so you can avoid the many names `of JUnit legacy methods`_. .. _`JUnit legacy methods`: http://docs.python.org/library/unittest.html#test-cases -.. _`assert statement`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement +.. _`Advanced assertion introspection`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement -Running multiple tests +Run multiple tests ---------------------------------------------------------- -``pytest`` will run all files in the current directory and its subdirectories of the form test_*.py or \*_test.py. More generally, it follows :ref:`standard test discovery rules `. +``pytest`` will run all files of the form test_*.py or \*_test.py in the current directory and its subdirectories. More generally, it follows :ref:`standard test discovery rules `. -Asserting that a certain exception is raised +Assert that a certain exception is raised -------------------------------------------------------------- -If you want to assert that some code raises an exception you can -use the ``raises`` helper:: +Use the ``raises`` helper to assert that some code raises an exception:: # content of test_sysexit.py import pytest @@ -96,18 +94,16 @@ use the ``raises`` helper:: with pytest.raises(SystemExit): f() -Running it with, this time in "quiet" reporting mode:: +Execute the test function with “quiet” reporting mode:: $ pytest -q test_sysexit.py . [100%] 1 passed in 0.12 seconds -Grouping multiple tests in a class +Group multiple tests in a class -------------------------------------------------------------- -Once you start to have more than a few tests it often makes sense -to group tests logically, in classes and modules. Let's write a class -containing two tests:: +Once you develop multiple tests, you may want to group them into a class. pytest makes it easy to create a class containing more than one test:: # content of test_class.py class TestClass(object): @@ -119,9 +115,7 @@ containing two tests:: x = "hello" assert hasattr(x, 'check') -The two tests are found because of the standard :ref:`test discovery`. -There is no need to subclass anything. We can simply -run the module by passing its filename:: +``pytest`` discovers all tests following its :ref:`Conventions for Python test discovery `, so it finds both ``test_`` prefixed functions. There is no need to subclass anything. We can simply run the module by passing its filename: $ pytest -q test_class.py .F [100%] @@ -139,26 +133,19 @@ run the module by passing its filename:: test_class.py:8: AssertionError 1 failed, 1 passed in 0.12 seconds -The first test passed, the second failed. Again we can easily see -the intermediate values used in the assertion, helping us to -understand the reason for the failure. +The first test passed and the second failed. You can easily see the intermediate values in the assertion to help you understand the reason for the failure. -Going functional: requesting a unique temporary directory +Request a unique temporary directory for functional tests -------------------------------------------------------------- -For functional tests one often needs to create some files -and pass them to application objects. pytest provides -:ref:`builtinfixtures` which allow to request arbitrary -resources, for example a unique temporary directory:: +``pytest`` provides `Builtin fixtures/function arguments `_ to request arbitrary resources, like a unique temporary directory: # content of test_tmpdir.py def test_needsfiles(tmpdir): print (tmpdir) assert 0 -We list the name ``tmpdir`` in the test function signature and -``pytest`` will lookup and call a fixture factory to create the resource -before performing the test function call. Let's just run it:: +List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:: $ pytest -q test_tmpdir.py F [100%] @@ -177,22 +164,21 @@ before performing the test function call. Let's just run it:: PYTEST_TMPDIR/test_needsfiles0 1 failed in 0.12 seconds -Before the test runs, a unique-per-test-invocation temporary directory -was created. More info at :ref:`tmpdir handling`. +More info on tmpdir handeling is available at `Temporary directories and files `_. -You can find out what kind of builtin :ref:`fixtures` exist by typing:: +Find out what kind of builtin ```pytest`` fixtures `_ exist with the command:: pytest --fixtures # shows builtin and custom fixtures -Where to go next +Continue reading ------------------------------------- -Here are a few suggestions where to go next: +Check out additional pytest resources to help you customize tests for your unique workflow: -* :ref:`cmdline` for command line invocation examples -* :ref:`good practices ` for virtualenv, test layout -* :ref:`existingtestsuite` for working with pre-existing tests -* :ref:`fixtures` for providing a functional baseline to your tests -* :ref:`plugins` managing and writing plugins +* ":ref:`cmdline`" for command line invocation examples +* ":ref:`goodpractices`" for virtualenv and test layouts +* ":ref:`existingtestsuite`" for working with pre-existing tests +* ":ref:`fixtures`" for providing a functional baseline to your tests +* ":ref:`plugins`" for managing and writing plugins .. include:: links.inc From f555a3a76cc205b0f0d2b253d0146290e7ca34cf Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Mon, 15 Jan 2018 13:27:10 -0800 Subject: [PATCH 12/50] Update getting started guide Proofread; added intro paragraph under first header to orient new users; fixed grammar errors (switched to active voice, actionable directions, etc) to improve readability --- .DS_Store | Bin 0 -> 6148 bytes doc/.DS_Store | Bin 0 -> 6148 bytes doc/en/.DS_Store | Bin 0 -> 6148 bytes doc/en/getting-started.rst | 14 +++++--------- 4 files changed, 5 insertions(+), 9 deletions(-) create mode 100644 .DS_Store create mode 100644 doc/.DS_Store create mode 100644 doc/en/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7cbe275046a42350e05301fa3e51caa9dcc81710 GIT binary patch literal 6148 zcmeH~Jr2S!425ml0g0s}V-^m;4I%_5-~tFbB6UFQIXcfj3xyd~=vlJA*s0a_4NWZ~ zdU%d|kwHY(aHH%j%uJDYGRh>k@o~Ffr|E7bw^h;#@Ihw#xlK?3DnJFO02QDD3sN8t z^7Upx&%{Te0#slb3fT9dz>PK8g8u11@DTv)QFg=HX9=)a0j$XuLtMpWxr-|5*!DDnJGPOabkW z=i>n{m1pb6>skGhRa-YW=$9kB{RAMfqj(EA!;KrJ4LH~3h_y_>bP3Z5S>vG!N$@uSMUZw^aLJ2(QSkU3$fqIb9pr1d;e&iX;6Vd)od{|=!lohtBGA;&_%QP(7ai*Ls7pS=NC^Gt$`e=02O#vU>M7p)&Dj8 zP5=K);))7Tfxl8fN2|?hi6>=k?LE$FZGrFLmUDxfVeS+RUXFoYjlb| WP3!`lj=0l-{24G^XjI_a3fut+K^1ra literal 0 HcmV?d00001 diff --git a/doc/en/getting-started.rst b/doc/en/getting-started.rst index d4847d59b38..3c6391c6a45 100644 --- a/doc/en/getting-started.rst +++ b/doc/en/getting-started.rst @@ -15,7 +15,7 @@ Installation and Getting Started ``pytest`` is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. Get started in minutes with a small unit test or complex functional test for your application or library. .. _`getstarted`: -.. _`installpytest`: +.. _`installation`: Install ``pytest`` ---------------------------------------- @@ -29,7 +29,7 @@ Install ``pytest`` $ pytest --version This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py -.. _`createyourfirsttest`: +.. _`simpletest`: Create your first test ---------------------------------------------------------- @@ -68,11 +68,7 @@ This test returns a failure report because ``func(3)`` does not return ``5``. .. note:: - You can use the ``assert`` statement to verify test expectations. pytest’s :ref:`Advanced assertion introspection` will intelligently report intermediate values of the assert expression so you can avoid the many names `of JUnit legacy methods`_. - -.. _`JUnit legacy methods`: http://docs.python.org/library/unittest.html#test-cases - -.. _`Advanced assertion introspection`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement + You can use the ``assert`` statement to verify test expectations. pytest’s `Advanced assertion introspection `_ will intelligently report intermediate values of the assert expression so you can avoid the many names `of JUnit legacy methods `_. Run multiple tests ---------------------------------------------------------- @@ -115,7 +111,7 @@ Once you develop multiple tests, you may want to group them into a class. pytest x = "hello" assert hasattr(x, 'check') -``pytest`` discovers all tests following its :ref:`Conventions for Python test discovery `, so it finds both ``test_`` prefixed functions. There is no need to subclass anything. We can simply run the module by passing its filename: +``pytest`` discovers all tests following its :ref:`Conventions for Python test discovery `, so it finds both ``test_`` prefixed functions. There is no need to subclass anything. We can simply run the module by passing its filename:: $ pytest -q test_class.py .F [100%] @@ -138,7 +134,7 @@ The first test passed and the second failed. You can easily see the intermediate Request a unique temporary directory for functional tests -------------------------------------------------------------- -``pytest`` provides `Builtin fixtures/function arguments `_ to request arbitrary resources, like a unique temporary directory: +``pytest`` provides `Builtin fixtures/function arguments `_ to request arbitrary resources, like a unique temporary directory:: # content of test_tmpdir.py def test_needsfiles(tmpdir): From b7485763581ba0f8936fdc451d1c9fa5b08007c0 Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Mon, 15 Jan 2018 13:36:44 -0800 Subject: [PATCH 13/50] Add name --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 862378be9f5..55772997833 100644 --- a/AUTHORS +++ b/AUTHORS @@ -38,6 +38,7 @@ Christian Boelsen Christian Theunert Christian Tismer Christopher Gilling +Cyrus Maden Daniel Grana Daniel Hahler Daniel Nuri From d8c23fd39bbe3e6d3f44ef44ffe58bee81fccaee Mon Sep 17 00:00:00 2001 From: Kate Date: Tue, 16 Jan 2018 12:36:28 +0300 Subject: [PATCH 14/50] Fix wrong formatting --- doc/en/parametrize.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/parametrize.rst b/doc/en/parametrize.rst index 7bc37ae3849..991dd4b1e41 100644 --- a/doc/en/parametrize.rst +++ b/doc/en/parametrize.rst @@ -123,7 +123,7 @@ To get all combinations of multiple parametrized arguments you can stack def test_foo(x, y): pass -This will run the test with the arguments set to ``x=0/y=2``,``x=1/y=2``, +This will run the test with the arguments set to ``x=0/y=2``, ``x=1/y=2``, ``x=0/y=3``, and ``x=1/y=3`` exhausting parameters in the order of the decorators. .. _`pytest_generate_tests`: From e0b63e34fa2ad6c5cb6d607b3f276d3724c7986f Mon Sep 17 00:00:00 2001 From: Kimberly Date: Thu, 18 Jan 2018 10:22:04 -0700 Subject: [PATCH 15/50] fixed typo in logging doc and added fix to changelog --- changelog/3129.trivial | 1 + doc/en/logging.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changelog/3129.trivial diff --git a/changelog/3129.trivial b/changelog/3129.trivial new file mode 100644 index 00000000000..96c6e1e25be --- /dev/null +++ b/changelog/3129.trivial @@ -0,0 +1 @@ +corrected 'you' to 'your' in logging doc diff --git a/doc/en/logging.rst b/doc/en/logging.rst index e3bf5603887..f87e1329bf8 100644 --- a/doc/en/logging.rst +++ b/doc/en/logging.rst @@ -69,7 +69,7 @@ with:: pytest --no-print-logs -Or in you ``pytest.ini``: +Or in your ``pytest.ini``: .. code-block:: ini From 8be1136d039465d8e69ebc4ede35956f40aa6ea3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 18 Jan 2018 16:40:00 -0200 Subject: [PATCH 16/50] Small changelog formatting --- changelog/3129.trivial | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3129.trivial b/changelog/3129.trivial index 96c6e1e25be..65958660c9f 100644 --- a/changelog/3129.trivial +++ b/changelog/3129.trivial @@ -1 +1 @@ -corrected 'you' to 'your' in logging doc +Corrected 'you' to 'your' in logging docs. From 931e8830ba62a54c0158fd9a60af6b5f28fa1f11 Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Thu, 18 Jan 2018 15:54:31 -0800 Subject: [PATCH 17/50] Update changelog Not issue ID. Will update with pr ID after submitting pr --- changelog/pr.doc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/pr.doc diff --git a/changelog/pr.doc b/changelog/pr.doc new file mode 100644 index 00000000000..28e61c1d8bf --- /dev/null +++ b/changelog/pr.doc @@ -0,0 +1 @@ +Improve readability (wording, grammar) of Getting Started guide From cd76366d87eaaec6d04234c3d625c4506c058eaf Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Thu, 18 Jan 2018 15:59:37 -0800 Subject: [PATCH 18/50] Rename pr.doc to 3131.doc --- changelog/{pr.doc => 3131.doc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{pr.doc => 3131.doc} (100%) diff --git a/changelog/pr.doc b/changelog/3131.doc similarity index 100% rename from changelog/pr.doc rename to changelog/3131.doc From ebb4c4715560bbf86b7cd328bef90b0c981bedc7 Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Fri, 19 Jan 2018 18:44:33 -0800 Subject: [PATCH 19/50] Delete .DS_Store --- doc/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/.DS_Store diff --git a/doc/.DS_Store b/doc/.DS_Store deleted file mode 100644 index f72d921e6ac404f4ad650e931a05c7f1f695bc1d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425lAKw|00n1usyg9yP1xB!B(As7&Qj?VKB1BDq?=vlJA*p1cp4NWZ~ zx_R_#kxoSBaHFg&3`~(vA!;KrJ4LH~3h_y_>bP Date: Fri, 19 Jan 2018 18:44:46 -0800 Subject: [PATCH 20/50] Delete .DS_Store --- doc/en/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/en/.DS_Store diff --git a/doc/en/.DS_Store b/doc/en/.DS_Store deleted file mode 100644 index f940bed6e384f5f76cd23313e2f8236eb972bc2c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKI|>3Z5S>vG!N$@uSMUZw^aLJ2(QSkU3$fqIb9pr1d;e&iX;6Vd)od{|=!lohtBGA;&_%QP(7ai*Ls7pS=NC^Gt$`e=02O#vU>M7p)&Dj8 zP5=K);))7Tfxl8fN2|?hi6>=k?LE$FZGrFLmUDxfVeS+RUXFoYjlb| WP3!`lj=0l-{24G^XjI_a3fut+K^1ra From 0b6df94b12a841a3de2fbced9e6d75c65f29761d Mon Sep 17 00:00:00 2001 From: Cyrus Maden Date: Fri, 19 Jan 2018 18:45:02 -0800 Subject: [PATCH 21/50] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 7cbe275046a42350e05301fa3e51caa9dcc81710..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425ml0g0s}V-^m;4I%_5-~tFbB6UFQIXcfj3xyd~=vlJA*s0a_4NWZ~ zdU%d|kwHY(aHH%j%uJDYGRh>k@o~Ffr|E7bw^h;#@Ihw#xlK?3DnJFO02QDD3sN8t z^7Upx&%{Te0#slb3fT9dz>PK8g8u11@DTv)QFg=HX9=)a0j$XuLtMpWxr-|5*!DDnJGPOabkW z=i>n{m1pb6>skGhRa-YW=$9kB{RAMfqj(E Date: Sat, 20 Jan 2018 11:12:59 -0800 Subject: [PATCH 22/50] Typo fix: "handeling" --> "handling" --- doc/en/getting-started.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/getting-started.rst b/doc/en/getting-started.rst index 3c6391c6a45..4a72844801a 100644 --- a/doc/en/getting-started.rst +++ b/doc/en/getting-started.rst @@ -160,7 +160,7 @@ List the name ``tmpdir`` in the test function signature and ``pytest`` will look PYTEST_TMPDIR/test_needsfiles0 1 failed in 0.12 seconds -More info on tmpdir handeling is available at `Temporary directories and files `_. +More info on tmpdir handling is available at `Temporary directories and files `_. Find out what kind of builtin ```pytest`` fixtures `_ exist with the command:: From 196dcc37a81aa5a0155f10a36a3131338108fa48 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Sun, 21 Jan 2018 21:50:26 +0200 Subject: [PATCH 23/50] Clarify a possible confusion when using pytest_fixture_setup with fixture functions that return None --- _pytest/hookspec.py | 10 +++++++++- changelog/2698.doc | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/2698.doc diff --git a/_pytest/hookspec.py b/_pytest/hookspec.py index c3d55018997..595ad6d914b 100644 --- a/_pytest/hookspec.py +++ b/_pytest/hookspec.py @@ -358,7 +358,15 @@ def pytest_runtest_logreport(report): def pytest_fixture_setup(fixturedef, request): """ performs fixture setup execution. - Stops at first non-None result, see :ref:`firstresult` """ + :return: The return value of the call to the fixture function + + Stops at first non-None result, see :ref:`firstresult` + + .. note:: + If the fixture function returns None, other implementations of + this hook function will continue to be called, according to the + behavior of the :ref:`firstresult` option. + """ def pytest_fixture_post_finalizer(fixturedef, request): diff --git a/changelog/2698.doc b/changelog/2698.doc new file mode 100644 index 00000000000..3088b6efc60 --- /dev/null +++ b/changelog/2698.doc @@ -0,0 +1 @@ +Clarify a possible confusion when using pytest_fixture_setup with fixture functions that return None. \ No newline at end of file From 5c0b340a4b76e4fa5063dfec25508c31fef2c613 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Sun, 21 Jan 2018 22:43:00 +0200 Subject: [PATCH 24/50] Clarify that warning capturing doesn't change the warning filter by default --- changelog/2457.doc | 1 + doc/en/warnings.rst | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 changelog/2457.doc diff --git a/changelog/2457.doc b/changelog/2457.doc new file mode 100644 index 00000000000..31d7aa1c2df --- /dev/null +++ b/changelog/2457.doc @@ -0,0 +1 @@ +Clarify that warning capturing doesn't change the warning filter by default. \ No newline at end of file diff --git a/doc/en/warnings.rst b/doc/en/warnings.rst index f249d7e3b2f..d5a627aea85 100644 --- a/doc/en/warnings.rst +++ b/doc/en/warnings.rst @@ -112,6 +112,12 @@ decorator or to all tests in a module by setting the ``pytestmark`` variable: pytestmark = pytest.mark.filterwarnings('error') +.. note:: + + Except for these features, pytest does not change the python warning filter; it only captures + and displays the warnings which are issued with respect to the currently configured filter, + including changes to the filter made by test functions or by the system under test. + .. note:: ``DeprecationWarning`` and ``PendingDeprecationWarning`` are hidden by the standard library From 8994603d46d32abc50f50a8967ed042c764b2172 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Sun, 21 Jan 2018 23:17:16 +0200 Subject: [PATCH 25/50] Document hooks (defined with historic=True) which cannot be used with hookwrapper=True --- _pytest/hookspec.py | 21 ++++++++++++++++++++- changelog/2423.doc | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelog/2423.doc diff --git a/_pytest/hookspec.py b/_pytest/hookspec.py index c3d55018997..417a9de56d6 100644 --- a/_pytest/hookspec.py +++ b/_pytest/hookspec.py @@ -16,6 +16,9 @@ def pytest_addhooks(pluginmanager): :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @@ -27,6 +30,9 @@ def pytest_namespace(): the pytest namespace. This hook is called at plugin registration time. + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @@ -36,6 +42,9 @@ def pytest_plugin_registered(plugin, manager): :param plugin: the plugin module or instance :param _pytest.config.PytestPluginManager manager: pytest plugin manager + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @@ -66,6 +75,9 @@ def pytest_addoption(parser): The config object is passed around on many internal objects via the ``.config`` attribute or can be retrieved as the ``pytestconfig`` fixture. + + .. note:: + This hook is incompatible with ``hookwrapper=True``. """ @@ -80,6 +92,9 @@ def pytest_configure(config): After that, the hook is called for other conftest files as they are imported. + .. note:: + This hook is incompatible with ``hookwrapper=True``. + :arg _pytest.config.Config config: pytest config object """ @@ -456,7 +471,11 @@ def pytest_terminal_summary(terminalreporter, exitstatus): def pytest_logwarning(message, code, nodeid, fslocation): """ process a warning specified by a message, a code string, a nodeid and fslocation (both of which may be None - if the warning is not tied to a partilar node/location).""" + if the warning is not tied to a partilar node/location). + + .. note:: + This hook is incompatible with ``hookwrapper=True``. + """ # ------------------------------------------------------------------------- # doctest hooks diff --git a/changelog/2423.doc b/changelog/2423.doc new file mode 100644 index 00000000000..a28bf02d450 --- /dev/null +++ b/changelog/2423.doc @@ -0,0 +1 @@ +Document hooks (defined with historic=True) which cannot be used with hookwrapper=True. \ No newline at end of file From 8a8797df8034fb37ced96406429fe6f1b28bbf1b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 21 Jan 2018 23:21:21 -0200 Subject: [PATCH 26/50] Small changelog formatting --- changelog/2423.doc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/2423.doc b/changelog/2423.doc index a28bf02d450..96cc6829783 100644 --- a/changelog/2423.doc +++ b/changelog/2423.doc @@ -1 +1 @@ -Document hooks (defined with historic=True) which cannot be used with hookwrapper=True. \ No newline at end of file +Document hooks (defined with ``historic=True``) which cannot be used with ``hookwrapper=True``. From a5e60b6a2d7f8044a08334cd4ca1a9873489528e Mon Sep 17 00:00:00 2001 From: Raphael Castaneda Date: Thu, 18 Jan 2018 16:06:42 -0800 Subject: [PATCH 27/50] implement #3130 - adding record_xml_attribute fixture update incorrect expected attribute value in test_record_attribute attr names must be strings Update CHANGELOG formatting update usage documentation Fix versionadded for record_xml_attribute Indent the xml schema properly inside the warning box in the docs --- AUTHORS | 1 + _pytest/junitxml.py | 26 +++++++++++++++++ changelog/3130.feature | 1 + doc/en/usage.rst | 60 ++++++++++++++++++++++++++++++++++++++++ testing/test_junitxml.py | 21 ++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 changelog/3130.feature diff --git a/AUTHORS b/AUTHORS index 862378be9f5..502cfff7ee7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -149,6 +149,7 @@ Punyashloka Biswal Quentin Pradet Ralf Schmitt Ran Benita +Raphael Castaneda Raphael Pierzina Raquel Alegre Ravi Chandra diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 7fb40dc3548..e929eeba8e4 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -85,6 +85,9 @@ def append(self, node): def add_property(self, name, value): self.properties.append((str(name), bin_xml_escape(value))) + def add_attribute(self, name, value): + self.attrs[str(name)] = bin_xml_escape(value) + def make_properties_node(self): """Return a Junit node containing custom properties, if any. """ @@ -98,6 +101,7 @@ def make_properties_node(self): def record_testreport(self, testreport): assert not self.testcase names = mangle_test_address(testreport.nodeid) + existing_attrs = self.attrs classnames = names[:-1] if self.xml.prefix: classnames.insert(0, self.xml.prefix) @@ -111,6 +115,7 @@ def record_testreport(self, testreport): if hasattr(testreport, "url"): attrs["url"] = testreport.url self.attrs = attrs + self.attrs.update(existing_attrs) # restore any user-defined attributes def to_xml(self): testcase = Junit.testcase(time=self.duration, **self.attrs) @@ -211,6 +216,27 @@ def add_property_noop(name, value): return add_property_noop +@pytest.fixture +def record_xml_attribute(request): + """Add extra xml attributes to the tag for the calling test. + The fixture is callable with ``(name, value)``, with value being automatically + xml-encoded + """ + request.node.warn( + code='C3', + message='record_xml_attribute is an experimental feature', + ) + xml = getattr(request.config, "_xml", None) + if xml is not None: + node_reporter = xml.node_reporter(request.node.nodeid) + return node_reporter.add_attribute + else: + def add_attr_noop(name, value): + pass + + return add_attr_noop + + def pytest_addoption(parser): group = parser.getgroup("terminal reporting") group.addoption( diff --git a/changelog/3130.feature b/changelog/3130.feature new file mode 100644 index 00000000000..af2c235887b --- /dev/null +++ b/changelog/3130.feature @@ -0,0 +1 @@ +New fixture ``record_xml_attribute`` that allows modifying and inserting attributes on the ```` xml node in JUnit reports. diff --git a/doc/en/usage.rst b/doc/en/usage.rst index 6091db8be38..9b9552bc61c 100644 --- a/doc/en/usage.rst +++ b/doc/en/usage.rst @@ -256,6 +256,66 @@ This will add an extra property ``example_key="1"`` to the generated Also please note that using this feature will break any schema verification. This might be a problem when used with some CI servers. +record_xml_attribute +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. versionadded:: 3.4 + +To add an additional xml attribute to a testcase element, you can use +``record_xml_attribute`` fixture. This can also be used to override existing values: + +.. code-block:: python + + def test_function(record_xml_attribute): + record_xml_attribute("assertions", "REQ-1234") + record_xml_attribute("classname", "custom_classname") + print('hello world') + assert True + +Unlike ``record_xml_property``, this will not add a new child element. +Instead, this will add an attribute ``assertions="REQ-1234"`` inside the generated +``testcase`` tag and override the default ``classname`` with ``"classname=custom_classname"``: + +.. code-block:: xml + + + + hello world + + + +.. warning:: + + ``record_xml_attribute`` is an experimental feature, and its interface might be replaced + by something more powerful and general in future versions. The + functionality per-se will be kept, however. + + Using this over ``record_xml_property`` can help when using ci tools to parse the xml report. + However, some parsers are quite strict about the elements and attributes that are allowed. + Many tools use an xsd schema (like the example below) to validate incoming xml. + Make sure you are using attribute names that are allowed by your parser. + + Below is the Scheme used by Jenkins to validate the XML report: + + .. code-block:: xml + + + + + + + + + + + + + + + + + + LogXML: add_global_property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index b604c02a3de..49318ef762d 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -879,6 +879,27 @@ def test_record_with_same_name(record_xml_property): pnodes[1].assert_attr(name="foo", value="baz") +def test_record_attribute(testdir): + testdir.makepyfile(""" + import pytest + + @pytest.fixture + def other(record_xml_attribute): + record_xml_attribute("bar", 1) + def test_record(record_xml_attribute, other): + record_xml_attribute("foo", "<1"); + """) + result, dom = runandparse(testdir, '-rw') + node = dom.find_first_by_tag("testsuite") + tnode = node.find_first_by_tag("testcase") + tnode.assert_attr(bar="1") + tnode.assert_attr(foo="<1") + result.stdout.fnmatch_lines([ + 'test_record_attribute.py::test_record', + '*record_xml_attribute*experimental*', + ]) + + def test_random_report_log_xdist(testdir): """xdist calls pytest_runtest_logreport as they are executed by the slaves, with nodes from several nodes overlapping, so junitxml must cope with that From a24ca9872fce47cb897ed0d28f5901c6f4639b16 Mon Sep 17 00:00:00 2001 From: Alan Velasco Date: Thu, 25 Jan 2018 07:49:58 -0600 Subject: [PATCH 28/50] Change cache directory name to include `pytest` --- AUTHORS | 1 + _pytest/cacheprovider.py | 5 +++-- changelog/3150.feature | 1 + testing/test_cache.py | 18 +++++++++--------- 4 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 changelog/3150.feature diff --git a/AUTHORS b/AUTHORS index b01f4bd85a7..37c7f5155da 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,6 +6,7 @@ Contributors include:: Abdeali JK Abhijeet Kasurde Ahn Ki-Wook +Alan Velasco Alexander Johnson Alexei Kozlenok Anatoly Bubenkoff diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 0db08a1aa6b..4d3df2221db 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -114,7 +114,8 @@ def pytest_report_collectionfinish(self): mode = "run all (no recorded failures)" else: noun = 'failure' if self._previously_failed_count == 1 else 'failures' - suffix = " first" if self.config.getvalue("failedfirst") else "" + suffix = " first" if self.config.getvalue( + "failedfirst") else "" mode = "rerun previous {count} {noun}{suffix}".format( count=self._previously_failed_count, suffix=suffix, noun=noun ) @@ -185,7 +186,7 @@ def pytest_addoption(parser): '--cache-clear', action='store_true', dest="cacheclear", help="remove all cache contents at start of test run.") parser.addini( - "cache_dir", default='.cache', + "cache_dir", default='.pytest_cache', help="cache directory path.") diff --git a/changelog/3150.feature b/changelog/3150.feature new file mode 100644 index 00000000000..9b17cb0e4f2 --- /dev/null +++ b/changelog/3150.feature @@ -0,0 +1 @@ +Change default cache directory name from `.cache` to `.pytest_cache` \ No newline at end of file diff --git a/testing/test_cache.py b/testing/test_cache.py index a37170cdd2b..2880b923aba 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -31,7 +31,7 @@ def test_config_cache_dataerror(self, testdir): def test_cache_writefail_cachfile_silent(self, testdir): testdir.makeini("[pytest]") - testdir.tmpdir.join('.cache').write('gone wrong') + testdir.tmpdir.join('.pytest_cache').write('gone wrong') config = testdir.parseconfigure() cache = config.cache cache.set('test/broken', []) @@ -39,14 +39,14 @@ def test_cache_writefail_cachfile_silent(self, testdir): @pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows') def test_cache_writefail_permissions(self, testdir): testdir.makeini("[pytest]") - testdir.tmpdir.ensure_dir('.cache').chmod(0) + testdir.tmpdir.ensure_dir('.pytest_cache').chmod(0) config = testdir.parseconfigure() cache = config.cache cache.set('test/broken', []) @pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows') def test_cache_failure_warns(self, testdir): - testdir.tmpdir.ensure_dir('.cache').chmod(0) + testdir.tmpdir.ensure_dir('.pytest_cache').chmod(0) testdir.makepyfile(""" def test_error(): raise Exception @@ -127,7 +127,7 @@ def test_hello(): """) result = testdir.runpytest("-v") result.stdout.fnmatch_lines([ - "cachedir: .cache" + "cachedir: .pytest_cache" ]) @@ -201,8 +201,8 @@ def test_3(): ]) # Run this again to make sure clear-cache is robust - if os.path.isdir('.cache'): - shutil.rmtree('.cache') + if os.path.isdir('.pytest_cache'): + shutil.rmtree('.pytest_cache') result = testdir.runpytest("--lf", "--cache-clear") result.stdout.fnmatch_lines([ "*1 failed*2 passed*", @@ -495,15 +495,15 @@ def test_lastfailed_creates_cache_when_needed(self, testdir): # Issue #1342 testdir.makepyfile(test_empty='') testdir.runpytest('-q', '--lf') - assert not os.path.exists('.cache') + assert not os.path.exists('.pytest_cache') testdir.makepyfile(test_successful='def test_success():\n assert True') testdir.runpytest('-q', '--lf') - assert not os.path.exists('.cache') + assert not os.path.exists('.pytest_cache') testdir.makepyfile(test_errored='def test_error():\n assert False') testdir.runpytest('-q', '--lf') - assert os.path.exists('.cache') + assert os.path.exists('.pytest_cache') def test_xfail_not_considered_failure(self, testdir): testdir.makepyfile(''' From cbbd606b6cac1027c35812f38456fd1dff956840 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 25 Jan 2018 17:21:54 -0200 Subject: [PATCH 29/50] Reword changelog --- changelog/3138.feature | 1 + changelog/3150.feature | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 changelog/3138.feature delete mode 100644 changelog/3150.feature diff --git a/changelog/3138.feature b/changelog/3138.feature new file mode 100644 index 00000000000..338d429f9f9 --- /dev/null +++ b/changelog/3138.feature @@ -0,0 +1 @@ +The default cache directory has been renamed from ``.cache`` to ``.pytest_cache`` after community feedback that the name ``.cache`` did not make it clear that it was used by pytest. diff --git a/changelog/3150.feature b/changelog/3150.feature deleted file mode 100644 index 9b17cb0e4f2..00000000000 --- a/changelog/3150.feature +++ /dev/null @@ -1 +0,0 @@ -Change default cache directory name from `.cache` to `.pytest_cache` \ No newline at end of file From 4285325cb88d90ad36724de9d441e51da614f249 Mon Sep 17 00:00:00 2001 From: Brian Maissy Date: Thu, 25 Jan 2018 23:07:34 +0200 Subject: [PATCH 30/50] Added note that calling pytest.main multiple times from the same process is not recommended because of import caching --- changelog/3143.doc | 1 + doc/en/usage.rst | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 changelog/3143.doc diff --git a/changelog/3143.doc b/changelog/3143.doc new file mode 100644 index 00000000000..b454708ad7f --- /dev/null +++ b/changelog/3143.doc @@ -0,0 +1 @@ +Added note that calling pytest.main multiple times from the same process is not recommended because of import caching. \ No newline at end of file diff --git a/doc/en/usage.rst b/doc/en/usage.rst index 6091db8be38..18830792b16 100644 --- a/doc/en/usage.rst +++ b/doc/en/usage.rst @@ -387,6 +387,15 @@ hook was invoked:: $ python myinvoke.py *** test run reporting finishing - + +.. note:: + + Calling ``pytest.main()`` will result in importing your tests and any modules + that they import. Due to the caching mechanism of python's import system, + making subsequent calls to ``pytest.main()`` from the same process will not + reflect changes to those files between the calls. For this reason, making + multiple calls to ``pytest.main()`` from the same process (in order to re-run + tests, for example) is not recommended. + .. include:: links.inc From bf2c10c8105d770354d0abc451de4ffd9b986562 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 17 Dec 2017 11:26:51 +0100 Subject: [PATCH 31/50] parameterset: refactor marking empty parametersets --- _pytest/mark.py | 15 ++++++++++----- _pytest/python.py | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index 45182bdcd33..35cb3224af4 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -73,7 +73,7 @@ def extract_from(cls, parameterset, legacy_force_tuple=False): return cls(argval, marks=newmarks, id=None) @classmethod - def _for_parameterize(cls, argnames, argvalues, function): + def _for_parameterize(cls, argnames, argvalues, function, config): if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] force_tuple = len(argnames) == 1 @@ -85,10 +85,7 @@ def _for_parameterize(cls, argnames, argvalues, function): del argvalues if not parameters: - fs, lineno = getfslineno(function) - reason = "got empty parameter set %r, function %s at %s:%d" % ( - argnames, function.__name__, fs, lineno) - mark = MARK_GEN.skip(reason=reason) + mark = get_empty_parameterset_mark(config, argnames, function) parameters.append(ParameterSet( values=(NOTSET,) * len(argnames), marks=[mark], @@ -97,6 +94,14 @@ def _for_parameterize(cls, argnames, argvalues, function): return argnames, parameters +def get_empty_parameterset_mark(config, argnames, function): + + fs, lineno = getfslineno(function) + reason = "got empty parameter set %r, function %s at %s:%d" % ( + argnames, function.__name__, fs, lineno) + return MARK_GEN.skip(reason=reason) + + class MarkerError(Exception): """Error in use of a pytest marker/attribute.""" diff --git a/_pytest/python.py b/_pytest/python.py index 735489e484a..2a84677ec31 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -786,7 +786,7 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None, from _pytest.mark import ParameterSet from py.io import saferepr argnames, parameters = ParameterSet._for_parameterize( - argnames, argvalues, self.function) + argnames, argvalues, self.function, self.config) del argvalues if scope is None: From 37b41de779957b0e0e826016b7dee9ee2f4aec89 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 17 Dec 2017 12:49:12 +0100 Subject: [PATCH 32/50] fix #2527 - introduce a option to pic the empty parameterset action --- _pytest/mark.py | 21 +++++++++++++++++++-- changelog/2527.feature | 1 + testing/test_mark.py | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 changelog/2527.feature diff --git a/_pytest/mark.py b/_pytest/mark.py index 35cb3224af4..24bb55838c8 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -95,11 +95,17 @@ def _for_parameterize(cls, argnames, argvalues, function, config): def get_empty_parameterset_mark(config, argnames, function): - + requested_mark = config.getini('empty_parameterset') + if requested_mark in ('', None, 'skip'): + mark = MARK_GEN.skip + elif requested_mark == 'xfail': + mark = MARK_GEN.xfail(run=False) + else: + raise LookupError(requested_mark) fs, lineno = getfslineno(function) reason = "got empty parameter set %r, function %s at %s:%d" % ( argnames, function.__name__, fs, lineno) - return MARK_GEN.skip(reason=reason) + return mark(reason=reason) class MarkerError(Exception): @@ -141,6 +147,9 @@ def pytest_addoption(parser): ) parser.addini("markers", "markers for test functions", 'linelist') + parser.addini( + "empty_parameterset", + "default marker for empty parametersets") def pytest_cmdline_main(config): @@ -284,6 +293,14 @@ def pytest_configure(config): if config.option.strict: MARK_GEN._config = config + empty_parameterset = config.getini("empty_parameterset") + + if empty_parameterset not in ('skip', 'xfail', None, ''): + from pytest import UsageError + raise UsageError( + "empty_parameterset must be one of skip and xfail," + " but it is {!r}".format(empty_parameterset)) + def pytest_unconfigure(config): MARK_GEN._config = getattr(config, '_old_mark_config', None) diff --git a/changelog/2527.feature b/changelog/2527.feature new file mode 100644 index 00000000000..97e2a63fb41 --- /dev/null +++ b/changelog/2527.feature @@ -0,0 +1 @@ +introduce a pytest ini option to pick the mark for empty parametersets and allow to use xfail(run=False) \ No newline at end of file diff --git a/testing/test_mark.py b/testing/test_mark.py index 45e88ae8f4b..1e601fc4426 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -891,3 +891,26 @@ class TestMarkDecorator(object): ]) def test__eq__(self, lhs, rhs, expected): assert (lhs == rhs) == expected + + +@pytest.mark.parametrize('mark', [None, 'skip', 'xfail']) +def test_parameterset_for_parametrize_marks(testdir, mark): + if mark is not None: + testdir.makeini("[pytest]\nempty_parameterset=" + mark) + + config = testdir.parseconfig() + from _pytest.mark import pytest_configure, get_empty_parameterset_mark + pytest_configure(config) + result_mark = get_empty_parameterset_mark(config, ['a'], all) + if mark is None: + # normalize to the requested name + mark = 'skip' + assert result_mark.name == mark + + if mark == 'xfail': + assert result_mark.kwargs.get('run') is False + + +def test_parameterset_for_parametrize_bad_markname(testdir): + with pytest.raises(pytest.UsageError): + test_parameterset_for_parametrize_marks(testdir, 'bad') From 7f83605c81205398976b658b868137b41156578a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 17 Dec 2017 15:25:56 +0100 Subject: [PATCH 33/50] fix empty parameterset tests by mocking a config object --- testing/python/metafunc.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 06979681a03..99f99f6cda2 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -14,7 +14,7 @@ class TestMetafunc(object): - def Metafunc(self, func): + def Metafunc(self, func, config=None): # the unit tests of this class check if things work correctly # on the funcarg level, so we don't need a full blown # initiliazation @@ -26,7 +26,7 @@ def __init__(self, names): names = fixtures.getfuncargnames(func) fixtureinfo = FixtureInfo(names) - return python.Metafunc(func, fixtureinfo, None) + return python.Metafunc(func, fixtureinfo, config) def test_no_funcargs(self, testdir): def function(): @@ -156,7 +156,19 @@ def func(x, y): def test_parametrize_empty_list(self): def func(y): pass - metafunc = self.Metafunc(func) + + class MockConfig(object): + def getini(self, name): + return '' + + @property + def hook(self): + return self + + def pytest_make_parametrize_id(self, **kw): + pass + + metafunc = self.Metafunc(func, MockConfig()) metafunc.parametrize("y", []) assert 'skip' == metafunc._calls[0].marks[0].name From d4c11e58aa58fbb6d2e560bc62111a7efb955ca3 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 11:18:50 +0100 Subject: [PATCH 34/50] exted empty parameterset check with reason test --- testing/test_mark.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_mark.py b/testing/test_mark.py index 1e601fc4426..c85a30b41c0 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -906,7 +906,7 @@ def test_parameterset_for_parametrize_marks(testdir, mark): # normalize to the requested name mark = 'skip' assert result_mark.name == mark - + assert result_mark.kwargs['reason'].startswith("got empty parameter set ") if mark == 'xfail': assert result_mark.kwargs.get('run') is False From 8979b2a9d78161739fe870ef71be800067cff33a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 11:26:48 +0100 Subject: [PATCH 35/50] document empty_parameterset in customize.rst --- doc/en/customize.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/en/customize.rst b/doc/en/customize.rst index 6edeedf98fb..bbff9d18995 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -346,3 +346,21 @@ passed multiple times. The expected format is ``name=value``. For example:: # content of pytest.ini [pytest] console_output_style = classic + + +.. confval:: empty_parameterset + + .. versionadded:: 3.4 + + allows to pick the action for empty parametersets in parameterization + + * ``skip`` skips tests with a empty parameterset + * ``xfail`` marks tests with a empty parameterset as xfail(run=False) + + The default is ``skip``, it will be shifted to xfail in future. + + .. code-block:: ini + + # content of pytest.ini + [pytest] + empty_parameterset = xfail \ No newline at end of file From d550c33cd02512fd78a9a6b0e205dbbaf4f5e9d1 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 11:56:24 +0100 Subject: [PATCH 36/50] s/empty_parameterset/empty_parameter_set_mark --- _pytest/mark.py | 4 ++-- doc/en/customize.rst | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index 24bb55838c8..c3352b387fe 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -95,7 +95,7 @@ def _for_parameterize(cls, argnames, argvalues, function, config): def get_empty_parameterset_mark(config, argnames, function): - requested_mark = config.getini('empty_parameterset') + requested_mark = config.getini('empty_parameter_set_mark') if requested_mark in ('', None, 'skip'): mark = MARK_GEN.skip elif requested_mark == 'xfail': @@ -148,7 +148,7 @@ def pytest_addoption(parser): parser.addini("markers", "markers for test functions", 'linelist') parser.addini( - "empty_parameterset", + "empty_parameter_set_mark", "default marker for empty parametersets") diff --git a/doc/en/customize.rst b/doc/en/customize.rst index bbff9d18995..eb79ba6682f 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -348,7 +348,7 @@ passed multiple times. The expected format is ``name=value``. For example:: console_output_style = classic -.. confval:: empty_parameterset +.. confval:: empty_parameter_set_mark .. versionadded:: 3.4 @@ -363,4 +363,4 @@ passed multiple times. The expected format is ``name=value``. For example:: # content of pytest.ini [pytest] - empty_parameterset = xfail \ No newline at end of file + empty_parameter_set_mark = xfail \ No newline at end of file From 77de45cce3bac71119c99581b848b5e20660276d Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 12:01:27 +0100 Subject: [PATCH 37/50] enhance docs for empty_parameter_set_mark according to review comments --- changelog/2527.feature | 2 +- doc/en/customize.rst | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/changelog/2527.feature b/changelog/2527.feature index 97e2a63fb41..ed00398d900 100644 --- a/changelog/2527.feature +++ b/changelog/2527.feature @@ -1 +1 @@ -introduce a pytest ini option to pick the mark for empty parametersets and allow to use xfail(run=False) \ No newline at end of file +Introduce ``empty_parameter_set_mark`` ini option to select which mark to apply when ``@pytest.mark.parametrize`` is given an empty set of parameters. Valid options are ``skip`` (default) and ``xfail``. Note that it is planned to change the default to ``xfail`` in future releases as this is considered less error prone. \ No newline at end of file diff --git a/doc/en/customize.rst b/doc/en/customize.rst index eb79ba6682f..80efe6b8353 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -350,17 +350,23 @@ passed multiple times. The expected format is ``name=value``. For example:: .. confval:: empty_parameter_set_mark - .. versionadded:: 3.4 + .. versionadded:: 3.4 - allows to pick the action for empty parametersets in parameterization + Allows to pick the action for empty parametersets in parameterization - * ``skip`` skips tests with a empty parameterset - * ``xfail`` marks tests with a empty parameterset as xfail(run=False) + * ``skip`` skips tests with a empty parameterset (default) + * ``xfail`` marks tests with a empty parameterset as xfail(run=False) - The default is ``skip``, it will be shifted to xfail in future. + .. note:: - .. code-block:: ini + it is planned to change the default to ``xfail`` in future releases + as this is considered less error prone. see `#3155`_ + + .. code-block:: ini # content of pytest.ini [pytest] - empty_parameter_set_mark = xfail \ No newline at end of file + empty_parameter_set_mark = xfail + + +.. _`#3155`: https://github.com/pytest-dev/pytest/issues/3155 \ No newline at end of file From a54cd4c2fdf63634560920ec7c4a582b8ca32e0e Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 12:05:52 +0100 Subject: [PATCH 38/50] correct testing and usage of the empty_parameter_set_mark config option --- _pytest/mark.py | 2 +- testing/test_mark.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index c3352b387fe..e5d956252f2 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -293,7 +293,7 @@ def pytest_configure(config): if config.option.strict: MARK_GEN._config = config - empty_parameterset = config.getini("empty_parameterset") + empty_parameterset = config.getini("empty_parameter_set_mark") if empty_parameterset not in ('skip', 'xfail', None, ''): from pytest import UsageError diff --git a/testing/test_mark.py b/testing/test_mark.py index c85a30b41c0..c3a80083cb6 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -893,16 +893,16 @@ def test__eq__(self, lhs, rhs, expected): assert (lhs == rhs) == expected -@pytest.mark.parametrize('mark', [None, 'skip', 'xfail']) +@pytest.mark.parametrize('mark', [None, '', 'skip', 'xfail']) def test_parameterset_for_parametrize_marks(testdir, mark): if mark is not None: - testdir.makeini("[pytest]\nempty_parameterset=" + mark) + testdir.makeini("[pytest]\nempty_parameter_set_mark=" + mark) config = testdir.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark pytest_configure(config) result_mark = get_empty_parameterset_mark(config, ['a'], all) - if mark is None: + if mark in (None, ''): # normalize to the requested name mark = 'skip' assert result_mark.name == mark From 17a1ed5edf73e12d14665a2e6ed2399ae6415579 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Fri, 26 Jan 2018 12:12:26 +0100 Subject: [PATCH 39/50] use a constant to sort out repeated use of the EMPTY_PARAMETERSET_OPTION --- _pytest/mark.py | 13 +++++++------ testing/test_mark.py | 8 ++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index e5d956252f2..b49f0fc644e 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -13,6 +13,8 @@ from .deprecated import MARK_PARAMETERSET_UNPACKING from .compat import NOTSET, getfslineno +EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" + def alias(name, warning=None): getter = attrgetter(name) @@ -95,7 +97,7 @@ def _for_parameterize(cls, argnames, argvalues, function, config): def get_empty_parameterset_mark(config, argnames, function): - requested_mark = config.getini('empty_parameter_set_mark') + requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION) if requested_mark in ('', None, 'skip'): mark = MARK_GEN.skip elif requested_mark == 'xfail': @@ -148,7 +150,7 @@ def pytest_addoption(parser): parser.addini("markers", "markers for test functions", 'linelist') parser.addini( - "empty_parameter_set_mark", + EMPTY_PARAMETERSET_OPTION, "default marker for empty parametersets") @@ -293,13 +295,12 @@ def pytest_configure(config): if config.option.strict: MARK_GEN._config = config - empty_parameterset = config.getini("empty_parameter_set_mark") + empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION) if empty_parameterset not in ('skip', 'xfail', None, ''): - from pytest import UsageError raise UsageError( - "empty_parameterset must be one of skip and xfail," - " but it is {!r}".format(empty_parameterset)) + "{!s} must be one of skip and xfail," + " but it is {!r}".format(EMPTY_PARAMETERSET_OPTION, empty_parameterset)) def pytest_unconfigure(config): diff --git a/testing/test_mark.py b/testing/test_mark.py index c3a80083cb6..b4dd656340e 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -3,7 +3,10 @@ import sys import pytest -from _pytest.mark import MarkGenerator as Mark, ParameterSet, transfer_markers +from _pytest.mark import ( + MarkGenerator as Mark, ParameterSet, transfer_markers, + EMPTY_PARAMETERSET_OPTION, +) class TestMark(object): @@ -896,7 +899,8 @@ def test__eq__(self, lhs, rhs, expected): @pytest.mark.parametrize('mark', [None, '', 'skip', 'xfail']) def test_parameterset_for_parametrize_marks(testdir, mark): if mark is not None: - testdir.makeini("[pytest]\nempty_parameter_set_mark=" + mark) + testdir.makeini( + "[pytest]\n{}={}".format(EMPTY_PARAMETERSET_OPTION, mark)) config = testdir.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark From 169635e8899e60de8155ad151ff99f10c3652fa0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 27 Jan 2018 11:02:32 -0200 Subject: [PATCH 40/50] Move example of empty_parameter_set_mark closer to the options --- doc/en/customize.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/doc/en/customize.rst b/doc/en/customize.rst index 80efe6b8353..9fe094ba19f 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -357,16 +357,17 @@ passed multiple times. The expected format is ``name=value``. For example:: * ``skip`` skips tests with a empty parameterset (default) * ``xfail`` marks tests with a empty parameterset as xfail(run=False) - .. note:: + .. code-block:: ini - it is planned to change the default to ``xfail`` in future releases - as this is considered less error prone. see `#3155`_ + # content of pytest.ini + [pytest] + empty_parameter_set_mark = xfail - .. code-block:: ini + .. note:: + + The default value of this option is planned to change to ``xfail`` in future releases + as this is considered less error prone, see `#3155`_ for more details. - # content of pytest.ini - [pytest] - empty_parameter_set_mark = xfail -.. _`#3155`: https://github.com/pytest-dev/pytest/issues/3155 \ No newline at end of file +.. _`#3155`: https://github.com/pytest-dev/pytest/issues/3155 From 269eeec702f84af60d89899cc5318ef8e74096ea Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 27 Jan 2018 11:45:46 -0200 Subject: [PATCH 41/50] Replace deprecated option.getvalue by option.getoption in cacheprovider --- _pytest/cacheprovider.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 4d3df2221db..27dadb32803 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -17,7 +17,7 @@ def __init__(self, config): self.config = config self._cachedir = Cache.cache_dir_from_config(config) self.trace = config.trace.root.get("cache") - if config.getvalue("cacheclear"): + if config.getoption("cacheclear"): self.trace("clearing cachedir") if self._cachedir.check(): self._cachedir.remove() @@ -104,7 +104,7 @@ class LFPlugin(object): def __init__(self, config): self.config = config active_keys = 'lf', 'failedfirst' - self.active = any(config.getvalue(key) for key in active_keys) + self.active = any(config.getoption(key) for key in active_keys) self.lastfailed = config.cache.get("cache/lastfailed", {}) self._previously_failed_count = None @@ -114,7 +114,7 @@ def pytest_report_collectionfinish(self): mode = "run all (no recorded failures)" else: noun = 'failure' if self._previously_failed_count == 1 else 'failures' - suffix = " first" if self.config.getvalue( + suffix = " first" if self.config.getoption( "failedfirst") else "" mode = "rerun previous {count} {noun}{suffix}".format( count=self._previously_failed_count, suffix=suffix, noun=noun @@ -152,7 +152,7 @@ def pytest_collection_modifyitems(self, session, config, items): # running a subset of all tests with recorded failures outside # of the set of tests currently executing return - if self.config.getvalue("lf"): + if self.config.getoption("lf"): items[:] = previously_failed config.hook.pytest_deselected(items=previously_passed) else: @@ -160,7 +160,7 @@ def pytest_collection_modifyitems(self, session, config, items): def pytest_sessionfinish(self, session): config = self.config - if config.getvalue("cacheshow") or hasattr(config, "slaveinput"): + if config.getoption("cacheshow") or hasattr(config, "slaveinput"): return saved_lastfailed = config.cache.get("cache/lastfailed", {}) From 6e4efccc38123aedd86e2a9313552fa3a0f8509b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 27 Jan 2018 11:46:29 -0200 Subject: [PATCH 42/50] Rename test_cache to test_cacheprovider for consistency with cacheprovider --- testing/{test_cache.py => test_cacheprovider.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename testing/{test_cache.py => test_cacheprovider.py} (100%) mode change 100755 => 100644 diff --git a/testing/test_cache.py b/testing/test_cacheprovider.py old mode 100755 new mode 100644 similarity index 100% rename from testing/test_cache.py rename to testing/test_cacheprovider.py From ab00c3e9117e22f4a7e138cbbb5b6c8754bf3102 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 29 Jan 2018 08:44:11 -0200 Subject: [PATCH 43/50] Add .pytest_cache directory to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3b7ec9facf2..99c4c7badb2 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ env/ 3rdparty/ .tox .cache +.pytest_cache .coverage .ropeproject .idea From ebab1b6c69e3b5cd436bfaf31c957c3df1649c64 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Tue, 23 Jan 2018 00:57:56 +0100 Subject: [PATCH 44/50] live-logging: Colorize levelname --- _pytest/logging.py | 60 ++++++++++++++++++++++++++++++- changelog/3142.feature | 1 + testing/logging/test_formatter.py | 29 +++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 changelog/3142.feature create mode 100644 testing/logging/test_formatter.py diff --git a/_pytest/logging.py b/_pytest/logging.py index f92b4c75e17..095115cd979 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -2,8 +2,10 @@ import logging from contextlib import closing, contextmanager +import re import six +from _pytest.config import create_terminal_writer import pytest import py @@ -12,6 +14,58 @@ DEFAULT_LOG_DATE_FORMAT = '%H:%M:%S' +class ColoredLevelFormatter(logging.Formatter): + """ + Colorize the %(levelname)..s part of the log format passed to __init__. + """ + + LOGLEVEL_COLOROPTS = { + logging.CRITICAL: {'red'}, + logging.ERROR: {'red', 'bold'}, + logging.WARNING: {'yellow'}, + logging.WARN: {'yellow'}, + logging.INFO: {'green'}, + logging.DEBUG: {'purple'}, + logging.NOTSET: set(), + } + LEVELNAME_FMT_REGEX = re.compile(r'%\(levelname\)([+-]?\d*s)') + + def __init__(self, terminalwriter, *args, **kwargs): + super(ColoredLevelFormatter, self).__init__( + *args, **kwargs) + if six.PY2: + self._original_fmt = self._fmt + else: + self._original_fmt = self._style._fmt + self._level_to_fmt_mapping = {} + + levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt) + if not levelname_fmt_match: + return + levelname_fmt = levelname_fmt_match.group() + + for level, color_opts in self.LOGLEVEL_COLOROPTS.items(): + formatted_levelname = levelname_fmt % { + 'levelname': logging.getLevelName(level)} + + # add ANSI escape sequences around the formatted levelname + color_kwargs = {name: True for name in color_opts} + colorized_formatted_levelname = terminalwriter.markup( + formatted_levelname, **color_kwargs) + self._level_to_fmt_mapping[level] = self.LEVELNAME_FMT_REGEX.sub( + colorized_formatted_levelname, + self._fmt) + + def format(self, record): + fmt = self._level_to_fmt_mapping.get( + record.levelno, self._original_fmt) + if six.PY2: + self._fmt = fmt + else: + self._style._fmt = fmt + return super(ColoredLevelFormatter, self).format(record) + + def get_option_ini(config, *names): for name in names: ret = config.getoption(name) # 'default' arg won't work as expected @@ -376,7 +430,11 @@ def _setup_cli_logging(self): log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager) log_cli_format = get_option_ini(self._config, 'log_cli_format', 'log_format') log_cli_date_format = get_option_ini(self._config, 'log_cli_date_format', 'log_date_format') - log_cli_formatter = logging.Formatter(log_cli_format, datefmt=log_cli_date_format) + if self._config.option.color != 'no' and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format): + log_cli_formatter = ColoredLevelFormatter(create_terminal_writer(self._config), + log_cli_format, datefmt=log_cli_date_format) + else: + log_cli_formatter = logging.Formatter(log_cli_format, datefmt=log_cli_date_format) log_cli_level = get_actual_log_level(self._config, 'log_cli_level', 'log_level') self.log_cli_handler = log_cli_handler self.live_logs_context = catching_logs(log_cli_handler, formatter=log_cli_formatter, level=log_cli_level) diff --git a/changelog/3142.feature b/changelog/3142.feature new file mode 100644 index 00000000000..1461be5143c --- /dev/null +++ b/changelog/3142.feature @@ -0,0 +1 @@ +Colorize the levelname column in the live-log output. \ No newline at end of file diff --git a/testing/logging/test_formatter.py b/testing/logging/test_formatter.py new file mode 100644 index 00000000000..10a921470d4 --- /dev/null +++ b/testing/logging/test_formatter.py @@ -0,0 +1,29 @@ +import logging + +import py.io +from _pytest.logging import ColoredLevelFormatter + + +def test_coloredlogformatter(): + logfmt = '%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s' + + record = logging.LogRecord( + name='dummy', level=logging.INFO, pathname='dummypath', lineno=10, + msg='Test Message', args=(), exc_info=False) + + class ColorConfig(object): + class option(object): + pass + + tw = py.io.TerminalWriter() + tw.hasmarkup = True + formatter = ColoredLevelFormatter(tw, logfmt) + output = formatter.format(record) + assert output == ('dummypath 10 ' + '\x1b[32mINFO \x1b[0m Test Message') + + tw.hasmarkup = False + formatter = ColoredLevelFormatter(tw, logfmt) + output = formatter.format(record) + assert output == ('dummypath 10 ' + 'INFO Test Message') From 4c148bd0ef8a0cc9d15d7aaf6d3cce044d99bfa3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 29 Jan 2018 18:23:17 -0200 Subject: [PATCH 45/50] Fix imports in failure_demo.py --- doc/en/example/assertion/failure_demo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/en/example/assertion/failure_demo.py b/doc/en/example/assertion/failure_demo.py index 423bbeb931d..3ae0268d399 100644 --- a/doc/en/example/assertion/failure_demo.py +++ b/doc/en/example/assertion/failure_demo.py @@ -157,6 +157,8 @@ def func1(self): # thanks to Matthew Scott for this test def test_dynamic_compile_shows_nicely(): + import imp + import sys src = 'def foo():\n assert 1 == 0\n' name = 'abc-123' module = imp.new_module(name) From 13ee1cffedbce45bd4c01cf72b661f6619c527d5 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 29 Jan 2018 19:00:08 -0200 Subject: [PATCH 46/50] Suggest to update all dependencies when preparing releases --- HOWTORELEASE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HOWTORELEASE.rst b/HOWTORELEASE.rst index 48a3461d4bc..9a251a8f0e8 100644 --- a/HOWTORELEASE.rst +++ b/HOWTORELEASE.rst @@ -12,7 +12,7 @@ taking a lot of time to make a new one. #. Install development dependencies in a virtual environment with:: - pip3 install -r tasks/requirements.txt + pip3 install -U -r tasks/requirements.txt #. Create a branch ``release-X.Y.Z`` with the version for the release. From 489e638b4eacc631959ac54017ec4863dd3d751e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 30 Jan 2018 19:47:56 +0000 Subject: [PATCH 47/50] Preparing release version 3.4.0 --- CHANGELOG.rst | 132 ++++++++++++++++++++++++++++++ changelog/2022.bugfix | 1 - changelog/2147.removal | 1 - changelog/2423.doc | 1 - changelog/2457.doc | 1 - changelog/2527.feature | 1 - changelog/2698.doc | 1 - changelog/2953.trivial | 1 - changelog/2976.trivial | 1 - changelog/3013.feature | 1 - changelog/3016.bugfix | 2 - changelog/3038.feature | 1 - changelog/3067.trivial | 1 - changelog/3074.bugfix | 1 - changelog/3076.doc | 1 - changelog/3088.bugfix | 1 - changelog/3092.doc | 1 - changelog/3101.feature | 3 - changelog/3103.bugfix | 1 - changelog/3107.feature | 1 - changelog/3117.feature | 1 - changelog/3129.trivial | 1 - changelog/3130.feature | 1 - changelog/3131.doc | 1 - changelog/3138.feature | 1 - changelog/3142.feature | 1 - changelog/3143.doc | 1 - doc/en/announce/index.rst | 1 + doc/en/announce/release-3.4.0.rst | 52 ++++++++++++ doc/en/builtin.rst | 4 + doc/en/cache.rst | 2 +- doc/en/example/markers.rst | 20 ++--- doc/en/example/nonpython.rst | 2 +- doc/en/example/reportingdemo.rst | 30 +++---- doc/en/example/simple.rst | 12 +-- doc/en/example/special.rst | 2 +- doc/en/fixture.rst | 24 +++--- doc/en/usage.rst | 1 + 38 files changed, 237 insertions(+), 74 deletions(-) delete mode 100644 changelog/2022.bugfix delete mode 100644 changelog/2147.removal delete mode 100644 changelog/2423.doc delete mode 100644 changelog/2457.doc delete mode 100644 changelog/2527.feature delete mode 100644 changelog/2698.doc delete mode 100644 changelog/2953.trivial delete mode 100644 changelog/2976.trivial delete mode 100644 changelog/3013.feature delete mode 100644 changelog/3016.bugfix delete mode 100644 changelog/3038.feature delete mode 100644 changelog/3067.trivial delete mode 100644 changelog/3074.bugfix delete mode 100644 changelog/3076.doc delete mode 100644 changelog/3088.bugfix delete mode 100644 changelog/3092.doc delete mode 100644 changelog/3101.feature delete mode 100644 changelog/3103.bugfix delete mode 100644 changelog/3107.feature delete mode 100644 changelog/3117.feature delete mode 100644 changelog/3129.trivial delete mode 100644 changelog/3130.feature delete mode 100644 changelog/3131.doc delete mode 100644 changelog/3138.feature delete mode 100644 changelog/3142.feature delete mode 100644 changelog/3143.doc create mode 100644 doc/en/announce/release-3.4.0.rst diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b72db97a9cf..d8186e65937 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,6 +8,138 @@ .. towncrier release notes start +Pytest 3.4.0 (2018-01-30) +========================= + +Deprecations and Removals +------------------------- + +- All pytest classes now subclass ``object`` for better Python 3 compatibility. + This should not affect user code except in very rare edge cases. (`#2147 + `_) + + +Features +-------- + +- Introduce ``empty_parameter_set_mark`` ini option to select which mark to + apply when ``@pytest.mark.parametrize`` is given an empty set of parameters. + Valid options are ``skip`` (default) and ``xfail``. Note that it is planned + to change the default to ``xfail`` in future releases as this is considered + less error prone. (`#2527 + `_) + +- **Incompatible change**: after community feedback the `logging + `_ functionality has + undergone some changes. Please consult the `logging documentation + `_ + for details. (`#3013 `_) + +- Console output fallsback to "classic" mode when capture is disabled (``-s``), + otherwise the output gets garbled to the point of being useless. (`#3038 + `_) + +- New `pytest_runtest_logfinish + `_ + hook which is called when a test item has finished executing, analogous to + `pytest_runtest_logstart + `_. + (`#3101 `_) + +- Improve performance when collecting tests using many fixtures. (`#3107 + `_) + +- New ``caplog.get_records(when)`` method which provides access the captured + records during each testing stage: ``"setup"``, ``"call"`` and ``"teardown"`` + stages. (`#3117 `_) + +- New fixture ``record_xml_attribute`` that allows modifying and inserting + attributes on the ```` xml node in JUnit reports. (`#3130 + `_) + +- The default cache directory has been renamed from ``.cache`` to + ``.pytest_cache`` after community feedback that the name ``.cache`` did not + make it clear that it was used by pytest. (`#3138 + `_) + +- Colorize the levelname column in the live-log output. (`#3142 + `_) + + +Bug Fixes +--------- + +- Fixed hanging pexpect test on MacOS by using flush() instead of wait(). + (`#2022 `_) + +- Fixed restoring Python state after in-process pytest runs with the + ``pytester`` plugin; this may break tests using making multiple inprocess + pytest runs if later ones depend on earlier ones leaking global interpreter + changes. (`#3016 `_) + +- Fix skipping plugin reporting hook when test aborted before plugin setup + hook. (`#3074 `_) + +- Fix progress percentage reported when tests fail during teardown. (`#3088 + `_) + +- **Incompatible change**: ``-o/--override`` option no longer eats all the + remaining options, which can lead to surprising behavior: for example, + ``pytest -o foo=1 /path/to/test.py`` would fail because ``/path/to/test.py`` + would be considered as part of the ``-o`` command-line argument. One + consequence of this is that now multiple configuration overrides need + multiple ``-o`` flags: ``pytest -o foo=1 -o bar=2``. (`#3103 + `_) + + +Improved Documentation +---------------------- + +- Document hooks (defined with ``historic=True``) which cannot be used with + ``hookwrapper=True``. (`#2423 + `_) + +- Clarify that warning capturing doesn't change the warning filter by default. + (`#2457 `_) + +- Clarify a possible confusion when using pytest_fixture_setup with fixture + functions that return None. (`#2698 + `_) + +- Fix the wording of a sentence on doctest flags use in pytest. (`#3076 + `_) + +- Prefer ``https://*.readthedocs.io`` over ``http://*.rtfd.org`` for links in + the documentation. (`#3092 + `_) + +- Improve readability (wording, grammar) of Getting Started guide (`#3131 + `_) + +- Added note that calling pytest.main multiple times from the same process is + not recommended because of import caching. (`#3143 + `_) + + +Trivial/Internal Changes +------------------------ + +- Show a simple and easy error when keyword expressions trigger a syntax error + (for example, ``"-k foo and import"`` will show an error that you can not use + the ``import`` keyword in expressions). (`#2953 + `_) + +- Change parametrized automatic test id generation to use the ``__name__`` + attribute of functions instead of the fallback argument name plus counter. + (`#2976 `_) + +- Replace py.std with stdlib imports. (`#3067 + `_) + +- Corrected 'you' to 'your' in logging docs. (`#3129 + `_) + + Pytest 3.3.2 (2017-12-25) ========================= diff --git a/changelog/2022.bugfix b/changelog/2022.bugfix deleted file mode 100644 index 67c9c8f7731..00000000000 --- a/changelog/2022.bugfix +++ /dev/null @@ -1 +0,0 @@ -Fixed hanging pexpect test on MacOS by using flush() instead of wait(). \ No newline at end of file diff --git a/changelog/2147.removal b/changelog/2147.removal deleted file mode 100644 index 8d2cfed5184..00000000000 --- a/changelog/2147.removal +++ /dev/null @@ -1 +0,0 @@ -All pytest classes now subclass ``object`` for better Python 3 compatibility. This should not affect user code except in very rare edge cases. diff --git a/changelog/2423.doc b/changelog/2423.doc deleted file mode 100644 index 96cc6829783..00000000000 --- a/changelog/2423.doc +++ /dev/null @@ -1 +0,0 @@ -Document hooks (defined with ``historic=True``) which cannot be used with ``hookwrapper=True``. diff --git a/changelog/2457.doc b/changelog/2457.doc deleted file mode 100644 index 31d7aa1c2df..00000000000 --- a/changelog/2457.doc +++ /dev/null @@ -1 +0,0 @@ -Clarify that warning capturing doesn't change the warning filter by default. \ No newline at end of file diff --git a/changelog/2527.feature b/changelog/2527.feature deleted file mode 100644 index ed00398d900..00000000000 --- a/changelog/2527.feature +++ /dev/null @@ -1 +0,0 @@ -Introduce ``empty_parameter_set_mark`` ini option to select which mark to apply when ``@pytest.mark.parametrize`` is given an empty set of parameters. Valid options are ``skip`` (default) and ``xfail``. Note that it is planned to change the default to ``xfail`` in future releases as this is considered less error prone. \ No newline at end of file diff --git a/changelog/2698.doc b/changelog/2698.doc deleted file mode 100644 index 3088b6efc60..00000000000 --- a/changelog/2698.doc +++ /dev/null @@ -1 +0,0 @@ -Clarify a possible confusion when using pytest_fixture_setup with fixture functions that return None. \ No newline at end of file diff --git a/changelog/2953.trivial b/changelog/2953.trivial deleted file mode 100644 index 25d9115c1a0..00000000000 --- a/changelog/2953.trivial +++ /dev/null @@ -1 +0,0 @@ -Show a simple and easy error when keyword expressions trigger a syntax error (for example, ``"-k foo and import"`` will show an error that you can not use the ``import`` keyword in expressions). diff --git a/changelog/2976.trivial b/changelog/2976.trivial deleted file mode 100644 index 5f767dd2740..00000000000 --- a/changelog/2976.trivial +++ /dev/null @@ -1 +0,0 @@ -Change parametrized automatic test id generation to use the ``__name__`` attribute of functions instead of the fallback argument name plus counter. diff --git a/changelog/3013.feature b/changelog/3013.feature deleted file mode 100644 index b690961db9f..00000000000 --- a/changelog/3013.feature +++ /dev/null @@ -1 +0,0 @@ -**Incompatible change**: after community feedback the `logging `_ functionality has undergone some changes. Please consult the `logging documentation `_ for details. diff --git a/changelog/3016.bugfix b/changelog/3016.bugfix deleted file mode 100644 index 1e2c86bdf0b..00000000000 --- a/changelog/3016.bugfix +++ /dev/null @@ -1,2 +0,0 @@ -Fixed restoring Python state after in-process pytest runs with the ``pytester`` plugin; this may break tests using -making multiple inprocess pytest runs if later ones depend on earlier ones leaking global interpreter changes. diff --git a/changelog/3038.feature b/changelog/3038.feature deleted file mode 100644 index a0da2eef31c..00000000000 --- a/changelog/3038.feature +++ /dev/null @@ -1 +0,0 @@ -Console output fallsback to "classic" mode when capture is disabled (``-s``), otherwise the output gets garbled to the point of being useless. diff --git a/changelog/3067.trivial b/changelog/3067.trivial deleted file mode 100644 index 2b7185100ed..00000000000 --- a/changelog/3067.trivial +++ /dev/null @@ -1 +0,0 @@ -Replace py.std with stdlib imports. diff --git a/changelog/3074.bugfix b/changelog/3074.bugfix deleted file mode 100644 index 814f26ff10d..00000000000 --- a/changelog/3074.bugfix +++ /dev/null @@ -1 +0,0 @@ -Fix skipping plugin reporting hook when test aborted before plugin setup hook. diff --git a/changelog/3076.doc b/changelog/3076.doc deleted file mode 100644 index 2958af78121..00000000000 --- a/changelog/3076.doc +++ /dev/null @@ -1 +0,0 @@ -Fix the wording of a sentence on doctest flags use in pytest. diff --git a/changelog/3088.bugfix b/changelog/3088.bugfix deleted file mode 100644 index 81b35157106..00000000000 --- a/changelog/3088.bugfix +++ /dev/null @@ -1 +0,0 @@ -Fix progress percentage reported when tests fail during teardown. diff --git a/changelog/3092.doc b/changelog/3092.doc deleted file mode 100644 index 6001b8e222a..00000000000 --- a/changelog/3092.doc +++ /dev/null @@ -1 +0,0 @@ -Prefer ``https://*.readthedocs.io`` over ``http://*.rtfd.org`` for links in the documentation. diff --git a/changelog/3101.feature b/changelog/3101.feature deleted file mode 100644 index 1ed0a8e08c3..00000000000 --- a/changelog/3101.feature +++ /dev/null @@ -1,3 +0,0 @@ -New `pytest_runtest_logfinish `_ -hook which is called when a test item has finished executing, analogous to -`pytest_runtest_logstart `_. diff --git a/changelog/3103.bugfix b/changelog/3103.bugfix deleted file mode 100644 index 4bdb23820e8..00000000000 --- a/changelog/3103.bugfix +++ /dev/null @@ -1 +0,0 @@ -**Incompatible change**: ``-o/--override`` option no longer eats all the remaining options, which can lead to surprising behavior: for example, ``pytest -o foo=1 /path/to/test.py`` would fail because ``/path/to/test.py`` would be considered as part of the ``-o`` command-line argument. One consequence of this is that now multiple configuration overrides need multiple ``-o`` flags: ``pytest -o foo=1 -o bar=2``. diff --git a/changelog/3107.feature b/changelog/3107.feature deleted file mode 100644 index 3a2e4e89242..00000000000 --- a/changelog/3107.feature +++ /dev/null @@ -1 +0,0 @@ -Improve performance when collecting tests using many fixtures. \ No newline at end of file diff --git a/changelog/3117.feature b/changelog/3117.feature deleted file mode 100644 index f428ed75dbd..00000000000 --- a/changelog/3117.feature +++ /dev/null @@ -1 +0,0 @@ -New ``caplog.get_records(when)`` method which provides access the captured records during each testing stage: ``"setup"``, ``"call"`` and ``"teardown"`` stages. diff --git a/changelog/3129.trivial b/changelog/3129.trivial deleted file mode 100644 index 65958660c9f..00000000000 --- a/changelog/3129.trivial +++ /dev/null @@ -1 +0,0 @@ -Corrected 'you' to 'your' in logging docs. diff --git a/changelog/3130.feature b/changelog/3130.feature deleted file mode 100644 index af2c235887b..00000000000 --- a/changelog/3130.feature +++ /dev/null @@ -1 +0,0 @@ -New fixture ``record_xml_attribute`` that allows modifying and inserting attributes on the ```` xml node in JUnit reports. diff --git a/changelog/3131.doc b/changelog/3131.doc deleted file mode 100644 index 28e61c1d8bf..00000000000 --- a/changelog/3131.doc +++ /dev/null @@ -1 +0,0 @@ -Improve readability (wording, grammar) of Getting Started guide diff --git a/changelog/3138.feature b/changelog/3138.feature deleted file mode 100644 index 338d429f9f9..00000000000 --- a/changelog/3138.feature +++ /dev/null @@ -1 +0,0 @@ -The default cache directory has been renamed from ``.cache`` to ``.pytest_cache`` after community feedback that the name ``.cache`` did not make it clear that it was used by pytest. diff --git a/changelog/3142.feature b/changelog/3142.feature deleted file mode 100644 index 1461be5143c..00000000000 --- a/changelog/3142.feature +++ /dev/null @@ -1 +0,0 @@ -Colorize the levelname column in the live-log output. \ No newline at end of file diff --git a/changelog/3143.doc b/changelog/3143.doc deleted file mode 100644 index b454708ad7f..00000000000 --- a/changelog/3143.doc +++ /dev/null @@ -1 +0,0 @@ -Added note that calling pytest.main multiple times from the same process is not recommended because of import caching. \ No newline at end of file diff --git a/doc/en/announce/index.rst b/doc/en/announce/index.rst index bc8d46f1f80..4f3ec8b4e52 100644 --- a/doc/en/announce/index.rst +++ b/doc/en/announce/index.rst @@ -6,6 +6,7 @@ Release announcements :maxdepth: 2 + release-3.4.0 release-3.3.2 release-3.3.1 release-3.3.0 diff --git a/doc/en/announce/release-3.4.0.rst b/doc/en/announce/release-3.4.0.rst new file mode 100644 index 00000000000..df1e004f1cc --- /dev/null +++ b/doc/en/announce/release-3.4.0.rst @@ -0,0 +1,52 @@ +pytest-3.4.0 +======================================= + +The pytest team is proud to announce the 3.4.0 release! + +pytest is a mature Python testing tool with more than a 1600 tests +against itself, passing on many different interpreters and platforms. + +This release contains a number of bugs fixes and improvements, so users are encouraged +to take a look at the CHANGELOG: + + http://doc.pytest.org/en/latest/changelog.html + +For complete documentation, please visit: + + http://docs.pytest.org + +As usual, you can upgrade from pypi via: + + pip install -U pytest + +Thanks to all who contributed to this release, among them: + +* Aaron +* Alan Velasco +* Anders Hovmöller +* Andrew Toolan +* Anthony Sottile +* Aron Coyle +* Brian Maissy +* Bruno Oliveira +* Cyrus Maden +* Florian Bruhin +* Henk-Jaap Wagenaar +* Ian Lesperance +* Jon Dufresne +* Jurko Gospodnetić +* Kate +* Kimberly +* Per A. Brodtkorb +* Pierre-Alexandre Fonta +* Raphael Castaneda +* Ronny Pfannschmidt +* ST John +* Segev Finer +* Thomas Hisch +* Tzu-ping Chung +* feuillemorte + + +Happy testing, +The Pytest Development Team diff --git a/doc/en/builtin.rst b/doc/en/builtin.rst index d11eb5606e0..a380b9abd18 100644 --- a/doc/en/builtin.rst +++ b/doc/en/builtin.rst @@ -116,6 +116,10 @@ You can ask for available builtin or project-custom Add extra xml properties to the tag for the calling test. The fixture is callable with ``(name, value)``, with value being automatically xml-encoded. + record_xml_attribute + Add extra xml attributes to the tag for the calling test. + The fixture is callable with ``(name, value)``, with value being automatically + xml-encoded caplog Access and control log capturing. diff --git a/doc/en/cache.rst b/doc/en/cache.rst index c88721b11b0..e3423e95b0a 100644 --- a/doc/en/cache.rst +++ b/doc/en/cache.rst @@ -225,7 +225,7 @@ You can always peek at the content of the cache using the =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: - cachedir: $REGENDOC_TMPDIR/.cache + cachedir: $REGENDOC_TMPDIR/.pytest_cache ------------------------------- cache values ------------------------------- cache/lastfailed contains: {'test_caching.py::test_function': True} diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index 43c20d5b7dc..cbbb3463366 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -32,7 +32,7 @@ You can then restrict a test run to only run tests marked with ``webtest``:: $ pytest -v -m webtest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 4 items @@ -46,7 +46,7 @@ Or the inverse, running all tests except the webtest ones:: $ pytest -v -m "not webtest" =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 4 items @@ -67,7 +67,7 @@ tests based on their module, class, method, or function name:: $ pytest -v test_server.py::TestClass::test_method =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 1 item @@ -80,7 +80,7 @@ You can also select on the class:: $ pytest -v test_server.py::TestClass =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 1 item @@ -93,7 +93,7 @@ Or select multiple nodes:: $ pytest -v test_server.py::TestClass test_server.py::test_send_http =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items @@ -131,7 +131,7 @@ select tests based on their names:: $ pytest -v -k http # running with the above defined example module =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 4 items @@ -145,7 +145,7 @@ And you can also run all tests except the ones that match the keyword:: $ pytest -k "not send_http" -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 4 items @@ -161,7 +161,7 @@ Or to select "http" and "quick" tests:: $ pytest -k "http or quick" -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 4 items @@ -432,7 +432,7 @@ The output is as follows:: $ pytest -q -s Marker info name=my_marker args=(,) kwars={} - . [100%] + . 1 passed in 0.12 seconds We can see that the custom marker has its argument set extended with the function ``hello_world``. This is the key difference between creating a custom marker as a callable, which invokes ``__call__`` behind the scenes, and using ``with_args``. @@ -477,7 +477,7 @@ Let's run this without capturing output and see what we get:: glob args=('function',) kwargs={'x': 3} glob args=('class',) kwargs={'x': 2} glob args=('module',) kwargs={'x': 1} - . [100%] + . 1 passed in 0.12 seconds marking platform specific tests with pytest diff --git a/doc/en/example/nonpython.rst b/doc/en/example/nonpython.rst index cf72c7219e1..dd25e888f57 100644 --- a/doc/en/example/nonpython.rst +++ b/doc/en/example/nonpython.rst @@ -60,7 +60,7 @@ consulted when reporting in ``verbose`` mode:: nonpython $ pytest -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR/nonpython, inifile: collecting ... collected 2 items diff --git a/doc/en/example/reportingdemo.rst b/doc/en/example/reportingdemo.rst index 9964d67f289..b0c25dedca6 100644 --- a/doc/en/example/reportingdemo.rst +++ b/doc/en/example/reportingdemo.rst @@ -411,6 +411,8 @@ get on the terminal - we are working on that):: ____________________ test_dynamic_compile_shows_nicely _____________________ def test_dynamic_compile_shows_nicely(): + import imp + import sys src = 'def foo():\n assert 1 == 0\n' name = 'abc-123' module = imp.new_module(name) @@ -419,14 +421,14 @@ get on the terminal - we are working on that):: sys.modules[name] = module > module.foo() - failure_demo.py:166: + failure_demo.py:168: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def foo(): > assert 1 == 0 E AssertionError - <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:163>:2: AssertionError + <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:165>:2: AssertionError ____________________ TestMoreErrors.test_complex_error _____________________ self = @@ -438,7 +440,7 @@ get on the terminal - we are working on that):: return 43 > somefunc(f(), g()) - failure_demo.py:176: + failure_demo.py:178: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ failure_demo.py:9: in somefunc otherfunc(x,y) @@ -460,7 +462,7 @@ get on the terminal - we are working on that):: > a,b = l E ValueError: not enough values to unpack (expected 2, got 0) - failure_demo.py:180: ValueError + failure_demo.py:182: ValueError ____________________ TestMoreErrors.test_z2_type_error _____________________ self = @@ -470,7 +472,7 @@ get on the terminal - we are working on that):: > a,b = l E TypeError: 'int' object is not iterable - failure_demo.py:184: TypeError + failure_demo.py:186: TypeError ______________________ TestMoreErrors.test_startswith ______________________ self = @@ -483,7 +485,7 @@ get on the terminal - we are working on that):: E + where False = ('456') E + where = '123'.startswith - failure_demo.py:189: AssertionError + failure_demo.py:191: AssertionError __________________ TestMoreErrors.test_startswith_nested ___________________ self = @@ -500,7 +502,7 @@ get on the terminal - we are working on that):: E + where '123' = .f at 0xdeadbeef>() E + and '456' = .g at 0xdeadbeef>() - failure_demo.py:196: AssertionError + failure_demo.py:198: AssertionError _____________________ TestMoreErrors.test_global_func ______________________ self = @@ -511,7 +513,7 @@ get on the terminal - we are working on that):: E + where False = isinstance(43, float) E + where 43 = globf(42) - failure_demo.py:199: AssertionError + failure_demo.py:201: AssertionError _______________________ TestMoreErrors.test_instance _______________________ self = @@ -522,7 +524,7 @@ get on the terminal - we are working on that):: E assert 42 != 42 E + where 42 = .x - failure_demo.py:203: AssertionError + failure_demo.py:205: AssertionError _______________________ TestMoreErrors.test_compare ________________________ self = @@ -532,7 +534,7 @@ get on the terminal - we are working on that):: E assert 11 < 5 E + where 11 = globf(10) - failure_demo.py:206: AssertionError + failure_demo.py:208: AssertionError _____________________ TestMoreErrors.test_try_finally ______________________ self = @@ -543,7 +545,7 @@ get on the terminal - we are working on that):: > assert x == 0 E assert 1 == 0 - failure_demo.py:211: AssertionError + failure_demo.py:213: AssertionError ___________________ TestCustomAssertMsg.test_single_line ___________________ self = @@ -557,7 +559,7 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = .A'>.a - failure_demo.py:222: AssertionError + failure_demo.py:224: AssertionError ____________________ TestCustomAssertMsg.test_multiline ____________________ self = @@ -574,7 +576,7 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = .A'>.a - failure_demo.py:228: AssertionError + failure_demo.py:230: AssertionError ___________________ TestCustomAssertMsg.test_custom_repr ___________________ self = @@ -594,7 +596,7 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = This is JSON\n{\n 'foo': 'bar'\n}.a - failure_demo.py:238: AssertionError + failure_demo.py:240: AssertionError ============================= warnings summary ============================= None Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0. diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 5dbf0a5198b..ffc68b29629 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -332,7 +332,7 @@ which will add info only when run with "--v":: $ pytest -v =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache info1: did you know that ... did you? rootdir: $REGENDOC_TMPDIR, inifile: @@ -385,9 +385,9 @@ Now we can profile which test functions execute the slowest:: test_some_are_slow.py ... [100%] ========================= slowest 3 test durations ========================= - 0.31s call test_some_are_slow.py::test_funcslow2 - 0.20s call test_some_are_slow.py::test_funcslow1 - 0.17s call test_some_are_slow.py::test_funcfast + 0.58s call test_some_are_slow.py::test_funcslow2 + 0.41s call test_some_are_slow.py::test_funcslow1 + 0.10s call test_some_are_slow.py::test_funcfast ========================= 3 passed in 0.12 seconds ========================= incremental testing - test steps @@ -537,7 +537,7 @@ We can run this:: file $REGENDOC_TMPDIR/b/test_error.py, line 1 def test_root(db): # no db here, will error out E fixture 'db' not found - > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory + > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_attribute, record_xml_property, recwarn, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. $REGENDOC_TMPDIR/b/test_error.py:1 @@ -731,7 +731,7 @@ and run it:: test_module.py Esetting up a test failed! test_module.py::test_setup_fails Fexecuting test failed test_module.py::test_call_fails - F [100%] + F ================================== ERRORS ================================== ____________________ ERROR at setup of test_setup_fails ____________________ diff --git a/doc/en/example/special.rst b/doc/en/example/special.rst index 4437e1cc30e..1fc32f6c817 100644 --- a/doc/en/example/special.rst +++ b/doc/en/example/special.rst @@ -68,5 +68,5 @@ If you run this without output capturing:: .test_method1 called .test other .test_unit1 method called - . [100%] + . 4 passed in 0.12 seconds diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 01a941ddf65..0828bdcf80b 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -286,7 +286,7 @@ tests. Let's execute it:: $ pytest -s -q --tb=no - FF [100%]teardown smtp + FFteardown smtp 2 failed in 0.12 seconds @@ -391,7 +391,7 @@ We use the ``request.module`` attribute to optionally obtain an again, nothing much has changed:: $ pytest -s -q --tb=no - FF [100%]finalizing (smtp.gmail.com) + FFfinalizing (smtp.gmail.com) 2 failed in 0.12 seconds @@ -612,7 +612,7 @@ Here we declare an ``app`` fixture which receives the previously defined $ pytest -v test_appsetup.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items @@ -681,40 +681,40 @@ Let's run the tests in verbose mode and with looking at the print-output:: $ pytest -v -s test_module.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5 - cachedir: .cache + cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 8 items test_module.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 - PASSED [ 12%] TEARDOWN otherarg 1 + PASSED TEARDOWN otherarg 1 test_module.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 - PASSED [ 25%] TEARDOWN otherarg 2 + PASSED TEARDOWN otherarg 2 test_module.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 - PASSED [ 37%] + PASSED test_module.py::test_2[1-mod1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 - PASSED [ 50%] TEARDOWN otherarg 1 + PASSED TEARDOWN otherarg 1 test_module.py::test_2[2-mod1] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 - PASSED [ 62%] TEARDOWN otherarg 2 + PASSED TEARDOWN otherarg 2 test_module.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 - PASSED [ 75%] + PASSED test_module.py::test_2[1-mod2] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 - PASSED [ 87%] TEARDOWN otherarg 1 + PASSED TEARDOWN otherarg 1 test_module.py::test_2[2-mod2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 - PASSED [100%] TEARDOWN otherarg 2 + PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2 diff --git a/doc/en/usage.rst b/doc/en/usage.rst index 417e507932d..abd8bac2bd8 100644 --- a/doc/en/usage.rst +++ b/doc/en/usage.rst @@ -447,6 +447,7 @@ hook was invoked:: $ python myinvoke.py *** test run reporting finishing + .. note:: From 527845ef298d94b1e101684bb52e5eac3ce53485 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 29 Jan 2018 18:57:30 -0200 Subject: [PATCH 48/50] Changelog adjustments suggested during review --- CHANGELOG.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d8186e65937..fa075943792 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,7 +14,7 @@ Pytest 3.4.0 (2018-01-30) Deprecations and Removals ------------------------- -- All pytest classes now subclass ``object`` for better Python 3 compatibility. +- All pytest classes now subclass ``object`` for better Python 2/3 compatibility. This should not affect user code except in very rare edge cases. (`#2147 `_) @@ -35,7 +35,7 @@ Features `_ for details. (`#3013 `_) -- Console output fallsback to "classic" mode when capture is disabled (``-s``), +- Console output falls back to "classic" mode when capturing is disabled (``-s``), otherwise the output gets garbled to the point of being useless. (`#3038 `_) @@ -49,9 +49,9 @@ Features - Improve performance when collecting tests using many fixtures. (`#3107 `_) -- New ``caplog.get_records(when)`` method which provides access the captured - records during each testing stage: ``"setup"``, ``"call"`` and ``"teardown"`` - stages. (`#3117 `_) +- New ``caplog.get_records(when)`` method which provides access to the captured + records for the ``"setup"``, ``"call"`` and ``"teardown"`` + testing stages. (`#3117 `_) - New fixture ``record_xml_attribute`` that allows modifying and inserting attributes on the ```` xml node in JUnit reports. (`#3130 @@ -69,11 +69,11 @@ Features Bug Fixes --------- -- Fixed hanging pexpect test on MacOS by using flush() instead of wait(). +- Fix hanging pexpect test on MacOS by using flush() instead of wait(). (`#2022 `_) -- Fixed restoring Python state after in-process pytest runs with the - ``pytester`` plugin; this may break tests using making multiple inprocess +- Fix restoring Python state after in-process pytest runs with the + ``pytester`` plugin; this may break tests using multiple inprocess pytest runs if later ones depend on earlier ones leaking global interpreter changes. (`#3016 `_) @@ -106,7 +106,7 @@ Improved Documentation functions that return None. (`#2698 `_) -- Fix the wording of a sentence on doctest flags use in pytest. (`#3076 +- Fix the wording of a sentence on doctest flags used in pytest. (`#3076 `_) - Prefer ``https://*.readthedocs.io`` over ``http://*.rtfd.org`` for links in From 3256fa9ee9345baa63dc959ecc527435cbfd8393 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 30 Jan 2018 20:10:40 -0200 Subject: [PATCH 49/50] Add devpi-client to tasks requirements --- tasks/requirements.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tasks/requirements.txt b/tasks/requirements.txt index 6392de0cc0a..be4bff99060 100644 --- a/tasks/requirements.txt +++ b/tasks/requirements.txt @@ -1,5 +1,6 @@ -invoke -tox +devpi-client gitpython +invoke towncrier +tox wheel From 2e6ffbb8e86c0f0208d8948eeb43fcb226840d17 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Fri, 26 Jan 2018 09:28:23 +0100 Subject: [PATCH 50/50] Add captured log msgs to junit xml file For each test this adds the captured log msgs to a system-* tag in the junit xml output file. The destination of the system-* tag is specified by junit_logging ini option. --- _pytest/junitxml.py | 54 +++++++++++++++++++++++++++++++++++----- _pytest/runner.py | 8 ++++++ changelog/3156.feature | 1 + testing/test_junitxml.py | 23 ++++++++++++++--- 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 changelog/3156.feature diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index e929eeba8e4..9593b7bd05f 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -130,10 +130,47 @@ def _add_simple(self, kind, message, data=None): self.append(node) def write_captured_output(self, report): - for capname in ('out', 'err'): - content = getattr(report, 'capstd' + capname) + content_out = report.capstdout + content_log = report.caplog + content_err = report.capstderr + + if content_log or content_out: + if content_log and self.xml.logging == 'system-out': + if content_out: + # syncing stdout and the log-output is not done yet. It's + # probably not worth the effort. Therefore, first the captured + # stdout is shown and then the captured logs. + content = '\n'.join([ + ' Captured Stdout '.center(80, '-'), + content_out, + '', + ' Captured Log '.center(80, '-'), + content_log]) + else: + content = content_log + else: + content = content_out + + if content: + tag = getattr(Junit, 'system-out') + self.append(tag(bin_xml_escape(content))) + + if content_log or content_err: + if content_log and self.xml.logging == 'system-err': + if content_err: + content = '\n'.join([ + ' Captured Stderr '.center(80, '-'), + content_err, + '', + ' Captured Log '.center(80, '-'), + content_log]) + else: + content = content_log + else: + content = content_err + if content: - tag = getattr(Junit, 'system-' + capname) + tag = getattr(Junit, 'system-err') self.append(tag(bin_xml_escape(content))) def append_pass(self, report): @@ -254,13 +291,17 @@ def pytest_addoption(parser): default=None, help="prepend prefix to classnames in junit-xml output") parser.addini("junit_suite_name", "Test suite name for JUnit report", default="pytest") - + parser.addini("junit_logging", "Write captured log messages to JUnit report: " + "one of no|system-out|system-err", + default="no") # choices=['no', 'stdout', 'stderr']) def pytest_configure(config): xmlpath = config.option.xmlpath # prevent opening xmllog on slave nodes (xdist) if xmlpath and not hasattr(config, 'slaveinput'): - config._xml = LogXML(xmlpath, config.option.junitprefix, config.getini("junit_suite_name")) + config._xml = LogXML(xmlpath, config.option.junitprefix, + config.getini("junit_suite_name"), + config.getini("junit_logging")) config.pluginmanager.register(config._xml) @@ -287,11 +328,12 @@ def mangle_test_address(address): class LogXML(object): - def __init__(self, logfile, prefix, suite_name="pytest"): + def __init__(self, logfile, prefix, suite_name="pytest", logging="no"): logfile = os.path.expanduser(os.path.expandvars(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix self.suite_name = suite_name + self.logging = logging self.stats = dict.fromkeys([ 'error', 'passed', diff --git a/_pytest/runner.py b/_pytest/runner.py index d82865b7684..fda8c785a81 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -256,6 +256,14 @@ def longreprtext(self): exc = tw.stringio.getvalue() return exc.strip() + @property + def caplog(self): + """Return captured log lines, if log capturing is enabled + + .. versionadded:: 3.4 + """ + return '\n'.join(content for (prefix, content) in self.get_sections('Captured log')) + @property def capstdout(self): """Return captured text from stdout, if capturing is enabled diff --git a/changelog/3156.feature b/changelog/3156.feature new file mode 100644 index 00000000000..125605b38bf --- /dev/null +++ b/changelog/3156.feature @@ -0,0 +1 @@ +Captured log messages are added to the ```` tag in the generated junit xml file if the ``junit_logging`` ini option is set to ``system-out``. If the value of this ini option is ``system-err`, the logs are written to ````. The default value for ``junit_logging`` is ``no``, meaning captured logs are not written to the output file. diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 49318ef762d..031caeb206a 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -328,23 +328,28 @@ def test_internal_error(self, testdir): fnode.assert_attr(message="internal error") assert "Division" in fnode.toxml() - def test_failure_function(self, testdir): + @pytest.mark.parametrize('junit_logging', ['no', 'system-out', 'system-err']) + def test_failure_function(self, testdir, junit_logging): testdir.makepyfile(""" + import logging import sys + def test_fail(): print ("hello-stdout") sys.stderr.write("hello-stderr\\n") + logging.info('info msg') + logging.warning('warning msg') raise ValueError(42) """) - result, dom = runandparse(testdir) + result, dom = runandparse(testdir, '-o', 'junit_logging=%s' % junit_logging) assert result.ret node = dom.find_first_by_tag("testsuite") node.assert_attr(failures=1, tests=1) tnode = node.find_first_by_tag("testcase") tnode.assert_attr( file="test_failure_function.py", - line="1", + line="3", classname="test_failure_function", name="test_fail") fnode = tnode.find_first_by_tag("failure") @@ -353,9 +358,21 @@ def test_fail(): systemout = fnode.next_siebling assert systemout.tag == "system-out" assert "hello-stdout" in systemout.toxml() + assert "info msg" not in systemout.toxml() systemerr = systemout.next_siebling assert systemerr.tag == "system-err" assert "hello-stderr" in systemerr.toxml() + assert "info msg" not in systemerr.toxml() + + if junit_logging == 'system-out': + assert "warning msg" in systemout.toxml() + assert "warning msg" not in systemerr.toxml() + elif junit_logging == 'system-err': + assert "warning msg" not in systemout.toxml() + assert "warning msg" in systemerr.toxml() + elif junit_logging == 'no': + assert "warning msg" not in systemout.toxml() + assert "warning msg" not in systemerr.toxml() def test_failure_verbose_message(self, testdir): testdir.makepyfile("""