Merge remote-tracking branch 'upstream/features' into invocation-scoped-fixtures

This commit is contained in:
Bruno Oliveira 2016-07-20 21:16:27 -03:00
commit 4f2bf965cb
49 changed files with 1132 additions and 1220 deletions

View File

@ -9,7 +9,9 @@ Alexei Kozlenok
Anatoly Bubenkoff Anatoly Bubenkoff
Andreas Zeidler Andreas Zeidler
Andy Freeland Andy Freeland
Andrzej Ostrowski
Anthon van der Neut Anthon van der Neut
Antony Lee
Armin Rigo Armin Rigo
Aron Curzon Aron Curzon
Aviv Palivoda Aviv Palivoda
@ -59,6 +61,7 @@ Jaap Broekhuizen
Jan Balster Jan Balster
Janne Vanhala Janne Vanhala
Jason R. Coombs Jason R. Coombs
Javier Domingo Cansino
John Towler John Towler
Joshua Bronson Joshua Bronson
Jurko Gospodnetić Jurko Gospodnetić

View File

@ -3,46 +3,56 @@
**Incompatible changes** **Incompatible changes**
* Removed the following deprecated commandline options A number of incompatible changes were made in this release, with the intent of removing features deprecated for a long
time or change existing behaviors in order to make them less surprising/more useful.
* ``--genscript`` * Reinterpretation mode has now been removed. Only plain and rewrite
* ``--no-assert`` mode are available, consequently the ``--assert=reinterp`` option is
* ``--nomagic`` no longer available. Thanks `@flub`_ for the PR.
* ``--report``
Thanks to `@RedBeardCode`_ for the PR (`#1664`_) * 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;
Thanks to `@RedBeardCode`_ for the PR (`#1664`_).
* ImportErrors in plugins now are a fatal error instead of issuing a * ImportErrors in plugins now are a fatal error instead of issuing a
pytest warning (`#1479`_). Thanks to `@The-Compiler`_ for the PR. pytest warning (`#1479`_). Thanks to `@The-Compiler`_ for the PR.
* removed support code for python 3 < 3.3 addressing (`#1627`_) * Removed support code for Python 3 versions < 3.3 (`#1627`_).
* Remove all py.test-X* entry points. The versioned, suffixed entry points * 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 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 points also created broken entry points in wheels, so removing them also
removes a source of confusion for users (`#1632`_). removes a source of confusion for users (`#1632`_).
Thanks `@obestwalter`_ for the PR. Thanks `@obestwalter`_ for the PR.
* Fix `#607`_: pytest.skip() is no longer allowed at module level to * ``pytest.skip()`` now raises an error when used to decorate a test function,
prevent misleading use as test function decorator. When used at a module as opposed to its original intent (to imperatively skip a test inside a test function). Previously
level an error will be raised during collection. this usage would cause the entire module to be skipped (`#607`_).
Thanks `@omarkohl`_ for the complete PR (`#1519`_). Thanks `@omarkohl`_ for the complete PR (`#1519`_).
* Fix `#1421`_: Exit tests if a collection error occurs and add * Exit tests if a collection error occurs. A poll indicated most users will hit CTRL-C
``--continue-on-collection-errors`` option to restore previous behaviour. anyway as soon as they see collection errors, so pytest might as well make that the default behavior (`#1421`_).
A ``--continue-on-collection-errors`` option has been added to restore the previous behaviour.
Thanks `@olegpidsadnyi`_ and `@omarkohl`_ for the complete PR (`#1628`_). Thanks `@olegpidsadnyi`_ and `@omarkohl`_ for the complete PR (`#1628`_).
* Renamed the pytest ``pdb`` module (plugin) into ``debugging``. * Renamed the pytest ``pdb`` module (plugin) into ``debugging`` to avoid clashes with the builtin ``pdb`` module.
* Raise helpful failure message, when requesting parametrized fixture at runtime, * Raise a helpful failure message when requesting a parametrized fixture at runtime,
e.g. with ``request.getfuncargvalue``. Previously these params were simply e.g. with ``request.getfixturevalue``. Previously these parameters were simply
never defined. So a fixture decorated like ``@pytest.fixture(params=[0, 1, never defined, so a fixture decorated like ``@pytest.fixture(params=[0, 1, 2])``
2])`` only ran once. Now a failure is raised. Fixes `#460`_. Thanks to only ran once (`#460`_).
`@nikratio`_ for bug report, `@RedBeardCode`_ and `@tomviner`_ for PR. Thanks to `@nikratio`_ for the bug report, `@RedBeardCode`_ and `@tomviner`_ for the PR.
* * ``_pytest.monkeypatch.monkeypatch`` class has been renamed to ``_pytest.monkeypatch.MonkeyPatch``
so it doesn't conflict with the ``monkeypatch`` fixture.
* * ``--exitfirst / -x`` can now be overridden by a following ``--maxfail=N``
and is just a synonym for ``--maxfail=1``.
* *
@ -53,14 +63,14 @@
**New Features** **New Features**
* Support nose-style ``__test__`` attribute on methods of classes, * Support nose-style ``__test__`` attribute on methods of classes,
including unittest-style Classes. If set to False, the test will not be including unittest-style Classes. If set to ``False``, the test will not be
collected. collected.
* New ``doctest_namespace`` fixture for injecting names into the * New ``doctest_namespace`` fixture for injecting names into the
namespace in which your doctests run. namespace in which doctests run.
Thanks `@milliams`_ for the complete PR (`#1428`_). Thanks `@milliams`_ for the complete PR (`#1428`_).
* New ``name`` argument to ``pytest.fixture`` mark, which allows a custom name * New ``name`` argument to ``pytest.fixture`` decorator which allows a custom name
for a fixture (to solve the funcarg-shadowing-fixture problem). for a fixture (to solve the funcarg-shadowing-fixture problem).
Thanks `@novas0x2a`_ for the complete PR (`#1444`_). Thanks `@novas0x2a`_ for the complete PR (`#1444`_).
@ -68,54 +78,63 @@
tests. tests.
Thanks `@kalekundert`_ for the complete PR (`#1441`_). Thanks `@kalekundert`_ for the complete PR (`#1441`_).
* New Add ability to add global properties in the final xunit output file. * Ability to add global properties in the final xunit output file by accessing
the internal ``junitxml`` plugin (experimental).
Thanks `@tareqalayan`_ for the complete PR `#1454`_). Thanks `@tareqalayan`_ for the complete PR `#1454`_).
* New ``ExceptionInfo.match()`` method to match a regular expression on the * New ``ExceptionInfo.match()`` method to match a regular expression on the
string representation of an exception. Closes proposal `#372`_. string representation of an exception (`#372`_).
Thanks `@omarkohl`_ for the complete PR (`#1502`_) and `@nicoddemus`_ for the Thanks `@omarkohl`_ for the complete PR (`#1502`_).
implementation tips.
* ``__tracebackhide__`` can now also be set to a callable which then can decide * ``__tracebackhide__`` can now also be set to a callable which then can decide
whether to filter the traceback based on the ``ExceptionInfo`` object passed whether to filter the traceback based on the ``ExceptionInfo`` object passed
to it. Thanks `@The-Compiler`_ for the complete PR (`#1526`_). to it. Thanks `@The-Compiler`_ for the complete PR (`#1526`_).
* New ``pytest_make_parametrize_id`` hook. * New ``pytest_make_parametrize_id(config, val)`` hook which can be used by plugins to provide
friendly strings for custom types.
Thanks `@palaviv`_ for the PR. Thanks `@palaviv`_ for the PR.
* ``capsys`` and ``capfd`` now have a ``disabled()`` method, which is a context manager * ``capsys`` and ``capfd`` now have a ``disabled()`` context-manager method, which
that can be used to temporarily disable capture within a test. can be used to temporarily disable capture within a test.
Thanks `@nicoddemus`_ for the PR. Thanks `@nicoddemus`_ for the PR.
* New cli flag ``--fixtures-per-test`` that shows which fixtures are being used * New cli flag ``--fixtures-per-test``: shows which fixtures are being used
for each selected test item. Features doc strings of fixtures by default. for each selected test item. Features doc strings of fixtures by default.
Can also show where fixtures are defined if combined with ``-v``. Can also show where fixtures are defined if combined with ``-v``.
Thanks `@hackebrot`_ for the PR. Thanks `@hackebrot`_ for the PR.
* Introduce pytest command as recommended entry point. Closes proposal * Introduce ``pytest`` command as recommended entry point. Note that ``py.test``
still works and is not scheduled for removal. Closes proposal
`#1629`_. Thanks `@obestwalter`_ and `@davehunt`_ for the complete PR `#1629`_. Thanks `@obestwalter`_ and `@davehunt`_ for the complete PR
(`#1633`_) (`#1633`_).
* New cli flags:
+ ``--setup-plan``: performs normal collection and reports
the potential setup and teardown and does not execute any fixtures and tests;
+ ``--setup-only``: performs normal collection, executes setup and teardown of
fixtures and reports them;
+ ``--setup-show``: performs normal test execution and additionally shows
setup and teardown of fixtures;
* New cli flags: (1) ``--setup-plan`` performs normal collection and reports
the potential setup and teardown, does not execute any fixtures and tests (2)
``--setup-only`` performs normal collection, executes setup and teardown of
fixtures and reports them. (3) ``--setup-show`` performs normal test
execution and additionally shows the setup and teardown of fixtures.
Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ and `@omarkohl`_ for the PRs. Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ and `@omarkohl`_ for the PRs.
* Added two new hooks: ``pytest_fixture_setup`` which executes the fixture * New cli flag ``--override-ini``/``-o``: overrides values from the ini file.
setup and ``pytest_fixture_post_finalizer`` which is called after the fixture's For example: ``"-o xfail_strict=True"``'.
finalizer and has access to the fixture's result cache. Thanks `@blueyed`_ and `@fengxx`_ for the PR.
Thanks `@d6e`_, `@sallner`_
* Issue a warning for asserts whose test is a tuple literal. Such asserts will * New hooks:
+ ``pytest_fixture_setup(fixturedef, request)``: executes fixture setup;
+ ``pytest_fixture_post_finalizer(fixturedef)``: called after the fixture's
finalizer and has access to the fixture's result cache.
Thanks `@d6e`_, `@sallner`_.
* Issue warnings for asserts whose test is a tuple literal. Such asserts will
never fail because tuples are always truthy and are usually a mistake never fail because tuples are always truthy and are usually a mistake
(see `#1562`_). Thanks `@kvas-it`_, for the PR. (see `#1562`_). Thanks `@kvas-it`_, for the PR.
* New cli flag ``--override-ini`` or ``-o`` that overrides values from the ini file.
Example '-o xfail_strict=True'. A complete ini-options can be viewed
by py.test --help. Thanks `@blueyed`_ and `@fengxx`_ for the PR
* Experimentally introduce new ``"invocation"`` fixture scope. At invocation scope a * Experimentally introduce new ``"invocation"`` fixture scope. At invocation scope a
fixture function is cached in the same way as the fixture or test function that requests it. fixture function is cached in the same way as the fixture or test function that requests it.
You can now use the builtin ``monkeypatch`` fixture from ``session``-scoped fixtures You can now use the builtin ``monkeypatch`` fixture from ``session``-scoped fixtures
@ -123,6 +142,10 @@
``session``-scoped one.* ``session``-scoped one.*
Thanks `@nicoddemus`_ for the PR. Thanks `@nicoddemus`_ for the PR.
* Allow passing a custom debugger class (e.g. ``--pdbcls=IPython.core.debugger:Pdb``).
Thanks to `@anntzer`_ for the PR.
* *
* *
@ -133,33 +156,42 @@
**Changes** **Changes**
* Plugins now benefit from assertion rewriting. Thanks
`@sober7`_, `@nicoddemus`_ and `@flub`_ for the PR.
* Fixtures marked with ``@pytest.fixture`` can now use ``yield`` statements exactly like * Fixtures marked with ``@pytest.fixture`` can now use ``yield`` statements exactly like
those marked with the ``@pytest.yield_fixture`` decorator. This change renders those marked with the ``@pytest.yield_fixture`` decorator. This change renders
``@pytest.yield_fixture`` deprecated and makes ``@pytest.fixture`` with ``yield`` statements ``@pytest.yield_fixture`` deprecated and makes ``@pytest.fixture`` with ``yield`` statements
the preferred way to write teardown code (`#1461`_). the preferred way to write teardown code (`#1461`_).
Thanks `@csaftoiu`_ for bringing this to attention and `@nicoddemus`_ for the PR. Thanks `@csaftoiu`_ for bringing this to attention and `@nicoddemus`_ for the PR.
* Fix `#1351`_: * Explicitly passed parametrize ids do not get escaped to ascii (`#1351`_).
explicitly passed parametrize ids do not get escaped to ascii.
Thanks `@ceridwen`_ for the PR. Thanks `@ceridwen`_ for the PR.
* parametrize ids can accept None as specific test id. The * Parametrize ids can accept ``None`` as specific test id, in which case the
automatically generated id for that argument will be used. automatically generated id for that argument will be used.
Thanks `@palaviv`_ for the complete PR (`#1468`_). Thanks `@palaviv`_ for the complete PR (`#1468`_).
* improved idmaker name selection in case of duplicate ids in * The parameter to xunit-style setup/teardown methods (``setup_method``,
``setup_module``, etc.) is now optional and may be omitted.
Thanks `@okken`_ for bringing this to attention and `@nicoddemus`_ for the PR.
* Improved automatic id generation selection in case of duplicate ids in
parametrize. parametrize.
Thanks `@palaviv`_ for the complete PR (`#1474`_). Thanks `@palaviv`_ for the complete PR (`#1474`_).
* Fix `#1426`_ Make ImportError during collection more explicit by reminding * Now pytest warnings summary is shown up by default. Added a new flag
the user to check the name of the test module/package(s). ``--disable-pytest-warnings`` to explicitly disable the warnings summary (`#1668`_).
* Make ImportError during collection more explicit by reminding
the user to check the name of the test module/package(s) (`#1426`_).
Thanks `@omarkohl`_ for the complete PR (`#1520`_). Thanks `@omarkohl`_ for the complete PR (`#1520`_).
* Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks * Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks
`@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_).
* pytest.raises in the context manager form accepts a custom * ``pytest.raises`` in the context manager form accepts a custom
message to raise when no exception occurred. ``message`` to raise when no exception occurred.
Thanks `@palaviv`_ for the complete PR (`#1616`_). Thanks `@palaviv`_ for the complete PR (`#1616`_).
* ``conftest.py`` files now benefit from assertion rewriting; previously it * ``conftest.py`` files now benefit from assertion rewriting; previously it
@ -173,26 +205,33 @@
is specified on the command line together with the ``--pyargs`` is specified on the command line together with the ``--pyargs``
option. Thanks to `@taschini`_ for the PR (`#1597`_). option. Thanks to `@taschini`_ for the PR (`#1597`_).
* Add proposal to docs for a new feature that enables users to combine multiple * Always include full assertion explanation during assertion rewriting. The previous behaviour was hiding
fixtures into one. Thanks to `@hpk42`_ and `@hackebrot`_. sub-expressions that happened to be ``False``, assuming this was redundant information.
* Rename ``getfuncargvalue`` to ``getfixturevalue``. ``getfuncargvalue`` is
deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_
for PR (`#1626`_).
* Always include full assertion explanation. The previous behaviour was hiding
sub-expressions that happened to be False, assuming this was redundant information.
Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and
`@tomviner`_ for PR. `@tomviner`_ for the PR.
* ``OptionGroup.addoption()`` now checks if option names were already * ``OptionGroup.addoption()`` now checks if option names were already
added before, to make it easier to track down issues like `#1618`_. added before, to make it easier to track down issues like `#1618`_.
Before, you only got exceptions later from ``argparse`` library, Before, you only got exceptions later from ``argparse`` library,
giving no clue about the actual reason for double-added options. giving no clue about the actual reason for double-added options.
* * ``yield``-based tests are considered deprecated and will be removed in pytest-4.0.
Thanks `@nicoddemus`_ for the PR.
* * Using ``pytest_funcarg__`` prefix to declare fixtures is considered deprecated and will be
removed in pytest-4.0 (`#1684`_).
Thanks `@nicoddemus`_ for the PR.
* Passing a command-line string to ``pytest.main()`` is considered deprecated and scheduled
for removal in pytest-4.0. It is recommended to pass a list of arguments instead (`#1723`_).
* Rename ``getfuncargvalue`` to ``getfixturevalue``. ``getfuncargvalue`` is
still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_
for the PR (`#1626`_).
* ``optparse`` type usage now triggers DeprecationWarnings (`#1740`_).
* ``optparse`` backward compatibility supports float/complex types (`#457`_).
* *
@ -202,22 +241,25 @@
**Bug Fixes** **Bug Fixes**
* When receiving identical test ids in parametrize we generate unique test ids. * Parametrize now correctly handles duplicated test ids.
* Fix internal error issue when ``method`` argument is missing for * Fix internal error issue when the ``method`` argument is missing for
``teardown_method()``. Fixes `#1605`_. ``teardown_method()`` (`#1605`_).
* Fix exception visualization in case the current working directory (CWD) gets * Fix exception visualization in case the current working directory (CWD) gets
deleted during testing. Fixes `#1235`_. Thanks `@bukzor`_ for reporting. PR by deleted during testing (`#1235`_). Thanks `@bukzor`_ for reporting. PR by
`@marscher`_. Thanks `@nicoddemus`_ for his help. `@marscher`_.
* Improve of the test output for logical expression with brackets. * Improve test output for logical expression with brackets (`#925`_).
Fixes `#925`_. Thanks `@DRMacIver`_ for reporting. Thanks to `@RedBeardCode`_ Thanks `@DRMacIver`_ for reporting and `@RedBeardCode`_ for the PR.
for PR.
* Create correct diff for strings ending with newlines. Fixes `#1553`_. * Create correct diff for strings ending with newlines (`#1553`_).
Thanks `@Vogtinator`_ for reporting. Thanks to `@RedBeardCode`_ and Thanks `@Vogtinator`_ for reporting and `@RedBeardCode`_ and
`@tomviner`_ for PR. `@tomviner`_ for the PR.
* ``ConftestImportFailure`` now shows the traceback making it easier to
identify bugs in ``conftest.py`` files (`#1516`_). Thanks `@txomon`_ for
the PR.
* *
@ -231,6 +273,8 @@
.. _#372: https://github.com/pytest-dev/pytest/issues/372 .. _#372: https://github.com/pytest-dev/pytest/issues/372
.. _#460: https://github.com/pytest-dev/pytest/pull/460 .. _#460: https://github.com/pytest-dev/pytest/pull/460
.. _#457: https://github.com/pytest-dev/pytest/issues/457
.. _#1740: https://github.com/pytest-dev/pytest/issues/1740
.. _#607: https://github.com/pytest-dev/pytest/issues/607 .. _#607: https://github.com/pytest-dev/pytest/issues/607
.. _#925: https://github.com/pytest-dev/pytest/issues/925 .. _#925: https://github.com/pytest-dev/pytest/issues/925
.. _#1235: https://github.com/pytest-dev/pytest/issues/1235 .. _#1235: https://github.com/pytest-dev/pytest/issues/1235
@ -247,6 +291,7 @@
.. _#1479: https://github.com/pytest-dev/pytest/issues/1479 .. _#1479: https://github.com/pytest-dev/pytest/issues/1479
.. _#1502: https://github.com/pytest-dev/pytest/pull/1502 .. _#1502: https://github.com/pytest-dev/pytest/pull/1502
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503 .. _#1503: https://github.com/pytest-dev/pytest/issues/1503
.. _#1516: https://github.com/pytest-dev/pytest/pull/1516
.. _#1519: https://github.com/pytest-dev/pytest/pull/1519 .. _#1519: https://github.com/pytest-dev/pytest/pull/1519
.. _#1520: https://github.com/pytest-dev/pytest/pull/1520 .. _#1520: https://github.com/pytest-dev/pytest/pull/1520
.. _#1526: https://github.com/pytest-dev/pytest/pull/1526 .. _#1526: https://github.com/pytest-dev/pytest/pull/1526
@ -260,16 +305,20 @@
.. _#1618: https://github.com/pytest-dev/pytest/issues/1618 .. _#1618: https://github.com/pytest-dev/pytest/issues/1618
.. _#1619: https://github.com/pytest-dev/pytest/issues/1619 .. _#1619: https://github.com/pytest-dev/pytest/issues/1619
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626 .. _#1626: https://github.com/pytest-dev/pytest/pull/1626
.. _#1668: https://github.com/pytest-dev/pytest/issues/1668
.. _#1627: https://github.com/pytest-dev/pytest/pull/1627 .. _#1627: https://github.com/pytest-dev/pytest/pull/1627
.. _#1628: https://github.com/pytest-dev/pytest/pull/1628 .. _#1628: https://github.com/pytest-dev/pytest/pull/1628
.. _#1629: https://github.com/pytest-dev/pytest/issues/1629 .. _#1629: https://github.com/pytest-dev/pytest/issues/1629
.. _#1632: https://github.com/pytest-dev/pytest/issues/1632 .. _#1632: https://github.com/pytest-dev/pytest/issues/1632
.. _#1633: https://github.com/pytest-dev/pytest/pull/1633 .. _#1633: https://github.com/pytest-dev/pytest/pull/1633
.. _#1664: https://github.com/pytest-dev/pytest/pull/1664 .. _#1664: https://github.com/pytest-dev/pytest/pull/1664
.. _#1684: https://github.com/pytest-dev/pytest/pull/1684
.. _#1723: https://github.com/pytest-dev/pytest/pull/1723
.. _@DRMacIver: https://github.com/DRMacIver .. _@DRMacIver: https://github.com/DRMacIver
.. _@RedBeardCode: https://github.com/RedBeardCode .. _@RedBeardCode: https://github.com/RedBeardCode
.. _@Vogtinator: https://github.com/Vogtinator .. _@Vogtinator: https://github.com/Vogtinator
.. _@anntzer: https://github.com/anntzer
.. _@bagerard: https://github.com/bagerard .. _@bagerard: https://github.com/bagerard
.. _@blueyed: https://github.com/blueyed .. _@blueyed: https://github.com/blueyed
.. _@ceridwen: https://github.com/ceridwen .. _@ceridwen: https://github.com/ceridwen
@ -287,6 +336,7 @@
.. _@nikratio: https://github.com/nikratio .. _@nikratio: https://github.com/nikratio
.. _@novas0x2a: https://github.com/novas0x2a .. _@novas0x2a: https://github.com/novas0x2a
.. _@obestwalter: https://github.com/obestwalter .. _@obestwalter: https://github.com/obestwalter
.. _@okken: https://github.com/okken
.. _@olegpidsadnyi: https://github.com/olegpidsadnyi .. _@olegpidsadnyi: https://github.com/olegpidsadnyi
.. _@omarkohl: https://github.com/omarkohl .. _@omarkohl: https://github.com/omarkohl
.. _@palaviv: https://github.com/palaviv .. _@palaviv: https://github.com/palaviv
@ -294,6 +344,7 @@
.. _@sober7: https://github.com/sober7 .. _@sober7: https://github.com/sober7
.. _@tareqalayan: https://github.com/tareqalayan .. _@tareqalayan: https://github.com/tareqalayan
.. _@taschini: https://github.com/taschini .. _@taschini: https://github.com/taschini
.. _@txomon: https://github.com/txomon
2.9.2 2.9.2
===== =====

View File

@ -4,9 +4,6 @@ from .code import ExceptionInfo # noqa
from .code import Frame # noqa from .code import Frame # noqa
from .code import Traceback # noqa from .code import Traceback # noqa
from .code import getrawcode # noqa from .code import getrawcode # noqa
from .code import patch_builtins # noqa
from .code import unpatch_builtins # noqa
from .source import Source # noqa from .source import Source # noqa
from .source import compile_ as compile # noqa from .source import compile_ as compile # noqa
from .source import getfslineno # noqa from .source import getfslineno # noqa

View File

@ -179,18 +179,6 @@ class TracebackEntry(object):
return self.frame.f_locals return self.frame.f_locals
locals = property(getlocals, None, None, "locals of underlaying frame") locals = property(getlocals, None, None, "locals of underlaying frame")
def reinterpret(self):
"""Reinterpret the failing statement and returns a detailed information
about what operations are performed."""
from _pytest.assertion.reinterpret import reinterpret
if self.exprinfo is None:
source = py.builtin._totext(self.statement).strip()
x = reinterpret(source, self.frame, should_fail=True)
if not py.builtin._istext(x):
raise TypeError("interpret returned non-string %r" % (x,))
self.exprinfo = x
return self.exprinfo
def getfirstlinesource(self): def getfirstlinesource(self):
# on Jython this firstlineno can be -1 apparently # on Jython this firstlineno can be -1 apparently
return max(self.frame.code.firstlineno, 0) return max(self.frame.code.firstlineno, 0)
@ -830,29 +818,6 @@ class ReprFuncArgs(TerminalRepr):
tw.line("") tw.line("")
oldbuiltins = {}
def patch_builtins(assertion=True, compile=True):
""" put compile and AssertionError builtins to Python's builtins. """
if assertion:
from _pytest.assertion import reinterpret
l = oldbuiltins.setdefault('AssertionError', [])
l.append(py.builtin.builtins.AssertionError)
py.builtin.builtins.AssertionError = reinterpret.AssertionError
if compile:
import _pytest._code
l = oldbuiltins.setdefault('compile', [])
l.append(py.builtin.builtins.compile)
py.builtin.builtins.compile = _pytest._code.compile
def unpatch_builtins(assertion=True, compile=True):
""" remove compile and AssertionError builtins from Python builtins. """
if assertion:
py.builtin.builtins.AssertionError = oldbuiltins['AssertionError'].pop()
if compile:
py.builtin.builtins.compile = oldbuiltins['compile'].pop()
def getrawcode(obj, trycall=True): def getrawcode(obj, trycall=True):
""" return code object for given function. """ """ return code object for given function. """
try: try:

View File

@ -5,9 +5,8 @@ import py
import os import os
import sys import sys
from _pytest.config import hookimpl
from _pytest.monkeypatch import monkeypatch
from _pytest.assertion import util from _pytest.assertion import util
from _pytest.assertion import rewrite
def pytest_addoption(parser): def pytest_addoption(parser):
@ -15,16 +14,42 @@ def pytest_addoption(parser):
group.addoption('--assert', group.addoption('--assert',
action="store", action="store",
dest="assertmode", dest="assertmode",
choices=("rewrite", "reinterp", "plain",), choices=("rewrite", "plain",),
default="rewrite", default="rewrite",
metavar="MODE", metavar="MODE",
help="""control assertion debugging tools. 'plain' help="""Control assertion debugging tools. 'plain'
performs no assertion debugging. 'reinterp' performs no assertion debugging. 'rewrite'
reinterprets assert statements after they failed (the default) rewrites assert statements in
to provide assertion expression information. test modules on import to provide assert
'rewrite' (the default) rewrites assert expression information.""")
statements in test modules on import to
provide assert expression information. """)
def pytest_namespace():
return {'register_assert_rewrite': register_assert_rewrite}
def register_assert_rewrite(*names):
"""Register a module name to be rewritten on import.
This function will make sure that the module will get it's assert
statements rewritten when it is imported. Thus you should make
sure to call this before the module is actually imported, usually
in your __init__.py if you are a plugin using a package.
"""
for hook in sys.meta_path:
if isinstance(hook, rewrite.AssertionRewritingHook):
importhook = hook
break
else:
importhook = DummyRewriteHook()
importhook.mark_rewrite(*names)
class DummyRewriteHook(object):
"""A no-op import hook for when rewriting is disabled."""
def mark_rewrite(self, *names):
pass
class AssertionState: class AssertionState:
@ -33,55 +58,37 @@ class AssertionState:
def __init__(self, config, mode): def __init__(self, config, mode):
self.mode = mode self.mode = mode
self.trace = config.trace.root.get("assertion") self.trace = config.trace.root.get("assertion")
self.hook = None
@hookimpl(tryfirst=True) def install_importhook(config):
def pytest_load_initial_conftests(early_config, parser, args): """Try to install the rewrite hook, raise SystemError if it fails."""
ns, ns_unknown_args = parser.parse_known_and_unknown_args(args)
mode = ns.assertmode
if mode == "rewrite":
try:
import ast # noqa
except ImportError:
mode = "reinterp"
else:
# Both Jython and CPython 2.6.0 have AST bugs that make the # Both Jython and CPython 2.6.0 have AST bugs that make the
# assertion rewriting hook malfunction. # assertion rewriting hook malfunction.
if (sys.platform.startswith('java') or if (sys.platform.startswith('java') or
sys.version_info[:3] == (2, 6, 0)): sys.version_info[:3] == (2, 6, 0)):
mode = "reinterp" raise SystemError('rewrite not supported')
early_config._assertstate = AssertionState(early_config, mode) config._assertstate = AssertionState(config, 'rewrite')
warn_about_missing_assertion(mode, early_config.pluginmanager) config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
if mode != "plain":
_load_modules(mode)
m = monkeypatch()
early_config._cleanup.append(m.undo)
m.setattr(py.builtin.builtins, 'AssertionError',
reinterpret.AssertionError) # noqa
hook = None
if mode == "rewrite":
hook = rewrite.AssertionRewritingHook(early_config) # noqa
sys.meta_path.insert(0, hook) sys.meta_path.insert(0, hook)
config._assertstate.trace('installed rewrite import hook')
early_config._assertstate.hook = hook
early_config._assertstate.trace("configured with mode set to %r" % (mode,))
def undo(): def undo():
hook = early_config._assertstate.hook hook = config._assertstate.hook
if hook is not None and hook in sys.meta_path: if hook is not None and hook in sys.meta_path:
sys.meta_path.remove(hook) sys.meta_path.remove(hook)
early_config.add_cleanup(undo) config.add_cleanup(undo)
return hook
def pytest_collection(session): def pytest_collection(session):
# this hook is only called when test modules are collected # this hook is only called when test modules are collected
# so for example not in the master process of pytest-xdist # so for example not in the master process of pytest-xdist
# (which does not collect test modules) # (which does not collect test modules)
hook = session.config._assertstate.hook assertstate = getattr(session.config, '_assertstate', None)
if hook is not None: if assertstate:
hook.set_session(session) if assertstate.hook is not None:
assertstate.hook.set_session(session)
def _running_on_ci(): def _running_on_ci():
@ -138,43 +145,10 @@ def pytest_runtest_teardown(item):
def pytest_sessionfinish(session): def pytest_sessionfinish(session):
hook = session.config._assertstate.hook assertstate = getattr(session.config, '_assertstate', None)
if hook is not None: if assertstate:
hook.session = None if assertstate.hook is not None:
assertstate.hook.set_session(None)
def _load_modules(mode):
"""Lazily import assertion related code."""
global rewrite, reinterpret
from _pytest.assertion import reinterpret # noqa
if mode == "rewrite":
from _pytest.assertion import rewrite # noqa
def warn_about_missing_assertion(mode, pluginmanager):
try:
assert False
except AssertionError:
pass
else:
if mode == "rewrite":
specifically = ("assertions which are not in test modules "
"will be ignored")
else:
specifically = "failing tests may report as passing"
# temporarily disable capture so we can print our warning
capman = pluginmanager.getplugin('capturemanager')
try:
out, err = capman.suspendcapture()
sys.stderr.write("WARNING: " + specifically +
" because assert statements are not executed "
"by the underlying Python interpreter "
"(are you using python -O?)\n")
finally:
capman.resumecapture()
sys.stdout.write(out)
sys.stderr.write(err)
# Expose this plugin's implementation for the pytest_assertrepr_compare hook # Expose this plugin's implementation for the pytest_assertrepr_compare hook

View File

@ -1,407 +0,0 @@
"""
Find intermediate evalutation results in assert statements through builtin AST.
"""
import ast
import sys
import _pytest._code
import py
from _pytest.assertion import util
u = py.builtin._totext
class AssertionError(util.BuiltinAssertionError):
def __init__(self, *args):
util.BuiltinAssertionError.__init__(self, *args)
if args:
# on Python2.6 we get len(args)==2 for: assert 0, (x,y)
# on Python2.7 and above we always get len(args) == 1
# with args[0] being the (x,y) tuple.
if len(args) > 1:
toprint = args
else:
toprint = args[0]
try:
self.msg = u(toprint)
except Exception:
self.msg = u(
"<[broken __repr__] %s at %0xd>"
% (toprint.__class__, id(toprint)))
else:
f = _pytest._code.Frame(sys._getframe(1))
try:
source = f.code.fullsource
if source is not None:
try:
source = source.getstatement(f.lineno, assertion=True)
except IndexError:
source = None
else:
source = str(source.deindent()).strip()
except py.error.ENOENT:
source = None
# this can also occur during reinterpretation, when the
# co_filename is set to "<run>".
if source:
self.msg = reinterpret(source, f, should_fail=True)
else:
self.msg = "<could not determine information>"
if not self.args:
self.args = (self.msg,)
if sys.version_info > (3, 0):
AssertionError.__module__ = "builtins"
if sys.platform.startswith("java"):
# See http://bugs.jython.org/issue1497
_exprs = ("BoolOp", "BinOp", "UnaryOp", "Lambda", "IfExp", "Dict",
"ListComp", "GeneratorExp", "Yield", "Compare", "Call",
"Repr", "Num", "Str", "Attribute", "Subscript", "Name",
"List", "Tuple")
_stmts = ("FunctionDef", "ClassDef", "Return", "Delete", "Assign",
"AugAssign", "Print", "For", "While", "If", "With", "Raise",
"TryExcept", "TryFinally", "Assert", "Import", "ImportFrom",
"Exec", "Global", "Expr", "Pass", "Break", "Continue")
_expr_nodes = set(getattr(ast, name) for name in _exprs)
_stmt_nodes = set(getattr(ast, name) for name in _stmts)
def _is_ast_expr(node):
return node.__class__ in _expr_nodes
def _is_ast_stmt(node):
return node.__class__ in _stmt_nodes
else:
def _is_ast_expr(node):
return isinstance(node, ast.expr)
def _is_ast_stmt(node):
return isinstance(node, ast.stmt)
try:
_Starred = ast.Starred
except AttributeError:
# Python 2. Define a dummy class so isinstance() will always be False.
class _Starred(object): pass
class Failure(Exception):
"""Error found while interpreting AST."""
def __init__(self, explanation=""):
self.cause = sys.exc_info()
self.explanation = explanation
def reinterpret(source, frame, should_fail=False):
mod = ast.parse(source)
visitor = DebugInterpreter(frame)
try:
visitor.visit(mod)
except Failure:
failure = sys.exc_info()[1]
return getfailure(failure)
if should_fail:
return ("(assertion failed, but when it was re-run for "
"printing intermediate values, it did not fail. Suggestions: "
"compute assert expression before the assert or use --assert=plain)")
def run(offending_line, frame=None):
if frame is None:
frame = _pytest._code.Frame(sys._getframe(1))
return reinterpret(offending_line, frame)
def getfailure(e):
explanation = util.format_explanation(e.explanation)
value = e.cause[1]
if str(value):
lines = explanation.split('\n')
lines[0] += " << %s" % (value,)
explanation = '\n'.join(lines)
text = "%s: %s" % (e.cause[0].__name__, explanation)
if text.startswith('AssertionError: assert '):
text = text[16:]
return text
operator_map = {
ast.BitOr : "|",
ast.BitXor : "^",
ast.BitAnd : "&",
ast.LShift : "<<",
ast.RShift : ">>",
ast.Add : "+",
ast.Sub : "-",
ast.Mult : "*",
ast.Div : "/",
ast.FloorDiv : "//",
ast.Mod : "%",
ast.Eq : "==",
ast.NotEq : "!=",
ast.Lt : "<",
ast.LtE : "<=",
ast.Gt : ">",
ast.GtE : ">=",
ast.Pow : "**",
ast.Is : "is",
ast.IsNot : "is not",
ast.In : "in",
ast.NotIn : "not in"
}
unary_map = {
ast.Not : "not %s",
ast.Invert : "~%s",
ast.USub : "-%s",
ast.UAdd : "+%s"
}
class DebugInterpreter(ast.NodeVisitor):
"""Interpret AST nodes to gleam useful debugging information. """
def __init__(self, frame):
self.frame = frame
def generic_visit(self, node):
# Fallback when we don't have a special implementation.
if _is_ast_expr(node):
mod = ast.Expression(node)
co = self._compile(mod)
try:
result = self.frame.eval(co)
except Exception:
raise Failure()
explanation = self.frame.repr(result)
return explanation, result
elif _is_ast_stmt(node):
mod = ast.Module([node])
co = self._compile(mod, "exec")
try:
self.frame.exec_(co)
except Exception:
raise Failure()
return None, None
else:
raise AssertionError("can't handle %s" %(node,))
def _compile(self, source, mode="eval"):
return compile(source, "<assertion interpretation>", mode)
def visit_Expr(self, expr):
return self.visit(expr.value)
def visit_Module(self, mod):
for stmt in mod.body:
self.visit(stmt)
def visit_Name(self, name):
explanation, result = self.generic_visit(name)
# See if the name is local.
source = "%r in locals() is not globals()" % (name.id,)
co = self._compile(source)
try:
local = self.frame.eval(co)
except Exception:
# have to assume it isn't
local = None
if local is None or not self.frame.is_true(local):
return name.id, result
return explanation, result
def visit_Compare(self, comp):
left = comp.left
left_explanation, left_result = self.visit(left)
for op, next_op in zip(comp.ops, comp.comparators):
next_explanation, next_result = self.visit(next_op)
op_symbol = operator_map[op.__class__]
explanation = "%s %s %s" % (left_explanation, op_symbol,
next_explanation)
source = "__exprinfo_left %s __exprinfo_right" % (op_symbol,)
co = self._compile(source)
try:
result = self.frame.eval(co, __exprinfo_left=left_result,
__exprinfo_right=next_result)
except Exception:
raise Failure(explanation)
try:
if not self.frame.is_true(result):
break
except KeyboardInterrupt:
raise
except:
break
left_explanation, left_result = next_explanation, next_result
if util._reprcompare is not None:
res = util._reprcompare(op_symbol, left_result, next_result)
if res:
explanation = res
return explanation, result
def visit_BoolOp(self, boolop):
is_or = isinstance(boolop.op, ast.Or)
explanations = []
for operand in boolop.values:
explanation, result = self.visit(operand)
explanations.append(explanation)
if result == is_or:
break
name = is_or and " or " or " and "
explanation = "(" + name.join(explanations) + ")"
return explanation, result
def visit_UnaryOp(self, unary):
pattern = unary_map[unary.op.__class__]
operand_explanation, operand_result = self.visit(unary.operand)
explanation = pattern % (operand_explanation,)
co = self._compile(pattern % ("__exprinfo_expr",))
try:
result = self.frame.eval(co, __exprinfo_expr=operand_result)
except Exception:
raise Failure(explanation)
return explanation, result
def visit_BinOp(self, binop):
left_explanation, left_result = self.visit(binop.left)
right_explanation, right_result = self.visit(binop.right)
symbol = operator_map[binop.op.__class__]
explanation = "(%s %s %s)" % (left_explanation, symbol,
right_explanation)
source = "__exprinfo_left %s __exprinfo_right" % (symbol,)
co = self._compile(source)
try:
result = self.frame.eval(co, __exprinfo_left=left_result,
__exprinfo_right=right_result)
except Exception:
raise Failure(explanation)
return explanation, result
def visit_Call(self, call):
func_explanation, func = self.visit(call.func)
arg_explanations = []
ns = {"__exprinfo_func" : func}
arguments = []
for arg in call.args:
arg_explanation, arg_result = self.visit(arg)
if isinstance(arg, _Starred):
arg_name = "__exprinfo_star"
ns[arg_name] = arg_result
arguments.append("*%s" % (arg_name,))
arg_explanations.append("*%s" % (arg_explanation,))
else:
arg_name = "__exprinfo_%s" % (len(ns),)
ns[arg_name] = arg_result
arguments.append(arg_name)
arg_explanations.append(arg_explanation)
for keyword in call.keywords:
arg_explanation, arg_result = self.visit(keyword.value)
if keyword.arg:
arg_name = "__exprinfo_%s" % (len(ns),)
keyword_source = "%s=%%s" % (keyword.arg)
arguments.append(keyword_source % (arg_name,))
arg_explanations.append(keyword_source % (arg_explanation,))
else:
arg_name = "__exprinfo_kwds"
arguments.append("**%s" % (arg_name,))
arg_explanations.append("**%s" % (arg_explanation,))
ns[arg_name] = arg_result
if getattr(call, 'starargs', None):
arg_explanation, arg_result = self.visit(call.starargs)
arg_name = "__exprinfo_star"
ns[arg_name] = arg_result
arguments.append("*%s" % (arg_name,))
arg_explanations.append("*%s" % (arg_explanation,))
if getattr(call, 'kwargs', None):
arg_explanation, arg_result = self.visit(call.kwargs)
arg_name = "__exprinfo_kwds"
ns[arg_name] = arg_result
arguments.append("**%s" % (arg_name,))
arg_explanations.append("**%s" % (arg_explanation,))
args_explained = ", ".join(arg_explanations)
explanation = "%s(%s)" % (func_explanation, args_explained)
args = ", ".join(arguments)
source = "__exprinfo_func(%s)" % (args,)
co = self._compile(source)
try:
result = self.frame.eval(co, **ns)
except Exception:
raise Failure(explanation)
pattern = "%s\n{%s = %s\n}"
rep = self.frame.repr(result)
explanation = pattern % (rep, rep, explanation)
return explanation, result
def _is_builtin_name(self, name):
pattern = "%r not in globals() and %r not in locals()"
source = pattern % (name.id, name.id)
co = self._compile(source)
try:
return self.frame.eval(co)
except Exception:
return False
def visit_Attribute(self, attr):
if not isinstance(attr.ctx, ast.Load):
return self.generic_visit(attr)
source_explanation, source_result = self.visit(attr.value)
explanation = "%s.%s" % (source_explanation, attr.attr)
source = "__exprinfo_expr.%s" % (attr.attr,)
co = self._compile(source)
try:
try:
result = self.frame.eval(co, __exprinfo_expr=source_result)
except AttributeError:
# Maybe the attribute name needs to be mangled?
if not attr.attr.startswith("__") or attr.attr.endswith("__"):
raise
source = "getattr(__exprinfo_expr.__class__, '__name__', '')"
co = self._compile(source)
class_name = self.frame.eval(co, __exprinfo_expr=source_result)
mangled_attr = "_" + class_name + attr.attr
source = "__exprinfo_expr.%s" % (mangled_attr,)
co = self._compile(source)
result = self.frame.eval(co, __exprinfo_expr=source_result)
except Exception:
raise Failure(explanation)
explanation = "%s\n{%s = %s.%s\n}" % (self.frame.repr(result),
self.frame.repr(result),
source_explanation, attr.attr)
# Check if the attr is from an instance.
source = "%r in getattr(__exprinfo_expr, '__dict__', {})"
source = source % (attr.attr,)
co = self._compile(source)
try:
from_instance = self.frame.eval(co, __exprinfo_expr=source_result)
except Exception:
from_instance = None
if from_instance is None or self.frame.is_true(from_instance):
rep = self.frame.repr(result)
pattern = "%s\n{%s = %s\n}"
explanation = pattern % (rep, rep, explanation)
return explanation, result
def visit_Assert(self, assrt):
test_explanation, test_result = self.visit(assrt.test)
explanation = "assert %s" % (test_explanation,)
if not self.frame.is_true(test_result):
try:
raise util.BuiltinAssertionError
except Exception:
raise Failure(explanation)
return explanation, test_result
def visit_Assign(self, assign):
value_explanation, value_result = self.visit(assign.value)
explanation = "... = %s" % (value_explanation,)
name = ast.Name("__exprinfo_expr", ast.Load(),
lineno=assign.value.lineno,
col_offset=assign.value.col_offset)
new_assign = ast.Assign(assign.targets, name, lineno=assign.lineno,
col_offset=assign.col_offset)
mod = ast.Module([new_assign])
co = self._compile(mod, "exec")
try:
self.frame.exec_(co, __exprinfo_expr=value_result)
except Exception:
raise Failure(explanation)
return explanation, value_result

View File

@ -51,6 +51,7 @@ class AssertionRewritingHook(object):
self.session = None self.session = None
self.modules = {} self.modules = {}
self._register_with_pkg_resources() self._register_with_pkg_resources()
self._must_rewrite = set()
def set_session(self, session): def set_session(self, session):
self.session = session self.session = session
@ -87,7 +88,7 @@ class AssertionRewritingHook(object):
fn = os.path.join(pth, name.rpartition(".")[2] + ".py") fn = os.path.join(pth, name.rpartition(".")[2] + ".py")
fn_pypath = py.path.local(fn) fn_pypath = py.path.local(fn)
if not self._should_rewrite(fn_pypath, state): if not self._should_rewrite(name, fn_pypath, state):
return None return None
# The requested module looks like a test file, so rewrite it. This is # The requested module looks like a test file, so rewrite it. This is
@ -137,7 +138,7 @@ class AssertionRewritingHook(object):
self.modules[name] = co, pyc self.modules[name] = co, pyc
return self return self
def _should_rewrite(self, fn_pypath, state): def _should_rewrite(self, name, fn_pypath, state):
# always rewrite conftest files # always rewrite conftest files
fn = str(fn_pypath) fn = str(fn_pypath)
if fn_pypath.basename == 'conftest.py': if fn_pypath.basename == 'conftest.py':
@ -161,8 +162,29 @@ class AssertionRewritingHook(object):
finally: finally:
self.session = session self.session = session
del session del session
else:
for marked in self._must_rewrite:
if marked.startswith(name):
return True
return False return False
def mark_rewrite(self, *names):
"""Mark import names as needing to be re-written.
The named module or package as well as any nested modules will
be re-written on import.
"""
already_imported = set(names).intersection(set(sys.modules))
if already_imported:
self._warn_already_imported(already_imported)
self._must_rewrite.update(names)
def _warn_already_imported(self, names):
self.config.warn(
'P1',
'Modules are already imported so can not be re-written: %s' %
','.join(names))
def load_module(self, name): def load_module(self, name):
# If there is an existing module object named 'fullname' in # If there is an existing module object named 'fullname' in
# sys.modules, the loader must use that existing module. (Otherwise, # sys.modules, the loader must use that existing module. (Otherwise,

View File

@ -5,11 +5,13 @@ import traceback
import types import types
import warnings import warnings
import pkg_resources
import py import py
# DON't import pytest here because it causes import cycle troubles # DON't import pytest here because it causes import cycle troubles
import sys, os import sys, os
import _pytest._code import _pytest._code
import _pytest.hookspec # the extension point definitions import _pytest.hookspec # the extension point definitions
import _pytest.assertion
from _pytest._pluggy import PluginManager, HookimplMarker, HookspecMarker from _pytest._pluggy import PluginManager, HookimplMarker, HookspecMarker
hookimpl = HookimplMarker("pytest") hookimpl = HookimplMarker("pytest")
@ -25,6 +27,12 @@ class ConftestImportFailure(Exception):
self.path = path self.path = path
self.excinfo = excinfo self.excinfo = excinfo
def __str__(self):
etype, evalue, etb = self.excinfo
formatted = traceback.format_tb(etb)
# The level of the tracebacks we want to print is hand crafted :(
return repr(evalue) + '\n' + ''.join(formatted[2:])
def main(args=None, plugins=None): def main(args=None, plugins=None):
""" return exit code, after performing an in-process test run. """ return exit code, after performing an in-process test run.
@ -98,6 +106,7 @@ def get_plugin_manager():
return get_config().pluginmanager return get_config().pluginmanager
def _prepareconfig(args=None, plugins=None): def _prepareconfig(args=None, plugins=None):
warning = None
if args is None: if args is None:
args = sys.argv[1:] args = sys.argv[1:]
elif isinstance(args, py.path.local): elif isinstance(args, py.path.local):
@ -106,6 +115,10 @@ def _prepareconfig(args=None, plugins=None):
if not isinstance(args, str): if not isinstance(args, str):
raise ValueError("not a string or argument list: %r" % (args,)) raise ValueError("not a string or argument list: %r" % (args,))
args = shlex.split(args, posix=sys.platform != "win32") args = shlex.split(args, posix=sys.platform != "win32")
# we want to remove this way of passing arguments to pytest.main()
# in pytest-4.0
warning = ('passing a string to pytest.main() is deprecated, '
'pass a list of arguments instead.')
config = get_config() config = get_config()
pluginmanager = config.pluginmanager pluginmanager = config.pluginmanager
try: try:
@ -115,6 +128,8 @@ def _prepareconfig(args=None, plugins=None):
pluginmanager.consider_pluginarg(plugin) pluginmanager.consider_pluginarg(plugin)
else: else:
pluginmanager.register(plugin) pluginmanager.register(plugin)
if warning:
config.warn('C1', warning)
return pluginmanager.hook.pytest_cmdline_parse( return pluginmanager.hook.pytest_cmdline_parse(
pluginmanager=pluginmanager, args=args) pluginmanager=pluginmanager, args=args)
except BaseException: except BaseException:
@ -153,6 +168,9 @@ class PytestPluginManager(PluginManager):
self.trace.root.setwriter(err.write) self.trace.root.setwriter(err.write)
self.enable_tracing() self.enable_tracing()
# Config._consider_importhook will set a real object if required.
self.rewrite_hook = _pytest.assertion.DummyRewriteHook()
def addhooks(self, module_or_class): def addhooks(self, module_or_class):
""" """
.. deprecated:: 2.8 .. deprecated:: 2.8
@ -361,7 +379,9 @@ class PytestPluginManager(PluginManager):
self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS")) self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS"))
def consider_module(self, mod): def consider_module(self, mod):
self._import_plugin_specs(getattr(mod, "pytest_plugins", None)) plugins = getattr(mod, 'pytest_plugins', [])
self.rewrite_hook.mark_rewrite(*plugins)
self._import_plugin_specs(plugins)
def _import_plugin_specs(self, spec): def _import_plugin_specs(self, spec):
if spec: if spec:
@ -538,13 +558,18 @@ class ArgumentError(Exception):
class Argument: class Argument:
"""class that mimics the necessary behaviour of optparse.Option """ """class that mimics the necessary behaviour of optparse.Option
its currently a least effort implementation
and ignoring choices and integer prefixes
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
"""
_typ_map = { _typ_map = {
'int': int, 'int': int,
'string': str, 'string': str,
'float': float,
'complex': complex,
} }
# enable after some grace period for plugin writers
TYPE_WARN = False
def __init__(self, *names, **attrs): def __init__(self, *names, **attrs):
"""store parms in private vars for use in add_argument""" """store parms in private vars for use in add_argument"""
@ -552,17 +577,12 @@ class Argument:
self._short_opts = [] self._short_opts = []
self._long_opts = [] self._long_opts = []
self.dest = attrs.get('dest') self.dest = attrs.get('dest')
if self.TYPE_WARN: if '%default' in (attrs.get('help') or ''):
try:
help = attrs['help']
if '%default' in help:
warnings.warn( warnings.warn(
'pytest now uses argparse. "%default" should be' 'pytest now uses argparse. "%default" should be'
' changed to "%(default)s" ', ' changed to "%(default)s" ',
FutureWarning, DeprecationWarning,
stacklevel=3) stacklevel=3)
except KeyError:
pass
try: try:
typ = attrs['type'] typ = attrs['type']
except KeyError: except KeyError:
@ -571,24 +591,22 @@ class Argument:
# this might raise a keyerror as well, don't want to catch that # this might raise a keyerror as well, don't want to catch that
if isinstance(typ, py.builtin._basestring): if isinstance(typ, py.builtin._basestring):
if typ == 'choice': if typ == 'choice':
if self.TYPE_WARN:
warnings.warn( warnings.warn(
'type argument to addoption() is a string %r.' 'type argument to addoption() is a string %r.'
' For parsearg this is optional and when supplied ' ' For parsearg this is optional and when supplied '
' should be a type.' ' should be a type.'
' (options: %s)' % (typ, names), ' (options: %s)' % (typ, names),
FutureWarning, DeprecationWarning,
stacklevel=3) stacklevel=3)
# argparse expects a type here take it from # argparse expects a type here take it from
# the type of the first element # the type of the first element
attrs['type'] = type(attrs['choices'][0]) attrs['type'] = type(attrs['choices'][0])
else: else:
if self.TYPE_WARN:
warnings.warn( warnings.warn(
'type argument to addoption() is a string %r.' 'type argument to addoption() is a string %r.'
' For parsearg this should be a type.' ' For parsearg this should be a type.'
' (options: %s)' % (typ, names), ' (options: %s)' % (typ, names),
FutureWarning, DeprecationWarning,
stacklevel=3) stacklevel=3)
attrs['type'] = Argument._typ_map[typ] attrs['type'] = Argument._typ_map[typ]
# used in test_parseopt -> test_parse_defaultgetter # used in test_parseopt -> test_parse_defaultgetter
@ -918,14 +936,62 @@ class Config(object):
self._parser.addini('addopts', 'extra command line options', 'args') self._parser.addini('addopts', 'extra command line options', 'args')
self._parser.addini('minversion', 'minimally required pytest version') self._parser.addini('minversion', 'minimally required pytest version')
def _consider_importhook(self, args, entrypoint_name):
"""Install the PEP 302 import hook if using assertion re-writing.
Needs to parse the --assert=<mode> option from the commandline
and find all the installed plugins to mark them for re-writing
by the importhook.
"""
ns, unknown_args = self._parser.parse_known_and_unknown_args(args)
mode = ns.assertmode
if mode == 'rewrite':
try:
hook = _pytest.assertion.install_importhook(self)
except SystemError:
mode = 'plain'
else:
self.pluginmanager.rewrite_hook = hook
for entrypoint in pkg_resources.iter_entry_points('pytest11'):
for entry in entrypoint.dist._get_metadata('RECORD'):
fn = entry.split(',')[0]
is_simple_module = os.sep not in fn and fn.endswith('.py')
is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py')
if is_simple_module:
module_name, ext = os.path.splitext(fn)
hook.mark_rewrite(module_name)
elif is_package:
package_name = os.path.dirname(fn)
hook.mark_rewrite(package_name)
self._warn_about_missing_assertion(mode)
def _warn_about_missing_assertion(self, mode):
try:
assert False
except AssertionError:
pass
else:
if mode == 'plain':
sys.stderr.write("WARNING: ASSERTIONS ARE NOT EXECUTED"
" and FAILING TESTS WILL PASS. Are you"
" using python -O?")
else:
sys.stderr.write("WARNING: assertions not in test modules or"
" plugins will be ignored"
" because assert statements are not executed "
"by the underlying Python interpreter "
"(are you using python -O?)\n")
def _preparse(self, args, addopts=True): def _preparse(self, args, addopts=True):
self._initini(args) self._initini(args)
if addopts: if addopts:
args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args
args[:] = self.getini("addopts") + args args[:] = self.getini("addopts") + args
self._checkversion() self._checkversion()
entrypoint_name = 'pytest11'
self._consider_importhook(args, entrypoint_name)
self.pluginmanager.consider_preparse(args) self.pluginmanager.consider_preparse(args)
self.pluginmanager.load_setuptools_entrypoints("pytest11") self.pluginmanager.load_setuptools_entrypoints(entrypoint_name)
self.pluginmanager.consider_env() self.pluginmanager.consider_env()
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy()) self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
if self.known_args_namespace.confcutdir is None and self.inifile: if self.known_args_namespace.confcutdir is None and self.inifile:

View File

@ -8,21 +8,33 @@ import pytest
def pytest_addoption(parser): def pytest_addoption(parser):
group = parser.getgroup("general") group = parser.getgroup("general")
group._addoption('--pdb', group._addoption(
action="store_true", dest="usepdb", default=False, '--pdb', dest="usepdb", action="store_true",
help="start the interactive Python debugger on errors.") help="start the interactive Python debugger on errors.")
group._addoption(
'--pdbcls', dest="usepdb_cls", metavar="modulename:classname",
help="start a custom interactive Python debugger on errors. "
"For example: --pdbcls=IPython.core.debugger:Pdb")
def pytest_namespace(): def pytest_namespace():
return {'set_trace': pytestPDB().set_trace} return {'set_trace': pytestPDB().set_trace}
def pytest_configure(config): def pytest_configure(config):
if config.getvalue("usepdb"): if config.getvalue("usepdb") or config.getvalue("usepdb_cls"):
config.pluginmanager.register(PdbInvoke(), 'pdbinvoke') config.pluginmanager.register(PdbInvoke(), 'pdbinvoke')
if config.getvalue("usepdb_cls"):
modname, classname = config.getvalue("usepdb_cls").split(":")
__import__(modname)
pdb_cls = getattr(sys.modules[modname], classname)
else:
pdb_cls = pdb.Pdb
pytestPDB._pdb_cls = pdb_cls
old = (pdb.set_trace, pytestPDB._pluginmanager) old = (pdb.set_trace, pytestPDB._pluginmanager)
def fin(): def fin():
pdb.set_trace, pytestPDB._pluginmanager = old pdb.set_trace, pytestPDB._pluginmanager = old
pytestPDB._config = None pytestPDB._config = None
pytestPDB._pdb_cls = pdb.Pdb
pdb.set_trace = pytest.set_trace pdb.set_trace = pytest.set_trace
pytestPDB._pluginmanager = config.pluginmanager pytestPDB._pluginmanager = config.pluginmanager
pytestPDB._config = config pytestPDB._config = config
@ -32,6 +44,7 @@ class pytestPDB:
""" Pseudo PDB that defers to the real pdb. """ """ Pseudo PDB that defers to the real pdb. """
_pluginmanager = None _pluginmanager = None
_config = None _config = None
_pdb_cls = pdb.Pdb
def set_trace(self): def set_trace(self):
""" invoke PDB set_trace debugging, dropping any IO capturing. """ """ invoke PDB set_trace debugging, dropping any IO capturing. """
@ -45,7 +58,7 @@ class pytestPDB:
tw.line() tw.line()
tw.sep(">", "PDB set_trace (IO-capturing turned off)") tw.sep(">", "PDB set_trace (IO-capturing turned off)")
self._pluginmanager.hook.pytest_enter_pdb(config=self._config) self._pluginmanager.hook.pytest_enter_pdb(config=self._config)
pdb.Pdb().set_trace(frame) self._pdb_cls().set_trace(frame)
class PdbInvoke: class PdbInvoke:
@ -98,7 +111,7 @@ def _find_last_non_hidden_frame(stack):
def post_mortem(t): def post_mortem(t):
class Pdb(pdb.Pdb): class Pdb(pytestPDB._pdb_cls):
def get_stack(self, f, t): def get_stack(self, f, t):
stack, i = pdb.Pdb.get_stack(self, f, t) stack, i = pdb.Pdb.get_stack(self, f, t)
if f is None: if f is None:

View File

@ -878,7 +878,12 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
else: else:
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
defaultfuncargprefixmarker = fixture() defaultfuncargprefixmarker = fixture()
funcarg_prefix_warning = (
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.')
@fixture(scope="session") @fixture(scope="session")
@ -887,8 +892,6 @@ def pytestconfig(request):
return request.config return request.config
class FixtureManager: class FixtureManager:
""" """
pytest fixtures definitions and information is stored and managed pytest fixtures definitions and information is stored and managed
@ -1063,6 +1066,7 @@ class FixtureManager:
if not callable(obj): if not callable(obj):
continue continue
marker = defaultfuncargprefixmarker marker = defaultfuncargprefixmarker
self.config.warn('C1', funcarg_prefix_warning.format(name=name))
name = name[len(self._argprefix):] name = name[len(self._argprefix):]
elif not isinstance(marker, FixtureFunctionMarker): elif not isinstance(marker, FixtureFunctionMarker):
# magic globals with __getattr__ might have got us a wrong # magic globals with __getattr__ might have got us a wrong
@ -1071,7 +1075,9 @@ class FixtureManager:
else: else:
if marker.name: if marker.name:
name = marker.name name = marker.name
assert not name.startswith(self._argprefix), name msg = 'fixtures cannot have "pytest_funcarg__" prefix ' \
'and be decorated with @pytest.fixture:\n%s' % name
assert not name.startswith(self._argprefix), msg
def new_fixture_def(name, scope): def new_fixture_def(name, scope):
"""Create and registers a new FixtureDef with given name and scope.""" """Create and registers a new FixtureDef with given name and scope."""

View File

@ -34,8 +34,8 @@ def pytest_addoption(parser):
# "**/test_*.py", "**/*_test.py"] # "**/test_*.py", "**/*_test.py"]
#) #)
group = parser.getgroup("general", "running and selection options") group = parser.getgroup("general", "running and selection options")
group._addoption('-x', '--exitfirst', action="store_true", default=False, group._addoption('-x', '--exitfirst', action="store_const",
dest="exitfirst", dest="maxfail", const=1,
help="exit instantly on first error or failed test."), help="exit instantly on first error or failed test."),
group._addoption('--maxfail', metavar="num", group._addoption('--maxfail', metavar="num",
action="store", type=int, dest="maxfail", default=0, action="store", type=int, dest="maxfail", default=0,
@ -74,10 +74,10 @@ def pytest_namespace():
collect = dict(Item=Item, Collector=Collector, File=File, Session=Session) collect = dict(Item=Item, Collector=Collector, File=File, Session=Session)
return dict(collect=collect) return dict(collect=collect)
def pytest_configure(config): def pytest_configure(config):
pytest.config = config # compatibiltiy pytest.config = config # compatibiltiy
if config.option.exitfirst:
config.option.maxfail = 1
def wrap_session(config, doit): def wrap_session(config, doit):
"""Skeleton command line program""" """Skeleton command line program"""

View File

@ -5,11 +5,14 @@ import re
from py.builtin import _basestring from py.builtin import _basestring
import pytest
RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$") RE_IMPORT_ERROR_NAME = re.compile("^No module named (.*)$")
def pytest_funcarg__monkeypatch(request): @pytest.fixture
"""The returned ``monkeypatch`` funcarg provides these def monkeypatch(request):
"""The returned ``monkeypatch`` fixture provides these
helper methods to modify objects, dictionaries or os.environ:: helper methods to modify objects, dictionaries or os.environ::
monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.setattr(obj, name, value, raising=True)
@ -26,7 +29,7 @@ def pytest_funcarg__monkeypatch(request):
parameter determines if a KeyError or AttributeError parameter determines if a KeyError or AttributeError
will be raised if the set/deletion operation has no target. will be raised if the set/deletion operation has no target.
""" """
mpatch = monkeypatch() mpatch = MonkeyPatch()
request.addfinalizer(mpatch.undo) request.addfinalizer(mpatch.undo)
return mpatch return mpatch
@ -93,7 +96,7 @@ class Notset:
notset = Notset() notset = Notset()
class monkeypatch: class MonkeyPatch:
""" Object keeping a record of setattr/item/env/syspath changes. """ """ Object keeping a record of setattr/item/env/syspath changes. """
def __init__(self): def __init__(self):

View File

@ -16,6 +16,7 @@ from _pytest._code import Source
import py import py
import pytest import pytest
from _pytest.main import Session, EXIT_OK from _pytest.main import Session, EXIT_OK
from _pytest.assertion.rewrite import AssertionRewritingHook
def pytest_addoption(parser): def pytest_addoption(parser):
@ -321,7 +322,8 @@ def linecomp(request):
return LineComp() return LineComp()
def pytest_funcarg__LineMatcher(request): @pytest.fixture(name='LineMatcher')
def LineMatcher_fixture(request):
return LineMatcher return LineMatcher
@ -684,8 +686,17 @@ class Testdir:
``pytest.main()`` instance should use. ``pytest.main()`` instance should use.
:return: A :py:class:`HookRecorder` instance. :return: A :py:class:`HookRecorder` instance.
""" """
# When running py.test inline any plugins active in the main
# test process are already imported. So this disables the
# warning which will trigger to say they can no longer be
# re-written, which is fine as they are already re-written.
orig_warn = AssertionRewritingHook._warn_already_imported
def revert():
AssertionRewritingHook._warn_already_imported = orig_warn
self.request.addfinalizer(revert)
AssertionRewritingHook._warn_already_imported = lambda *a: None
rec = [] rec = []
class Collect: class Collect:
def pytest_configure(x, config): def pytest_configure(x, config):

View File

@ -25,10 +25,6 @@ cutdir2 = py.path.local(_pytest.__file__).dirpath()
cutdir1 = py.path.local(pluggy.__file__.rstrip("oc")) cutdir1 = py.path.local(pluggy.__file__.rstrip("oc"))
def _has_positional_arg(func):
return func.__code__.co_argcount
def filter_traceback(entry): def filter_traceback(entry):
# entry.path might sometimes return a str object when the entry # entry.path might sometimes return a str object when the entry
# points to dynamically generated code # points to dynamically generated code
@ -439,34 +435,51 @@ class Module(pytest.File, PyCollector):
"decorator) is not allowed. Use @pytest.mark.skip or " "decorator) is not allowed. Use @pytest.mark.skip or "
"@pytest.mark.skipif instead." "@pytest.mark.skipif instead."
) )
#print "imported test module", mod
self.config.pluginmanager.consider_module(mod) self.config.pluginmanager.consider_module(mod)
return mod return mod
def setup(self): def setup(self):
setup_module = xunitsetup(self.obj, "setUpModule") setup_module = _get_xunit_setup_teardown(self.obj, "setUpModule")
if setup_module is None: if setup_module is None:
setup_module = xunitsetup(self.obj, "setup_module") setup_module = _get_xunit_setup_teardown(self.obj, "setup_module")
if setup_module is not None: if setup_module is not None:
#XXX: nose compat hack, move to nose plugin
# if it takes a positional arg, its probably a pytest style one
# so we pass the current module object
if _has_positional_arg(setup_module):
setup_module(self.obj)
else:
setup_module() setup_module()
fin = getattr(self.obj, 'tearDownModule', None)
if fin is None: teardown_module = _get_xunit_setup_teardown(self.obj, 'tearDownModule')
fin = getattr(self.obj, 'teardown_module', None) if teardown_module is None:
if fin is not None: teardown_module = _get_xunit_setup_teardown(self.obj, 'teardown_module')
#XXX: nose compat hack, move to nose plugin if teardown_module is not None:
# if it takes a positional arg, it's probably a pytest style one self.addfinalizer(teardown_module)
# so we pass the current module object
if _has_positional_arg(fin):
finalizer = lambda: fin(self.obj) def _get_xunit_setup_teardown(holder, attr_name, param_obj=None):
"""
Return a callable to perform xunit-style setup or teardown if
the function exists in the ``holder`` object.
The ``param_obj`` parameter is the parameter which will be passed to the function
when the callable is called without arguments, defaults to the ``holder`` object.
Return ``None`` if a suitable callable is not found.
"""
param_obj = param_obj if param_obj is not None else holder
result = _get_xunit_func(holder, attr_name)
if result is not None:
arg_count = result.__code__.co_argcount
if inspect.ismethod(result):
arg_count -= 1
if arg_count:
return lambda: result(param_obj)
else: else:
finalizer = fin return result
self.addfinalizer(finalizer)
def _get_xunit_func(obj, name):
"""Return the attribute from the given object to be used as a setup/teardown
xunit-style function, but only if not marked as a fixture to
avoid calling it twice.
"""
meth = getattr(obj, name, None)
if fixtures.getfixturemarker(meth) is None:
return meth
class Class(PyCollector): class Class(PyCollector):
@ -479,7 +492,7 @@ class Class(PyCollector):
return [self._getcustomclass("Instance")(name="()", parent=self)] return [self._getcustomclass("Instance")(name="()", parent=self)]
def setup(self): def setup(self):
setup_class = xunitsetup(self.obj, 'setup_class') setup_class = _get_xunit_func(self.obj, 'setup_class')
if setup_class is not None: if setup_class is not None:
setup_class = getattr(setup_class, 'im_func', setup_class) setup_class = getattr(setup_class, 'im_func', setup_class)
setup_class = getattr(setup_class, '__func__', setup_class) setup_class = getattr(setup_class, '__func__', setup_class)
@ -523,12 +536,12 @@ class FunctionMixin(PyobjMixin):
else: else:
setup_name = 'setup_function' setup_name = 'setup_function'
teardown_name = 'teardown_function' teardown_name = 'teardown_function'
setup_func_or_method = xunitsetup(obj, setup_name) setup_func_or_method = _get_xunit_setup_teardown(obj, setup_name, param_obj=self.obj)
if setup_func_or_method is not None: if setup_func_or_method is not None:
setup_func_or_method(self.obj) setup_func_or_method()
fin = getattr(obj, teardown_name, None) teardown_func_or_method = _get_xunit_setup_teardown(obj, teardown_name, param_obj=self.obj)
if fin is not None: if teardown_func_or_method is not None:
self.addfinalizer(lambda: fin(self.obj)) self.addfinalizer(teardown_func_or_method)
def _prunetraceback(self, excinfo): def _prunetraceback(self, excinfo):
if hasattr(self, '_obj') and not self.config.option.fulltrace: if hasattr(self, '_obj') and not self.config.option.fulltrace:
@ -589,6 +602,8 @@ class Generator(FunctionMixin, PyCollector):
raise ValueError("%r generated tests with non-unique name %r" %(self, name)) raise ValueError("%r generated tests with non-unique name %r" %(self, name))
seen[name] = True seen[name] = True
l.append(self.Function(name, self, args=args, callobj=call)) l.append(self.Function(name, self, args=args, callobj=call))
msg = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
self.config.warn('C1', msg, fslocation=self.fspath)
return l return l
def getcallargs(self, obj): def getcallargs(self, obj):
@ -611,8 +626,6 @@ def hasinit(obj):
return True return True
class CallSpec2(object): class CallSpec2(object):
def __init__(self, metafunc): def __init__(self, metafunc):
self.metafunc = metafunc self.metafunc = metafunc
@ -1493,8 +1506,3 @@ class Function(FunctionMixin, pytest.Item, fixtures.FuncargnamesCompatAttr):
super(Function, self).setup() super(Function, self).setup()
fixtures.fillfixtures(self) fixtures.fillfixtures(self)
def xunitsetup(obj, name):
meth = getattr(obj, name, None)
if fixtures.getfixturemarker(meth) is None:
return meth

View File

@ -108,11 +108,7 @@ class MarkEvaluator:
def _getglobals(self): def _getglobals(self):
d = {'os': os, 'sys': sys, 'config': self.item.config} d = {'os': os, 'sys': sys, 'config': self.item.config}
func = self.item.obj d.update(self.item.obj.__globals__)
try:
d.update(func.__globals__)
except AttributeError:
d.update(func.func_globals)
return d return d
def _istrue(self): def _istrue(self):

View File

@ -20,10 +20,15 @@ def pytest_addoption(parser):
group._addoption('-q', '--quiet', action="count", group._addoption('-q', '--quiet', action="count",
dest="quiet", default=0, help="decrease verbosity."), dest="quiet", default=0, help="decrease verbosity."),
group._addoption('-r', group._addoption('-r',
action="store", dest="reportchars", default=None, metavar="chars", action="store", dest="reportchars", default='', metavar="chars",
help="show extra test summary info as specified by chars (f)ailed, " help="show extra test summary info as specified by chars (f)ailed, "
"(E)error, (s)skipped, (x)failed, (X)passed (w)pytest-warnings " "(E)error, (s)skipped, (x)failed, (X)passed, "
"(p)passed, (P)passed with output, (a)all except pP.") "(p)passed, (P)passed with output, (a)all except pP. "
"The pytest warnings are displayed at all times except when "
"--disable-pytest-warnings is set")
group._addoption('--disable-pytest-warnings', default=False,
dest='disablepytestwarnings', action='store_true',
help='disable warnings summary, overrides -r w flag')
group._addoption('-l', '--showlocals', group._addoption('-l', '--showlocals',
action="store_true", dest="showlocals", default=False, action="store_true", dest="showlocals", default=False,
help="show locals in tracebacks (disabled by default).") help="show locals in tracebacks (disabled by default).")
@ -52,6 +57,10 @@ def pytest_configure(config):
def getreportopt(config): def getreportopt(config):
reportopts = "" reportopts = ""
reportchars = config.option.reportchars reportchars = config.option.reportchars
if not config.option.disablepytestwarnings and 'w' not in reportchars:
reportchars += 'w'
elif config.option.disablepytestwarnings and 'w' in reportchars:
reportchars = reportchars.replace('w', '')
if reportchars: if reportchars:
for char in reportchars: for char in reportchars:
if char not in reportopts and char != 'a': if char not in reportopts and char != 'a':

View File

@ -3,7 +3,7 @@ import re
import pytest import pytest
import py import py
from _pytest.monkeypatch import monkeypatch from _pytest.monkeypatch import MonkeyPatch
class TempdirFactory: class TempdirFactory:
@ -92,7 +92,7 @@ def pytest_configure(config):
available at pytest_configure time, but ideally should be moved entirely available at pytest_configure time, but ideally should be moved entirely
to the tmpdir_factory session fixture. to the tmpdir_factory session fixture.
""" """
mp = monkeypatch() mp = MonkeyPatch()
t = TempdirFactory(config) t = TempdirFactory(config)
config._cleanup.extend([mp.undo, t.finish]) config._cleanup.extend([mp.undo, t.finish])
mp.setattr(config, '_tmpdirhandler', t, raising=False) mp.setattr(config, '_tmpdirhandler', t, raising=False)

View File

@ -85,7 +85,7 @@ and if you need to have access to the actual exception info you may use::
the actual exception raised. The main attributes of interest are the actual exception raised. The main attributes of interest are
``.type``, ``.value`` and ``.traceback``. ``.type``, ``.value`` and ``.traceback``.
.. versionchanged:: 2.10 .. versionchanged:: 3.0
In the context manager form you may use the keyword argument In the context manager form you may use the keyword argument
``message`` to specify a custom failure message:: ``message`` to specify a custom failure message::

View File

@ -0,0 +1,12 @@
.. _backwards-compatibility:
Backwards Compatibility Policy
==============================
Keeping backwards compatibility has a very high priority in the pytest project. Although we have deprecated functionality over the years, most of it is still supported. All deprecations in pytest were done because simpler or more efficient ways of accomplishing the same tasks have emerged, making the old way of doing things unnecessary.
With the pytest 3.0 release we introduced a clear communication scheme for when we will actually remove the old busted joint and politely ask you to use the new hotness instead, while giving you enough time to adjust your tests or raise concerns if there are valid reasons to keep deprecated functionality around.
To communicate changes we are already issuing deprecation warnings, but they are not displayed by default. In pytest 3.0 we changed the default setting so that pytest deprecation warnings are displayed if not explicitly silenced (with ``--disable-pytest-warnings``).
We will only remove deprecated functionality in major releases (e.g. if we deprecate something in 3.0 we will remove it in 4.0), and keep it around for at least two minor releases (e.g. if we deprecate something in 3.9 and 4.0 is the next release, we will not remove it in 4.0 but in 5.0).

View File

@ -116,7 +116,7 @@ libraries or subprocesses that directly write to operating
system level output streams (FD1 and FD2). system level output streams (FD1 and FD2).
.. versionadded:: 2.10 .. versionadded:: 3.0
To temporarily disable capture within a test, both ``capsys`` To temporarily disable capture within a test, both ``capsys``
and ``capfd`` have a ``disabled()`` method that can be used and ``capfd`` have a ``disabled()`` method that can be used

View File

@ -20,6 +20,7 @@ Full pytest documentation
cache cache
plugins plugins
backwards-compatibility
contributing contributing
talks talks

View File

@ -105,7 +105,7 @@ itself::
The 'doctest_namespace' fixture The 'doctest_namespace' fixture
------------------------------- -------------------------------
.. versionadded:: 2.10 .. versionadded:: 3.0
The ``doctest_namespace`` fixture can be used to inject items into the The ``doctest_namespace`` fixture can be used to inject items into the
namespace in which your doctests run. It is intended to be used within namespace in which your doctests run. It is intended to be used within

View File

@ -204,7 +204,7 @@ This will add an extra property ``example_key="1"`` to the generated
LogXML: add_global_property LogXML: add_global_property
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. versionadded:: 2.10 .. versionadded:: 3.0
If you want to add a properties node in the testsuite level, which may contains properties that are relevant If you want to add a properties node in the testsuite level, which may contains properties that are relevant
to all testcases you can use ``LogXML.add_global_properties`` to all testcases you can use ``LogXML.add_global_properties``

View File

@ -176,6 +176,63 @@ If a package is installed this way, ``pytest`` will load
to make it easy for users to find your plugin. to make it easy for users to find your plugin.
Assertion Rewriting
-------------------
One of the main features of ``pytest`` is the use of plain assert
statements and the detailed introspection of expressions upon
assertion failures. This is provided by "assertion rewriting" which
modifies the parsed AST before it gets compiled to bytecode. This is
done via a :pep:`302` import hook which gets installed early on when
``pytest`` starts up and will perform this re-writing when modules get
imported. However since we do not want to test different bytecode
then you will run in production this hook only re-writes test modules
themselves as well as any modules which are part of plugins. Any
other imported module will not be re-written and normal assertion
behaviour will happen.
If you have assertion helpers in other modules where you would need
assertion rewriting to be enabled you need to ask ``pytest``
explicitly to re-write this module before it gets imported.
.. autofunction:: pytest.register_assert_rewrite
This is especially important when you write a pytest plugin which is
created using a package. The import hook only treats ``conftest.py``
files and any modules which are listed in the ``pytest11`` entrypoint
as plugins. As an example consider the following package::
pytest_foo/__init__.py
pytest_foo/plugin.py
pytest_foo/helper.py
With the following typical ``setup.py`` extract:
.. code-block:: python
setup(
...
entry_points={'pytest11': ['foo = pytest_foo.plugin']},
...
)
In this case only ``pytest_foo/plugin.py`` will be re-written. If the
helper module also contains assert statements which need to be
re-written it needs to be marked as such, before it gets imported.
This is easiest by marking it for re-writing inside the
``__init__.py`` module, which will always be imported first when a
module inside a package is imported. This way ``plugin.py`` can still
import ``helper.py`` normally. The contents of
``pytest_foo/__init__.py`` will then need to look like this:
.. code-block:: python
import pytest
pytest.register_assert_rewrite('pytest_foo.helper')
Requiring/Loading plugins in a test module or conftest file Requiring/Loading plugins in a test module or conftest file
----------------------------------------------------------- -----------------------------------------------------------
@ -190,6 +247,16 @@ will be loaded as well. You can also use dotted path like this::
which will import the specified module as a ``pytest`` plugin. which will import the specified module as a ``pytest`` plugin.
Plugins imported like this will automatically be marked to require
assertion rewriting using the :func:`pytest.register_assert_rewrite`
mechanism. However for this to have any effect the module must not be
imported already, it it was already imported at the time the
``pytest_plugins`` statement is processed a warning will result and
assertions inside the plugin will not be re-written. To fix this you
can either call :func:`pytest.register_assert_rewrite` yourself before
the module is imported, or you can arrange the code to delay the
importing until after the plugin is registered.
Accessing another plugin by name Accessing another plugin by name
-------------------------------- --------------------------------

View File

@ -7,21 +7,20 @@ classic xunit-style setup
This section describes a classic and popular way how you can implement This section describes a classic and popular way how you can implement
fixtures (setup and teardown test state) on a per-module/class/function basis. fixtures (setup and teardown test state) on a per-module/class/function basis.
pytest started supporting these methods around 2005 and subsequently
nose and the standard library introduced them (under slightly different
names). While these setup/teardown methods are and will remain fully
supported you may also use pytest's more powerful :ref:`fixture mechanism
<fixture>` which leverages the concept of dependency injection, allowing
for a more modular and more scalable approach for managing test state,
especially for larger projects and for functional testing. You can
mix both fixture mechanisms in the same file but unittest-based
test methods cannot receive fixture arguments.
.. note:: .. note::
As of pytest-2.4, teardownX functions are not called if While these setup/teardown methods are simple and familiar to those
setupX existed and failed/was skipped. This harmonizes coming from a ``unittest`` or nose ``background``, you may also consider
behaviour across all major python testing tools. using pytest's more powerful :ref:`fixture mechanism
<fixture>` which leverages the concept of dependency injection, allowing
for a more modular and more scalable approach for managing test state,
especially for larger projects and for functional testing. You can
mix both fixture mechanisms in the same file but
test methods of ``unittest.TestCase`` subclasses
cannot receive fixture arguments.
Module level setup/teardown Module level setup/teardown
-------------------------------------- --------------------------------------
@ -38,6 +37,8 @@ which will usually be called once for all the functions::
method. method.
""" """
As of pytest-3.0, the ``module`` parameter is optional.
Class level setup/teardown Class level setup/teardown
---------------------------------- ----------------------------------
@ -71,6 +72,8 @@ Similarly, the following methods are called around each method invocation::
call. call.
""" """
As of pytest-3.0, the ``method`` parameter is optional.
If you would rather define test functions directly at module level If you would rather define test functions directly at module level
you can also use the following functions to implement fixtures:: you can also use the following functions to implement fixtures::
@ -84,7 +87,13 @@ you can also use the following functions to implement fixtures::
call. call.
""" """
Note that it is possible for setup/teardown pairs to be invoked multiple times As of pytest-3.0, the ``function`` parameter is optional.
per testing process.
Remarks:
* It is possible for setup/teardown pairs to be invoked multiple times
per testing process.
* teardown functions are not called if the corresponding setup function existed
and failed/was skipped.
.. _`unittest.py module`: http://docs.python.org/library/unittest.html .. _`unittest.py module`: http://docs.python.org/library/unittest.html

View File

@ -3,12 +3,12 @@
"yield_fixture" functions "yield_fixture" functions
--------------------------------------------------------------- ---------------------------------------------------------------
.. deprecated:: 2.10 .. deprecated:: 3.0
.. versionadded:: 2.4 .. versionadded:: 2.4
.. important:: .. important::
Since pytest-2.10, fixtures using the normal ``fixture`` decorator can use a ``yield`` Since pytest-3.0, fixtures using the normal ``fixture`` decorator can use a ``yield``
statement to provide fixture values and execute teardown code, exactly like ``yield_fixture`` statement to provide fixture values and execute teardown code, exactly like ``yield_fixture``
in previous versions. in previous versions.

View File

@ -762,3 +762,52 @@ class TestDurationWithFixture:
* setup *test_1* * setup *test_1*
* call *test_1* * call *test_1*
""") """)
def test_yield_tests_deprecation(testdir):
testdir.makepyfile("""
def func1(arg, arg2):
assert arg == arg2
def test_gen():
yield "m1", func1, 15, 3*5
yield "m2", func1, 42, 6*7
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
'*2 passed*',
])
def test_funcarg_prefix_deprecation(testdir):
testdir.makepyfile("""
def pytest_funcarg__value():
return 10
def test_funcarg_prefix(value):
assert value == 10
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
('WC1 None pytest_funcarg__value: '
'declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
'*1 passed*',
])
def test_str_args_deprecated(tmpdir, testdir):
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
warnings = []
class Collect:
def pytest_logwarning(self, message):
warnings.append(message)
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
testdir.delete_loaded_modules()
msg = ('passing a string to pytest.main() is deprecated, '
'pass a list of arguments instead.')
assert msg in warnings
assert ret == EXIT_NOTESTSCOLLECTED

View File

@ -66,24 +66,6 @@ def test_code_from_func():
assert co.path assert co.path
def test_builtin_patch_unpatch(monkeypatch):
cpy_builtin = py.builtin.builtins
comp = cpy_builtin.compile
def mycompile(*args, **kwargs):
return comp(*args, **kwargs)
class Sub(AssertionError):
pass
monkeypatch.setattr(cpy_builtin, 'AssertionError', Sub)
monkeypatch.setattr(cpy_builtin, 'compile', mycompile)
_pytest._code.patch_builtins()
assert cpy_builtin.AssertionError != Sub
assert cpy_builtin.compile != mycompile
_pytest._code.unpatch_builtins()
assert cpy_builtin.AssertionError is Sub
assert cpy_builtin.compile == mycompile
def test_unicode_handling(): def test_unicode_handling():
value = py.builtin._totext('\xc4\x85\xc4\x87\n', 'utf-8').encode('utf8') value = py.builtin._totext('\xc4\x85\xc4\x87\n', 'utf-8').encode('utf8')
def f(): def f():

View File

@ -274,18 +274,6 @@ class TestTraceback_f_g_h:
assert entry.lineno == co.firstlineno + 2 assert entry.lineno == co.firstlineno + 2
assert entry.frame.code.name == 'g' assert entry.frame.code.name == 'g'
def hello(x):
x + 5
def test_tbentry_reinterpret():
try:
hello("hello")
except TypeError:
excinfo = _pytest._code.ExceptionInfo()
tbentry = excinfo.traceback[-1]
msg = tbentry.reinterpret()
assert msg.startswith("TypeError: ('hello' + 5)")
def test_excinfo_exconly(): def test_excinfo_exconly():
excinfo = pytest.raises(ValueError, h) excinfo = pytest.raises(ValueError, h)
assert excinfo.exconly().startswith('ValueError') assert excinfo.exconly().startswith('ValueError')
@ -381,7 +369,9 @@ def test_match_raises_error(testdir):
]) ])
class TestFormattedExcinfo: class TestFormattedExcinfo:
def pytest_funcarg__importasmod(self, request):
@pytest.fixture
def importasmod(self, request):
def importasmod(source): def importasmod(source):
source = _pytest._code.Source(source) source = _pytest._code.Source(source)
tmpdir = request.getfixturevalue("tmpdir") tmpdir = request.getfixturevalue("tmpdir")
@ -429,7 +419,7 @@ class TestFormattedExcinfo:
assert lines == [ assert lines == [
' def f():', ' def f():',
'> assert 0', '> assert 0',
'E assert 0' 'E AssertionError'
] ]
@ -768,23 +758,6 @@ raise ValueError()
assert reprtb.extraline == "!!! Recursion detected (same locals & position)" assert reprtb.extraline == "!!! Recursion detected (same locals & position)"
assert str(reprtb) assert str(reprtb)
def test_tb_entry_AssertionError(self, importasmod):
# probably this test is a bit redundant
# as py/magic/testing/test_assertion.py
# already tests correctness of
# assertion-reinterpretation logic
mod = importasmod("""
def somefunc():
x = 1
assert x == 2
""")
excinfo = pytest.raises(AssertionError, mod.somefunc)
p = FormattedExcinfo()
reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
lines = reprentry.lines
assert lines[-1] == "E assert 1 == 2"
def test_reprexcinfo_getrepr(self, importasmod): def test_reprexcinfo_getrepr(self, importasmod):
mod = importasmod(""" mod = importasmod("""
def f(x): def f(x):
@ -933,21 +906,6 @@ raise ValueError()
repr.toterminal(tw) repr.toterminal(tw)
assert tw.stringio.getvalue() assert tw.stringio.getvalue()
def test_native_style(self):
excinfo = self.excinfo_from_exec("""
assert 0
""")
repr = excinfo.getrepr(style='native')
assert "assert 0" in str(repr.reprcrash)
s = str(repr)
assert s.startswith('Traceback (most recent call last):\n File')
assert s.endswith('\nAssertionError: assert 0')
assert 'exec (source.compile())' in s
# python 2.4 fails to get the source line for the assert
if py.std.sys.version_info >= (2, 5):
assert s.count('assert 0') == 2
def test_traceback_repr_style(self, importasmod): def test_traceback_repr_style(self, importasmod):
mod = importasmod(""" mod = importasmod("""
def f(): def f():

View File

@ -285,13 +285,14 @@ class TestSourceParsingAndCompiling:
#print "block", str(block) #print "block", str(block)
assert str(stmt).strip().startswith('assert') assert str(stmt).strip().startswith('assert')
def test_compilefuncs_and_path_sanity(self): @pytest.mark.parametrize('name', ['', None, 'my'])
def test_compilefuncs_and_path_sanity(self, name):
def check(comp, name): def check(comp, name):
co = comp(self.source, name) co = comp(self.source, name)
if not name: if not name:
expected = "codegen %s:%d>" %(mypath, mylineno+2+1) expected = "codegen %s:%d>" %(mypath, mylineno+2+2)
else: else:
expected = "codegen %r %s:%d>" % (name, mypath, mylineno+2+1) expected = "codegen %r %s:%d>" % (name, mypath, mylineno+2+2)
fn = co.co_filename fn = co.co_filename
assert fn.endswith(expected) assert fn.endswith(expected)
@ -300,8 +301,7 @@ class TestSourceParsingAndCompiling:
mypath = mycode.path mypath = mycode.path
for comp in _pytest._code.compile, _pytest._code.Source.compile: for comp in _pytest._code.compile, _pytest._code.Source.compile:
for name in '', None, 'my': check(comp, name)
yield check, comp, name
def test_offsetless_synerr(self): def test_offsetless_synerr(self):
pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode='eval') pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode='eval')

View File

@ -795,21 +795,24 @@ class TestTracebackCutting:
def test_traceback_argsetup(self, testdir): def test_traceback_argsetup(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_funcarg__hello(request): import pytest
@pytest.fixture
def hello(request):
raise ValueError("xyz") raise ValueError("xyz")
""") """)
p = testdir.makepyfile("def test(hello): pass") p = testdir.makepyfile("def test(hello): pass")
result = testdir.runpytest(p) result = testdir.runpytest(p)
assert result.ret != 0 assert result.ret != 0
out = result.stdout.str() out = result.stdout.str()
assert out.find("xyz") != -1 assert "xyz" in out
assert out.find("conftest.py:2: ValueError") != -1 assert "conftest.py:5: ValueError" in out
numentries = out.count("_ _ _") # separator for traceback entries numentries = out.count("_ _ _") # separator for traceback entries
assert numentries == 0 assert numentries == 0
result = testdir.runpytest("--fulltrace", p) result = testdir.runpytest("--fulltrace", p)
out = result.stdout.str() out = result.stdout.str()
assert out.find("conftest.py:2: ValueError") != -1 assert "conftest.py:5: ValueError" in out
numentries = out.count("_ _ _ _") # separator for traceback entries numentries = out.count("_ _ _ _") # separator for traceback entries
assert numentries > 3 assert numentries > 3

View File

@ -30,7 +30,10 @@ class TestFillFixtures:
def test_funcarg_lookupfails(self, testdir): def test_funcarg_lookupfails(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__xyzsomething(request): import pytest
@pytest.fixture
def xyzsomething(request):
return 42 return 42
def test_func(some): def test_func(some):
@ -46,9 +49,13 @@ class TestFillFixtures:
def test_funcarg_basic(self, testdir): def test_funcarg_basic(self, testdir):
item = testdir.getitem(""" item = testdir.getitem("""
def pytest_funcarg__some(request): import pytest
@pytest.fixture
def some(request):
return request.function.__name__ return request.function.__name__
def pytest_funcarg__other(request): @pytest.fixture
def other(request):
return 42 return 42
def test_func(some, other): def test_func(some, other):
pass pass
@ -61,7 +68,10 @@ class TestFillFixtures:
def test_funcarg_lookup_modulelevel(self, testdir): def test_funcarg_lookup_modulelevel(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
return request.function.__name__ return request.function.__name__
class TestClass: class TestClass:
@ -75,9 +85,13 @@ class TestFillFixtures:
def test_funcarg_lookup_classlevel(self, testdir): def test_funcarg_lookup_classlevel(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
import pytest
class TestClass: class TestClass:
def pytest_funcarg__something(self, request):
@pytest.fixture
def something(self, request):
return request.instance return request.instance
def test_method(self, something): def test_method(self, something):
assert something is self assert something is self
""") """)
@ -91,12 +105,14 @@ class TestFillFixtures:
sub2 = testdir.mkpydir("sub2") sub2 = testdir.mkpydir("sub2")
sub1.join("conftest.py").write(_pytest._code.Source(""" sub1.join("conftest.py").write(_pytest._code.Source("""
import pytest import pytest
def pytest_funcarg__arg1(request): @pytest.fixture
def arg1(request):
pytest.raises(Exception, "request.getfixturevalue('arg2')") pytest.raises(Exception, "request.getfixturevalue('arg2')")
""")) """))
sub2.join("conftest.py").write(_pytest._code.Source(""" sub2.join("conftest.py").write(_pytest._code.Source("""
import pytest import pytest
def pytest_funcarg__arg2(request): @pytest.fixture
def arg2(request):
pytest.raises(Exception, "request.getfixturevalue('arg1')") pytest.raises(Exception, "request.getfixturevalue('arg1')")
""")) """))
@ -396,7 +412,10 @@ class TestFillFixtures:
class TestRequestBasic: class TestRequestBasic:
def test_request_attributes(self, testdir): def test_request_attributes(self, testdir):
item = testdir.getitem(""" item = testdir.getitem("""
def pytest_funcarg__something(request): pass import pytest
@pytest.fixture
def something(request): pass
def test_func(something): pass def test_func(something): pass
""") """)
req = fixtures.FixtureRequest(item) req = fixtures.FixtureRequest(item)
@ -410,8 +429,11 @@ class TestRequestBasic:
def test_request_attributes_method(self, testdir): def test_request_attributes_method(self, testdir):
item, = testdir.getitems(""" item, = testdir.getitems("""
import pytest
class TestB: class TestB:
def pytest_funcarg__something(self, request):
@pytest.fixture
def something(self, request):
return 1 return 1
def test_func(self, something): def test_func(self, something):
pass pass
@ -420,9 +442,11 @@ class TestRequestBasic:
assert req.cls.__name__ == "TestB" assert req.cls.__name__ == "TestB"
assert req.instance.__class__ == req.cls assert req.instance.__class__ == req.cls
def XXXtest_request_contains_funcarg_arg2fixturedefs(self, testdir): def test_request_contains_funcarg_arg2fixturedefs(self, testdir):
modcol = testdir.getmodulecol(""" modcol = testdir.getmodulecol("""
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
pass pass
class TestClass: class TestClass:
def test_method(self, something): def test_method(self, something):
@ -432,15 +456,21 @@ class TestRequestBasic:
assert item1.name == "test_method" assert item1.name == "test_method"
arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs
assert len(arg2fixturedefs) == 1 assert len(arg2fixturedefs) == 1
assert arg2fixturedefs[0].__name__ == "pytest_funcarg__something" assert arg2fixturedefs['something'][0].argname == "something"
def test_getfixturevalue_recursive(self, testdir): def test_getfixturevalue_recursive(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
return 1 return 1
""") """)
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
return request.getfixturevalue("something") + 1 return request.getfixturevalue("something") + 1
def test_func(something): def test_func(something):
assert something == 2 assert something == 2
@ -452,9 +482,12 @@ class TestRequestBasic:
'getfixmethod', ('getfixturevalue', 'getfuncargvalue')) 'getfixmethod', ('getfixturevalue', 'getfuncargvalue'))
def test_getfixturevalue(self, testdir, getfixmethod): def test_getfixturevalue(self, testdir, getfixmethod):
item = testdir.getitem(""" item = testdir.getitem("""
import pytest
l = [2] l = [2]
def pytest_funcarg__something(request): return 1 @pytest.fixture
def pytest_funcarg__other(request): def something(request): return 1
@pytest.fixture
def other(request):
return l.pop() return l.pop()
def test_func(something): pass def test_func(something): pass
""") """)
@ -477,8 +510,10 @@ class TestRequestBasic:
def test_request_addfinalizer(self, testdir): def test_request_addfinalizer(self, testdir):
item = testdir.getitem(""" item = testdir.getitem("""
import pytest
teardownlist = [] teardownlist = []
def pytest_funcarg__something(request): @pytest.fixture
def something(request):
request.addfinalizer(lambda: teardownlist.append(1)) request.addfinalizer(lambda: teardownlist.append(1))
def test_func(something): pass def test_func(something): pass
""") """)
@ -503,7 +538,8 @@ class TestRequestBasic:
result = testdir.runpytest_subprocess() result = testdir.runpytest_subprocess()
assert result.ret != 0 assert result.ret != 0
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*AssertionError:*pytest_funcarg__marked_with_prefix_and_decorator*" "*AssertionError: fixtures cannot have*@pytest.fixture*",
"*pytest_funcarg__marked_with_prefix_and_decorator*"
]) ])
def test_request_addfinalizer_failing_setup(self, testdir): def test_request_addfinalizer_failing_setup(self, testdir):
@ -541,8 +577,10 @@ class TestRequestBasic:
def test_request_addfinalizer_partial_setup_failure(self, testdir): def test_request_addfinalizer_partial_setup_failure(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
import pytest
l = [] l = []
def pytest_funcarg__something(request): @pytest.fixture
def something(request):
request.addfinalizer(lambda: l.append(None)) request.addfinalizer(lambda: l.append(None))
def test_func(something, missingarg): def test_func(something, missingarg):
pass pass
@ -583,9 +621,11 @@ class TestRequestBasic:
def test_funcargnames_compatattr(self, testdir): def test_funcargnames_compatattr(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
assert metafunc.funcargnames == metafunc.fixturenames assert metafunc.funcargnames == metafunc.fixturenames
def pytest_funcarg__fn(request): @pytest.fixture
def fn(request):
assert request._pyfuncitem.funcargnames == \ assert request._pyfuncitem.funcargnames == \
request._pyfuncitem.fixturenames request._pyfuncitem.fixturenames
return request.funcargnames, request.fixturenames return request.funcargnames, request.fixturenames
@ -630,7 +670,9 @@ class TestRequestBasic:
# this tests that normalization of nodeids takes place # this tests that normalization of nodeids takes place
b = testdir.mkdir("tests").mkdir("unit") b = testdir.mkdir("tests").mkdir("unit")
b.join("conftest.py").write(_pytest._code.Source(""" b.join("conftest.py").write(_pytest._code.Source("""
def pytest_funcarg__arg1(): import pytest
@pytest.fixture
def arg1():
pass pass
""")) """))
p = b.join("test_module.py") p = b.join("test_module.py")
@ -678,7 +720,10 @@ class TestRequestBasic:
class TestRequestMarking: class TestRequestMarking:
def test_applymarker(self, testdir): def test_applymarker(self, testdir):
item1,item2 = testdir.getitems(""" item1,item2 = testdir.getitems("""
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
pass pass
class TestClass: class TestClass:
def test_func1(self, something): def test_func1(self, something):
@ -737,7 +782,10 @@ class TestRequestCachedSetup:
reprec = testdir.inline_runsource(""" reprec = testdir.inline_runsource("""
mysetup = ["hello",].pop mysetup = ["hello",].pop
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
return request.cached_setup(mysetup, scope="module") return request.cached_setup(mysetup, scope="module")
def test_func1(something): def test_func1(something):
@ -752,7 +800,9 @@ class TestRequestCachedSetup:
reprec = testdir.inline_runsource(""" reprec = testdir.inline_runsource("""
mysetup = ["hello", "hello2", "hello3"].pop mysetup = ["hello", "hello2", "hello3"].pop
def pytest_funcarg__something(request): import pytest
@pytest.fixture
def something(request):
return request.cached_setup(mysetup, scope="class") return request.cached_setup(mysetup, scope="class")
def test_func1(something): def test_func1(something):
assert something == "hello3" assert something == "hello3"
@ -802,9 +852,13 @@ class TestRequestCachedSetup:
def test_request_cached_setup_two_args(self, testdir): def test_request_cached_setup_two_args(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
return request.cached_setup(lambda: 42) return request.cached_setup(lambda: 42)
def pytest_funcarg__arg2(request): @pytest.fixture
def arg2(request):
return request.cached_setup(lambda: 17) return request.cached_setup(lambda: 17)
def test_two_different_setups(arg1, arg2): def test_two_different_setups(arg1, arg2):
assert arg1 != arg2 assert arg1 != arg2
@ -816,10 +870,14 @@ class TestRequestCachedSetup:
def test_request_cached_setup_getfixturevalue(self, testdir): def test_request_cached_setup_getfixturevalue(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
arg1 = request.getfixturevalue("arg2") arg1 = request.getfixturevalue("arg2")
return request.cached_setup(lambda: arg1 + 1) return request.cached_setup(lambda: arg1 + 1)
def pytest_funcarg__arg2(request): @pytest.fixture
def arg2(request):
return request.cached_setup(lambda: 10) return request.cached_setup(lambda: 10)
def test_two_funcarg(arg1): def test_two_funcarg(arg1):
assert arg1 == 11 assert arg1 == 11
@ -831,8 +889,10 @@ class TestRequestCachedSetup:
def test_request_cached_setup_functional(self, testdir): def test_request_cached_setup_functional(self, testdir):
testdir.makepyfile(test_0=""" testdir.makepyfile(test_0="""
import pytest
l = [] l = []
def pytest_funcarg__something(request): @pytest.fixture
def something(request):
val = request.cached_setup(fsetup, fteardown) val = request.cached_setup(fsetup, fteardown)
return val return val
def fsetup(mycache=[1]): def fsetup(mycache=[1]):
@ -858,7 +918,10 @@ class TestRequestCachedSetup:
def test_issue117_sessionscopeteardown(self, testdir): def test_issue117_sessionscopeteardown(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__app(request): import pytest
@pytest.fixture
def app(request):
app = request.cached_setup( app = request.cached_setup(
scope='session', scope='session',
setup=lambda: 0, setup=lambda: 0,
@ -1119,16 +1182,23 @@ class TestFixtureUsages:
class TestFixtureManagerParseFactories: class TestFixtureManagerParseFactories:
def pytest_funcarg__testdir(self, request):
@pytest.fixture
def testdir(self, request):
testdir = request.getfixturevalue("testdir") testdir = request.getfixturevalue("testdir")
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_funcarg__hello(request): import pytest
@pytest.fixture
def hello(request):
return "conftest" return "conftest"
def pytest_funcarg__fm(request): @pytest.fixture
def fm(request):
return request._fixturemanager return request._fixturemanager
def pytest_funcarg__item(request): @pytest.fixture
def item(request):
return request._pyfuncitem return request._pyfuncitem
""") """)
return testdir return testdir
@ -1154,17 +1224,21 @@ class TestFixtureManagerParseFactories:
faclist = fm.getfixturedefs(name, item.nodeid) faclist = fm.getfixturedefs(name, item.nodeid)
assert len(faclist) == 1 assert len(faclist) == 1
fac = faclist[0] fac = faclist[0]
assert fac.func.__name__ == "pytest_funcarg__" + name assert fac.func.__name__ == name
""") """)
reprec = testdir.inline_run("-s") reprec = testdir.inline_run("-s")
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_parsefactories_conftest_and_module_and_class(self, testdir): def test_parsefactories_conftest_and_module_and_class(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__hello(request): import pytest
@pytest.fixture
def hello(request):
return "module" return "module"
class TestClass: class TestClass:
def pytest_funcarg__hello(self, request): @pytest.fixture
def hello(self, request):
return "class" return "class"
def test_hello(self, item, fm): def test_hello(self, item, fm):
faclist = fm.getfixturedefs("hello", item.nodeid) faclist = fm.getfixturedefs("hello", item.nodeid)
@ -1212,7 +1286,9 @@ class TestFixtureManagerParseFactories:
class TestAutouseDiscovery: class TestAutouseDiscovery:
def pytest_funcarg__testdir(self, testdir):
@pytest.fixture
def testdir(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -1226,10 +1302,12 @@ class TestAutouseDiscovery:
def perfunction2(arg1): def perfunction2(arg1):
pass pass
def pytest_funcarg__fm(request): @pytest.fixture
def fm(request):
return request._fixturemanager return request._fixturemanager
def pytest_funcarg__item(request): @pytest.fixture
def item(request):
return request._pyfuncitem return request._pyfuncitem
""") """)
return testdir return testdir
@ -1508,7 +1586,8 @@ class TestAutouseManagement:
def test_2(self): def test_2(self):
pass pass
""") """)
reprec = testdir.inline_run("-v","-s") confcut = "--confcutdir={0}".format(testdir.tmpdir)
reprec = testdir.inline_run("-v","-s", confcut)
reprec.assertoutcome(passed=8) reprec.assertoutcome(passed=8)
config = reprec.getcalls("pytest_unconfigure")[0].config config = reprec.getcalls("pytest_unconfigure")[0].config
l = config.pluginmanager._getconftestmodules(p)[0].l l = config.pluginmanager._getconftestmodules(p)[0].l
@ -1773,17 +1852,19 @@ class TestFixtureMarker:
def test_scope_module_and_finalizer(self, testdir): def test_scope_module_and_finalizer(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
finalized = [] finalized_list = []
created = [] created_list = []
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def arg(request): def arg(request):
created.append(1) created_list.append(1)
assert request.scope == "module" assert request.scope == "module"
request.addfinalizer(lambda: finalized.append(1)) request.addfinalizer(lambda: finalized_list.append(1))
def pytest_funcarg__created(request): @pytest.fixture
return len(created) def created(request):
def pytest_funcarg__finalized(request): return len(created_list)
return len(finalized) @pytest.fixture
def finalized(request):
return len(finalized_list)
""") """)
testdir.makepyfile( testdir.makepyfile(
test_mod1=""" test_mod1="""

View File

@ -15,7 +15,9 @@ class TestOEJSKITSpecials:
return self.fspath, 3, "xyz" return self.fspath, 3, "xyz"
""") """)
modcol = testdir.getmodulecol(""" modcol = testdir.getmodulecol("""
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
return 42 return 42
class MyClass: class MyClass:
pass pass
@ -43,7 +45,8 @@ class TestOEJSKITSpecials:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def hello(): def hello():
pass pass
def pytest_funcarg__arg1(request): @pytest.fixture
def arg1(request):
return 42 return 42
class MyClass: class MyClass:
pass pass

View File

@ -448,13 +448,13 @@ class TestMetafunc:
def test_parametrize_functional(self, testdir): def test_parametrize_functional(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize('x', [1,2], indirect=True) metafunc.parametrize('x', [1,2], indirect=True)
metafunc.parametrize('y', [2]) metafunc.parametrize('y', [2])
def pytest_funcarg__x(request): @pytest.fixture
def x(request):
return request.param * 10 return request.param * 10
#def pytest_funcarg__y(request):
# return request.param
def test_simple(x,y): def test_simple(x,y):
assert x in (10,20) assert x in (10,20)
@ -578,7 +578,8 @@ class TestMetafuncFunctional:
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.addcall(param=metafunc) metafunc.addcall(param=metafunc)
def pytest_funcarg__metafunc(request): @pytest.fixture
def metafunc(request):
assert request._pyfuncitem._genid == "0" assert request._pyfuncitem._genid == "0"
return request.param return request.param
@ -630,7 +631,9 @@ class TestMetafuncFunctional:
metafunc.addcall(param=10) metafunc.addcall(param=10)
metafunc.addcall(param=20) metafunc.addcall(param=20)
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
return request.param return request.param
def test_func1(arg1): def test_func1(arg1):
@ -669,9 +672,12 @@ class TestMetafuncFunctional:
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.addcall(param=(1,1), id="hello") metafunc.addcall(param=(1,1), id="hello")
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
return request.param[0] return request.param[0]
def pytest_funcarg__arg2(request): @pytest.fixture
def arg2(request):
return request.param[1] return request.param[1]
class TestClass: class TestClass:
@ -755,11 +761,14 @@ class TestMetafuncFunctional:
metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg1", [1], indirect=True)
metafunc.parametrize("arg2", [10], indirect=True) metafunc.parametrize("arg2", [10], indirect=True)
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
x = request.getfixturevalue("arg2") x = request.getfixturevalue("arg2")
return x + request.param return x + request.param
def pytest_funcarg__arg2(request): @pytest.fixture
def arg2(request):
return request.param return request.param
def test_func1(arg1, arg2): def test_func1(arg1, arg2):
@ -777,10 +786,13 @@ class TestMetafuncFunctional:
assert "arg1" in metafunc.fixturenames assert "arg1" in metafunc.fixturenames
metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg1", [1], indirect=True)
def pytest_funcarg__arg1(request): import pytest
@pytest.fixture
def arg1(request):
return request.param return request.param
def pytest_funcarg__arg2(request, arg1): @pytest.fixture
def arg2(request, arg1):
return 10 * arg1 return 10 * arg1
def test_func(arg2): def test_func(arg2):
@ -870,7 +882,8 @@ class TestMetafuncFunctional:
if "arg" in metafunc.funcargnames: if "arg" in metafunc.funcargnames:
metafunc.parametrize("arg", [1,2], indirect=True, metafunc.parametrize("arg", [1,2], indirect=True,
scope=%r) scope=%r)
def pytest_funcarg__arg(request): @pytest.fixture
def arg(request):
l.append(request.param) l.append(request.param)
return request.param return request.param
def test_hello(arg): def test_hello(arg):

View File

@ -1,274 +0,0 @@
"PYTEST_DONT_REWRITE"
import py
import pytest
from _pytest.assertion import util
def exvalue():
return py.std.sys.exc_info()[1]
def f():
return 2
def test_not_being_rewritten():
assert "@py_builtins" not in globals()
def test_assert():
try:
assert f() == 3
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith('assert 2 == 3\n')
def test_assert_with_explicit_message():
try:
assert f() == 3, "hello"
except AssertionError:
e = exvalue()
assert e.msg == 'hello'
def test_assert_within_finally():
excinfo = pytest.raises(ZeroDivisionError, """
try:
1/0
finally:
i = 42
""")
s = excinfo.exconly()
assert py.std.re.search("division.+by zero", s) is not None
#def g():
# A.f()
#excinfo = getexcinfo(TypeError, g)
#msg = getmsg(excinfo)
#assert msg.find("must be called with A") != -1
def test_assert_multiline_1():
try:
assert (f() ==
3)
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith('assert 2 == 3\n')
def test_assert_multiline_2():
try:
assert (f() == (4,
3)[-1])
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith('assert 2 ==')
def test_in():
try:
assert "hi" in [1, 2]
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 'hi' in")
def test_is():
try:
assert 1 is 2
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 1 is 2")
def test_attrib():
class Foo(object):
b = 1
i = Foo()
try:
assert i.b == 2
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 1 == 2")
def test_attrib_inst():
class Foo(object):
b = 1
try:
assert Foo().b == 2
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 1 == 2")
def test_len():
l = list(range(42))
try:
assert len(l) == 100
except AssertionError:
e = exvalue()
s = str(e)
assert s.startswith("assert 42 == 100")
assert "where 42 = len([" in s
def test_assert_non_string_message():
class A:
def __str__(self):
return "hello"
try:
assert 0 == 1, A()
except AssertionError:
e = exvalue()
assert e.msg == "hello"
def test_assert_keyword_arg():
def f(x=3):
return False
try:
assert f(x=5)
except AssertionError:
e = exvalue()
assert "x=5" in e.msg
def test_private_class_variable():
class X:
def __init__(self):
self.__v = 41
def m(self):
assert self.__v == 42
try:
X().m()
except AssertionError:
e = exvalue()
assert "== 42" in e.msg
# These tests should both fail, but should fail nicely...
class WeirdRepr:
def __repr__(self):
return '<WeirdRepr\nsecond line>'
def bug_test_assert_repr():
v = WeirdRepr()
try:
assert v == 1
except AssertionError:
e = exvalue()
assert e.msg.find('WeirdRepr') != -1
assert e.msg.find('second line') != -1
assert 0
def test_assert_non_string():
try:
assert 0, ['list']
except AssertionError:
e = exvalue()
assert e.msg.find("list") != -1
def test_assert_implicit_multiline():
try:
x = [1,2,3]
assert x != [1,
2, 3]
except AssertionError:
e = exvalue()
assert e.msg.find('assert [1, 2, 3] !=') != -1
def test_assert_with_brokenrepr_arg():
class BrokenRepr:
def __repr__(self): 0 / 0
e = AssertionError(BrokenRepr())
if e.msg.find("broken __repr__") == -1:
pytest.fail("broken __repr__ not handle correctly")
def test_multiple_statements_per_line():
try:
a = 1; assert a == 2
except AssertionError:
e = exvalue()
assert "assert 1 == 2" in e.msg
def test_power():
try:
assert 2**3 == 7
except AssertionError:
e = exvalue()
assert "assert (2 ** 3) == 7" in e.msg
def test_assert_customizable_reprcompare(monkeypatch):
monkeypatch.setattr(util, '_reprcompare', lambda *args: 'hello')
try:
assert 3 == 4
except AssertionError:
e = exvalue()
s = str(e)
assert "hello" in s
def test_assert_long_source_1():
try:
assert len == [
(None, ['somet text', 'more text']),
]
except AssertionError:
e = exvalue()
s = str(e)
assert 're-run' not in s
assert 'somet text' in s
def test_assert_long_source_2():
try:
assert(len == [
(None, ['somet text', 'more text']),
])
except AssertionError:
e = exvalue()
s = str(e)
assert 're-run' not in s
assert 'somet text' in s
def test_assert_raise_alias(testdir):
testdir.makepyfile("""
"PYTEST_DONT_REWRITE"
import sys
EX = AssertionError
def test_hello():
raise EX("hello"
"multi"
"line")
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"*def test_hello*",
"*raise EX*",
"*1 failed*",
])
def test_assert_raise_subclass():
class SomeEx(AssertionError):
def __init__(self, *args):
super(SomeEx, self).__init__()
try:
raise SomeEx("hello")
except AssertionError:
s = str(exvalue())
assert 're-run' not in s
assert 'could not determine' in s
def test_assert_raises_in_nonzero_of_object_pytest_issue10():
class A(object):
def __nonzero__(self):
raise ValueError(42)
def __lt__(self, other):
return A()
def __repr__(self):
return "<MY42 object>"
def myany(x):
return True
try:
assert not(myany(A() < 0))
except AssertionError:
e = exvalue()
s = str(e)
assert "<MY42 object> < 0" in s

View File

@ -3,10 +3,8 @@ import sys
import textwrap import textwrap
import _pytest.assertion as plugin import _pytest.assertion as plugin
import _pytest._code
import py import py
import pytest import pytest
from _pytest.assertion import reinterpret
from _pytest.assertion import util from _pytest.assertion import util
PY3 = sys.version_info >= (3, 0) PY3 = sys.version_info >= (3, 0)
@ -23,24 +21,200 @@ def mock_config():
return Config() return Config()
def interpret(expr): class TestImportHookInstallation:
return reinterpret.reinterpret(expr, _pytest._code.Frame(sys._getframe(1)))
@pytest.mark.parametrize('initial_conftest', [True, False])
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
def test_conftest_assertion_rewrite(self, testdir, initial_conftest, mode):
"""Test that conftest files are using assertion rewrite on import.
(#1619)
"""
testdir.tmpdir.join('foo/tests').ensure(dir=1)
conftest_path = 'conftest.py' if initial_conftest else 'foo/conftest.py'
contents = {
conftest_path: """
import pytest
@pytest.fixture
def check_first():
def check(values, value):
assert values.pop(0) == value
return check
""",
'foo/tests/test_foo.py': """
def test(check_first):
check_first([10, 30], 30)
"""
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess('--assert=%s' % mode)
if mode == 'plain':
expected = 'E AssertionError'
elif mode == 'rewrite':
expected = '*assert 10 == 30*'
else:
assert 0
result.stdout.fnmatch_lines([expected])
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
def test_pytest_plugins_rewrite(self, testdir, mode):
contents = {
'conftest.py': """
pytest_plugins = ['ham']
""",
'ham.py': """
import pytest
@pytest.fixture
def check_first():
def check(values, value):
assert values.pop(0) == value
return check
""",
'test_foo.py': """
def test_foo(check_first):
check_first([10, 30], 30)
""",
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess('--assert=%s' % mode)
if mode == 'plain':
expected = 'E AssertionError'
elif mode == 'rewrite':
expected = '*assert 10 == 30*'
else:
assert 0
result.stdout.fnmatch_lines([expected])
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
def test_installed_plugin_rewrite(self, testdir, mode):
# Make sure the hook is installed early enough so that plugins
# installed via setuptools are re-written.
testdir.tmpdir.join('hampkg').ensure(dir=1)
contents = {
'hampkg/__init__.py': """
import pytest
@pytest.fixture
def check_first2():
def check(values, value):
assert values.pop(0) == value
return check
""",
'spamplugin.py': """
import pytest
from hampkg import check_first2
@pytest.fixture
def check_first():
def check(values, value):
assert values.pop(0) == value
return check
""",
'mainwrapper.py': """
import pytest, pkg_resources
class DummyDistInfo:
project_name = 'spam'
version = '1.0'
def _get_metadata(self, name):
return ['spamplugin.py,sha256=abc,123',
'hampkg/__init__.py,sha256=abc,123']
class DummyEntryPoint:
name = 'spam'
module_name = 'spam.py'
attrs = ()
extras = None
dist = DummyDistInfo()
def load(self, require=True, *args, **kwargs):
import spamplugin
return spamplugin
def iter_entry_points(name):
yield DummyEntryPoint()
pkg_resources.iter_entry_points = iter_entry_points
pytest.main()
""",
'test_foo.py': """
def test(check_first):
check_first([10, 30], 30)
def test2(check_first2):
check_first([10, 30], 30)
""",
}
testdir.makepyfile(**contents)
result = testdir.run(sys.executable, 'mainwrapper.py', '-s', '--assert=%s' % mode)
if mode == 'plain':
expected = 'E AssertionError'
elif mode == 'rewrite':
expected = '*assert 10 == 30*'
else:
assert 0
result.stdout.fnmatch_lines([expected])
def test_rewrite_ast(self, testdir):
testdir.tmpdir.join('pkg').ensure(dir=1)
contents = {
'pkg/__init__.py': """
import pytest
pytest.register_assert_rewrite('pkg.helper')
""",
'pkg/helper.py': """
def tool():
a, b = 2, 3
assert a == b
""",
'pkg/plugin.py': """
import pytest, pkg.helper
@pytest.fixture
def tool():
return pkg.helper.tool
""",
'pkg/other.py': """
l = [3, 2]
def tool():
assert l.pop() == 3
""",
'conftest.py': """
pytest_plugins = ['pkg.plugin']
""",
'test_pkg.py': """
import pkg.other
def test_tool(tool):
tool()
def test_other():
pkg.other.tool()
""",
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess('--assert=rewrite')
result.stdout.fnmatch_lines(['>*assert a == b*',
'E*assert 2 == 3*',
'>*assert l.pop() == 3*',
'E*AssertionError'])
class TestBinReprIntegration: class TestBinReprIntegration:
def test_pytest_assertrepr_compare_called(self, testdir): def test_pytest_assertrepr_compare_called(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest
l = [] l = []
def pytest_assertrepr_compare(op, left, right): def pytest_assertrepr_compare(op, left, right):
l.append((op, left, right)) l.append((op, left, right))
def pytest_funcarg__l(request):
@pytest.fixture
def list(request):
return l return l
""") """)
testdir.makepyfile(""" testdir.makepyfile("""
def test_hello(): def test_hello():
assert 0 == 1 assert 0 == 1
def test_check(l): def test_check(list):
assert l == [("==", 0, 1)] assert list == [("==", 0, 1)]
""") """)
result = testdir.runpytest("-v") result = testdir.runpytest("-v")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
@ -477,14 +651,6 @@ def test_assertion_options(testdir):
result = testdir.runpytest_subprocess("--assert=plain") result = testdir.runpytest_subprocess("--assert=plain")
assert "3 == 4" not in result.stdout.str() assert "3 == 4" not in result.stdout.str()
def test_old_assert_mode(testdir):
testdir.makepyfile("""
def test_in_old_mode():
assert "@py_builtins" not in globals()
""")
result = testdir.runpytest_subprocess("--assert=reinterp")
assert result.ret == 0
def test_triple_quoted_string_issue113(testdir): def test_triple_quoted_string_issue113(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def test_hello(): def test_hello():

View File

@ -12,7 +12,7 @@ if sys.platform.startswith("java"):
import _pytest._code import _pytest._code
from _pytest.assertion import util from _pytest.assertion import util
from _pytest.assertion.rewrite import rewrite_asserts, PYTEST_TAG from _pytest.assertion.rewrite import rewrite_asserts, PYTEST_TAG, AssertionRewritingHook
from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_NOTESTSCOLLECTED
@ -524,6 +524,16 @@ def test_rewritten():
testdir.makepyfile("import a_package_without_init_py.module") testdir.makepyfile("import a_package_without_init_py.module")
assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED
def test_rewrite_warning(self, pytestconfig, monkeypatch):
hook = AssertionRewritingHook(pytestconfig)
warnings = []
def mywarn(code, msg):
warnings.append((code, msg))
monkeypatch.setattr(hook.config, 'warn', mywarn)
hook.mark_rewrite('_pytest')
assert '_pytest' in warnings[0][1]
class TestAssertionRewriteHookDetails(object): class TestAssertionRewriteHookDetails(object):
def test_loader_is_package_false_for_module(self, testdir): def test_loader_is_package_false_for_module(self, testdir):
testdir.makepyfile(test_fun=""" testdir.makepyfile(test_fun="""
@ -704,40 +714,6 @@ class TestAssertionRewriteHookDetails(object):
result = testdir.runpytest() result = testdir.runpytest()
result.stdout.fnmatch_lines('*1 passed*') result.stdout.fnmatch_lines('*1 passed*')
@pytest.mark.parametrize('initial_conftest', [True, False])
@pytest.mark.parametrize('mode', ['plain', 'rewrite', 'reinterp'])
def test_conftest_assertion_rewrite(self, testdir, initial_conftest, mode):
"""Test that conftest files are using assertion rewrite on import.
(#1619)
"""
testdir.tmpdir.join('foo/tests').ensure(dir=1)
conftest_path = 'conftest.py' if initial_conftest else 'foo/conftest.py'
contents = {
conftest_path: """
import pytest
@pytest.fixture
def check_first():
def check(values, value):
assert values.pop(0) == value
return check
""",
'foo/tests/test_foo.py': """
def test(check_first):
check_first([10, 30], 30)
"""
}
testdir.makepyfile(**contents)
result = testdir.runpytest_subprocess('--assert=%s' % mode)
if mode == 'plain':
expected = 'E AssertionError'
elif mode == 'rewrite':
expected = '*assert 10 == 30*'
elif mode == 'reinterp':
expected = '*AssertionError:*was re-run*'
else:
assert 0
result.stdout.fnmatch_lines([expected])
def test_issue731(testdir): def test_issue731(testdir):
testdir.makepyfile(""" testdir.makepyfile("""

View File

@ -373,10 +373,14 @@ def test_preparse_ordering_with_setuptools(testdir, monkeypatch):
pkg_resources = pytest.importorskip("pkg_resources") pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name): def my_iter(name):
assert name == "pytest11" assert name == "pytest11"
class Dist:
project_name = 'spam'
version = '1.0'
def _get_metadata(self, name):
return ['foo.txt,sha256=abc,123']
class EntryPoint: class EntryPoint:
name = "mytestplugin" name = "mytestplugin"
class dist: dist = Dist()
pass
def load(self): def load(self):
class PseudoPlugin: class PseudoPlugin:
x = 42 x = 42
@ -396,9 +400,14 @@ def test_setuptools_importerror_issue1479(testdir, monkeypatch):
pkg_resources = pytest.importorskip("pkg_resources") pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name): def my_iter(name):
assert name == "pytest11" assert name == "pytest11"
class Dist:
project_name = 'spam'
version = '1.0'
def _get_metadata(self, name):
return ['foo.txt,sha256=abc,123']
class EntryPoint: class EntryPoint:
name = "mytestplugin" name = "mytestplugin"
dist = None dist = Dist()
def load(self): def load(self):
raise ImportError("Don't hide me!") raise ImportError("Don't hide me!")
return iter([EntryPoint()]) return iter([EntryPoint()])
@ -412,8 +421,14 @@ def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch):
pkg_resources = pytest.importorskip("pkg_resources") pkg_resources = pytest.importorskip("pkg_resources")
def my_iter(name): def my_iter(name):
assert name == "pytest11" assert name == "pytest11"
class Dist:
project_name = 'spam'
version = '1.0'
def _get_metadata(self, name):
return ['foo.txt,sha256=abc,123']
class EntryPoint: class EntryPoint:
name = "mytestplugin" name = "mytestplugin"
dist = Dist()
def load(self): def load(self):
assert 0, "should not arrive here" assert 0, "should not arrive here"
return iter([EntryPoint()]) return iter([EntryPoint()])
@ -505,7 +520,6 @@ def test_load_initial_conftest_last_ordering(testdir):
expected = [ expected = [
"_pytest.config", "_pytest.config",
'test_config', 'test_config',
'_pytest.assertion',
'_pytest.capture', '_pytest.capture',
] ]
assert [x.function.__module__ for x in l] == expected assert [x.function.__module__ for x in l] == expected
@ -539,11 +553,11 @@ class TestWarning:
def test_hello(fix): def test_hello(fix):
pass pass
""") """)
result = testdir.runpytest() result = testdir.runpytest("--disable-pytest-warnings")
assert result.parseoutcomes()["pytest-warnings"] > 0 assert result.parseoutcomes()["pytest-warnings"] > 0
assert "hello" not in result.stdout.str() assert "hello" not in result.stdout.str()
result = testdir.runpytest("-rw") result = testdir.runpytest()
result.stdout.fnmatch_lines(""" result.stdout.fnmatch_lines("""
===*pytest-warning summary*=== ===*pytest-warning summary*===
*WT1*test_warn_on_test_item*:5*hello* *WT1*test_warn_on_test_item*:5*hello*

View File

@ -203,6 +203,7 @@ def test_conftest_import_order(testdir, monkeypatch):
def impct(p): def impct(p):
return p return p
conftest = PytestPluginManager() conftest = PytestPluginManager()
conftest._confcutdir = testdir.tmpdir
monkeypatch.setattr(conftest, '_importconftest', impct) monkeypatch.setattr(conftest, '_importconftest', impct)
assert conftest._getconftestmodules(sub) == [ct1, ct2] assert conftest._getconftestmodules(sub) == [ct1, ct2]
@ -407,3 +408,16 @@ def test_issue1073_conftest_special_objects(testdir):
""") """)
res = testdir.runpytest() res = testdir.runpytest()
assert res.ret == 0 assert res.ret == 0
def test_conftest_exception_handling(testdir):
testdir.makeconftest('''
raise ValueError()
''')
testdir.makepyfile("""
def test_some():
pass
""")
res = testdir.runpytest()
assert res.ret == 4
assert 'raise ValueError()' in [line.strip() for line in res.errlines]

View File

@ -119,7 +119,10 @@ class TestPython:
def test_setup_error(self, testdir): def test_setup_error(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__arg(request): import pytest
@pytest.fixture
def arg(request):
raise ValueError() raise ValueError()
def test_function(arg): def test_function(arg):
pass pass
@ -131,7 +134,7 @@ class TestPython:
tnode = node.find_first_by_tag("testcase") tnode = node.find_first_by_tag("testcase")
tnode.assert_attr( tnode.assert_attr(
file="test_setup_error.py", file="test_setup_error.py",
line="2", line="5",
classname="test_setup_error", classname="test_setup_error",
name="test_function") name="test_function")
fnode = tnode.find_first_by_tag("error") fnode = tnode.find_first_by_tag("error")
@ -444,7 +447,10 @@ class TestPython:
def test_setup_error_captures_stdout(self, testdir): def test_setup_error_captures_stdout(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__arg(request): import pytest
@pytest.fixture
def arg(request):
print('hello-stdout') print('hello-stdout')
raise ValueError() raise ValueError()
def test_function(arg): def test_function(arg):
@ -459,7 +465,10 @@ class TestPython:
def test_setup_error_captures_stderr(self, testdir): def test_setup_error_captures_stderr(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import sys import sys
def pytest_funcarg__arg(request): import pytest
@pytest.fixture
def arg(request):
sys.stderr.write('hello-stderr') sys.stderr.write('hello-stderr')
raise ValueError() raise ValueError()
def test_function(arg): def test_function(arg):

View File

@ -481,7 +481,8 @@ class TestFunctional:
def test_mark_dynamically_in_funcarg(self, testdir): def test_mark_dynamically_in_funcarg(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
def pytest_funcarg__arg(request): @pytest.fixture
def arg(request):
request.applymarker(pytest.mark.hello) request.applymarker(pytest.mark.hello)
def pytest_terminal_summary(terminalreporter): def pytest_terminal_summary(terminalreporter):
l = terminalreporter.stats['passed'] l = terminalreporter.stats['passed']

View File

@ -3,10 +3,11 @@ import sys
import textwrap import textwrap
import pytest import pytest
from _pytest.monkeypatch import monkeypatch as MonkeyPatch from _pytest.monkeypatch import MonkeyPatch
def pytest_funcarg__mp(request): @pytest.fixture
def mp(request):
cwd = os.getcwd() cwd = os.getcwd()
sys_path = list(sys.path) sys_path = list(sys.path)
@ -205,7 +206,7 @@ def test_setenv_prepend():
def test_monkeypatch_plugin(testdir): def test_monkeypatch_plugin(testdir):
reprec = testdir.inline_runsource(""" reprec = testdir.inline_runsource("""
def test_method(monkeypatch): def test_method(monkeypatch):
assert monkeypatch.__class__.__name__ == "monkeypatch" assert monkeypatch.__class__.__name__ == "MonkeyPatch"
""") """)
res = reprec.countoutcomes() res = reprec.countoutcomes()
assert tuple(res) == (1, 0, 0), res assert tuple(res) == (1, 0, 0), res

View File

@ -1,6 +1,7 @@
import sys import sys
import _pytest._code import _pytest._code
import pytest
def runpdb_and_get_report(testdir, source): def runpdb_and_get_report(testdir, source):
@ -12,7 +13,9 @@ def runpdb_and_get_report(testdir, source):
class TestPDB: class TestPDB:
def pytest_funcarg__pdblist(self, request):
@pytest.fixture
def pdblist(self, request):
monkeypatch = request.getfixturevalue("monkeypatch") monkeypatch = request.getfixturevalue("monkeypatch")
pdblist = [] pdblist = []
def mypdb(*args): def mypdb(*args):
@ -311,3 +314,28 @@ class TestPDB:
child.sendeof() child.sendeof()
if child.isalive(): if child.isalive():
child.wait() child.wait()
def test_pdb_custom_cls(self, testdir):
called = []
# install dummy debugger class and track which methods were called on it
class _CustomPdb:
def __init__(self, *args, **kwargs):
called.append("init")
def reset(self):
called.append("reset")
def interaction(self, *args):
called.append("interaction")
_pytest._CustomPdb = _CustomPdb
p1 = testdir.makepyfile("""xxx """)
result = testdir.runpytest_inprocess(
"--pdbcls=_pytest:_CustomPdb", p1)
result.stdout.fnmatch_lines([
"*NameError*xxx*",
"*1 error*",
])
assert called == ["init", "reset", "interaction"]

View File

@ -229,11 +229,12 @@ class BaseFunctionalTests:
assert reps[5].failed assert reps[5].failed
def test_exact_teardown_issue1206(self, testdir): def test_exact_teardown_issue1206(self, testdir):
"""issue shadowing error with wrong number of arguments on teardown_method."""
rec = testdir.inline_runsource(""" rec = testdir.inline_runsource("""
import pytest import pytest
class TestClass: class TestClass:
def teardown_method(self): def teardown_method(self, x, y, z):
pass pass
def test_method(self): def test_method(self):
@ -256,9 +257,9 @@ class BaseFunctionalTests:
assert reps[2].when == "teardown" assert reps[2].when == "teardown"
assert reps[2].longrepr.reprcrash.message in ( assert reps[2].longrepr.reprcrash.message in (
# python3 error # python3 error
'TypeError: teardown_method() takes 1 positional argument but 2 were given', "TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
# python2 error # python2 error
'TypeError: teardown_method() takes exactly 1 argument (2 given)' 'TypeError: teardown_method() takes exactly 4 arguments (2 given)'
) )
def test_failure_in_setup_function_ignores_custom_repr(self, testdir): def test_failure_in_setup_function_ignores_custom_repr(self, testdir):
@ -399,13 +400,15 @@ def test_callinfo():
@pytest.mark.xfail @pytest.mark.xfail
def test_runtest_in_module_ordering(testdir): def test_runtest_in_module_ordering(testdir):
p1 = testdir.makepyfile(""" p1 = testdir.makepyfile("""
import pytest
def pytest_runtest_setup(item): # runs after class-level! def pytest_runtest_setup(item): # runs after class-level!
item.function.mylist.append("module") item.function.mylist.append("module")
class TestClass: class TestClass:
def pytest_runtest_setup(self, item): def pytest_runtest_setup(self, item):
assert not hasattr(item.function, 'mylist') assert not hasattr(item.function, 'mylist')
item.function.mylist = ['class'] item.function.mylist = ['class']
def pytest_funcarg__mylist(self, request): @pytest.fixture
def mylist(self, request):
return request.function.mylist return request.function.mylist
def pytest_runtest_call(self, item, __multicall__): def pytest_runtest_call(self, item, __multicall__):
try: try:

View File

@ -1,6 +1,8 @@
# #
# test correct setup/teardowns at # test correct setup/teardowns at
# module, class, and instance level # module, class, and instance level
import pytest
def test_module_and_function_setup(testdir): def test_module_and_function_setup(testdir):
reprec = testdir.inline_runsource(""" reprec = testdir.inline_runsource("""
@ -234,7 +236,8 @@ def test_setup_funcarg_setup_when_outer_scope_fails(testdir):
import pytest import pytest
def setup_module(mod): def setup_module(mod):
raise ValueError(42) raise ValueError(42)
def pytest_funcarg__hello(request): @pytest.fixture
def hello(request):
raise ValueError("xyz43") raise ValueError("xyz43")
def test_function1(hello): def test_function1(hello):
pass pass
@ -250,3 +253,53 @@ def test_setup_funcarg_setup_when_outer_scope_fails(testdir):
"*2 error*" "*2 error*"
]) ])
assert "xyz43" not in result.stdout.str() assert "xyz43" not in result.stdout.str()
@pytest.mark.parametrize('arg', ['', 'arg'])
def test_setup_teardown_function_level_with_optional_argument(testdir, monkeypatch, arg):
"""parameter to setup/teardown xunit-style functions parameter is now optional (#1728)."""
import sys
trace_setups_teardowns = []
monkeypatch.setattr(sys, 'trace_setups_teardowns', trace_setups_teardowns, raising=False)
p = testdir.makepyfile("""
import pytest
import sys
trace = sys.trace_setups_teardowns.append
def setup_module({arg}): trace('setup_module')
def teardown_module({arg}): trace('teardown_module')
def setup_function({arg}): trace('setup_function')
def teardown_function({arg}): trace('teardown_function')
def test_function_1(): pass
def test_function_2(): pass
class Test:
def setup_method(self, {arg}): trace('setup_method')
def teardown_method(self, {arg}): trace('teardown_method')
def test_method_1(self): pass
def test_method_2(self): pass
""".format(arg=arg))
result = testdir.inline_run(p)
result.assertoutcome(passed=4)
expected = [
'setup_module',
'setup_function',
'teardown_function',
'setup_function',
'teardown_function',
'setup_method',
'teardown_method',
'setup_method',
'teardown_method',
'teardown_module',
]
assert trace_setups_teardowns == expected

View File

@ -197,6 +197,14 @@ class TestNewSession(SessionTests):
colfail = [x for x in finished if x.failed] colfail = [x for x in finished if x.failed]
assert len(colfail) == 1 assert len(colfail) == 1
def test_minus_x_overriden_by_maxfail(self, testdir):
testdir.makepyfile(__init__="")
testdir.makepyfile(test_one="xxxx", test_two="yyyy", test_third="zzz")
reprec = testdir.inline_run("-x", "--maxfail=2", testdir.tmpdir)
finished = reprec.getreports("pytest_collectreport")
colfail = [x for x in finished if x.failed]
assert len(colfail) == 2
def test_plugin_specify(testdir): def test_plugin_specify(testdir):
pytest.raises(ImportError, """ pytest.raises(ImportError, """

View File

@ -309,7 +309,8 @@ class TestXFail:
def test_dynamic_xfail_no_run(self, testdir): def test_dynamic_xfail_no_run(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
import pytest import pytest
def pytest_funcarg__arg(request): @pytest.fixture
def arg(request):
request.applymarker(pytest.mark.xfail(run=False)) request.applymarker(pytest.mark.xfail(run=False))
def test_this(arg): def test_this(arg):
assert 0 assert 0
@ -323,7 +324,8 @@ class TestXFail:
def test_dynamic_xfail_set_during_funcarg_setup(self, testdir): def test_dynamic_xfail_set_during_funcarg_setup(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
import pytest import pytest
def pytest_funcarg__arg(request): @pytest.fixture
def arg(request):
request.applymarker(pytest.mark.xfail) request.applymarker(pytest.mark.xfail)
def test_this2(arg): def test_this2(arg):
assert 0 assert 0

View File

@ -590,17 +590,30 @@ def test_getreportopt():
class config: class config:
class option: class option:
reportchars = "" reportchars = ""
disablepytestwarnings = True
config.option.reportchars = "sf" config.option.reportchars = "sf"
assert getreportopt(config) == "sf" assert getreportopt(config) == "sf"
config.option.reportchars = "sfx" config.option.reportchars = "sfxw"
assert getreportopt(config) == "sfx" assert getreportopt(config) == "sfx"
config.option.reportchars = "sfx"
config.option.disablepytestwarnings = False
assert getreportopt(config) == "sfxw"
config.option.reportchars = "sfxw"
config.option.disablepytestwarnings = False
assert getreportopt(config) == "sfxw"
def test_terminalreporter_reportopt_addopts(testdir): def test_terminalreporter_reportopt_addopts(testdir):
testdir.makeini("[pytest]\naddopts=-rs") testdir.makeini("[pytest]\naddopts=-rs")
testdir.makepyfile(""" testdir.makepyfile("""
def pytest_funcarg__tr(request): import pytest
@pytest.fixture
def tr(request):
tr = request.config.pluginmanager.getplugin("terminalreporter") tr = request.config.pluginmanager.getplugin("terminalreporter")
return tr return tr
def test_opt(tr): def test_opt(tr):
@ -614,7 +627,10 @@ def test_terminalreporter_reportopt_addopts(testdir):
def test_tbstyle_short(testdir): def test_tbstyle_short(testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
def pytest_funcarg__arg(request): import pytest
@pytest.fixture
def arg(request):
return 42 return 42
def test_opt(arg): def test_opt(arg):
x = 0 x = 0
@ -625,7 +641,7 @@ def test_tbstyle_short(testdir):
assert 'arg = 42' not in s assert 'arg = 42' not in s
assert 'x = 0' not in s assert 'x = 0' not in s
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*%s:5*" % p.basename, "*%s:8*" % p.basename,
" assert x", " assert x",
"E assert*", "E assert*",
]) ])

View File

@ -265,8 +265,8 @@ def test_testcase_custom_exception_info(testdir, type):
def run(self, result): def run(self, result):
excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0) excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0)
# we fake an incompatible exception info # we fake an incompatible exception info
from _pytest.monkeypatch import monkeypatch from _pytest.monkeypatch import MonkeyPatch
mp = monkeypatch() mp = MonkeyPatch()
def t(*args): def t(*args):
mp.undo() mp.undo()
raise TypeError() raise TypeError()