2010-11-02 07:53:53 +08:00
|
|
|
Some Issues and Questions
|
2009-08-19 01:04:57 +08:00
|
|
|
==================================
|
2009-09-08 15:57:19 +08:00
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
.. note::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
This FAQ is here only mostly for historic reasons. Checkout
|
2014-01-18 19:31:33 +08:00
|
|
|
`pytest Q&A at Stackoverflow <http://stackoverflow.com/search?q=pytest>`_
|
2013-10-01 18:37:11 +08:00
|
|
|
for many questions and answers related to pytest and/or use
|
|
|
|
:ref:`contact channels` to get help.
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
On naming, nosetests, licensing and magic
|
2010-10-11 05:45:45 +08:00
|
|
|
------------------------------------------------
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
How does pytest relate to nose and unittest?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` and nose_ share basic philosophy when it comes
|
2011-03-04 06:40:38 +08:00
|
|
|
to running and writing Python tests. In fact, you can run many tests
|
2014-01-18 19:31:33 +08:00
|
|
|
written for nose with ``pytest``. nose_ was originally created
|
|
|
|
as a clone of ``pytest`` when ``pytest`` was in the ``0.8`` release
|
2011-03-04 06:40:38 +08:00
|
|
|
cycle. Note that starting with pytest-2.0 support for running unittest
|
2012-06-17 16:59:30 +08:00
|
|
|
test suites is majorly improved.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
how does pytest relate to twisted's trial?
|
2012-06-17 16:59:30 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Since some time ``pytest`` has builtin support for supporting tests
|
2012-06-17 16:59:30 +08:00
|
|
|
written using trial. It does not itself start a reactor, however,
|
2014-01-18 19:31:33 +08:00
|
|
|
and does not handle Deferreds returned from a test in pytest style.
|
2012-10-23 01:22:01 +08:00
|
|
|
If you are using trial's unittest.TestCase chances are that you can
|
|
|
|
just run your tests even if you return Deferreds. In addition,
|
|
|
|
there also is a dedicated `pytest-twisted
|
2015-02-11 15:52:48 +08:00
|
|
|
<http://pypi.python.org/pypi/pytest-twisted>`_ plugin which allows you to
|
|
|
|
return deferreds from pytest-style tests, allowing the use of
|
2012-10-23 01:22:01 +08:00
|
|
|
:ref:`fixtures` and other features.
|
2014-01-18 19:31:33 +08:00
|
|
|
|
|
|
|
how does pytest work with Django?
|
2012-06-17 16:59:30 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2012-06-17 16:59:30 +08:00
|
|
|
In 2012, some work is going into the `pytest-django plugin <http://pypi.python.org/pypi/pytest-django>`_. It substitutes the usage of Django's
|
2015-02-11 15:52:48 +08:00
|
|
|
``manage.py test`` and allows the use of all pytest features_ most of which
|
2012-06-17 16:59:30 +08:00
|
|
|
are not available from Django directly.
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2012-07-02 19:13:48 +08:00
|
|
|
.. _features: features.html
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
What's this "magic" with pytest? (historic notes)
|
2012-06-17 16:59:30 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Around 2007 (version ``0.8``) some people thought that ``pytest``
|
2012-06-17 16:59:30 +08:00
|
|
|
was using too much "magic". It had been part of the `pylib`_ which
|
2015-02-11 15:52:48 +08:00
|
|
|
contains a lot of unrelated python library code. Around 2010 there
|
2014-01-18 19:31:33 +08:00
|
|
|
was a major cleanup refactoring, which removed unused or deprecated code
|
2012-06-17 16:59:30 +08:00
|
|
|
and resulted in the new ``pytest`` PyPI package which strictly contains
|
2014-01-18 19:31:33 +08:00
|
|
|
only test-related code. This release also brought a complete pluginification
|
2012-06-17 16:59:30 +08:00
|
|
|
such that the core is around 300 lines of code and everything else is
|
2014-01-18 19:31:33 +08:00
|
|
|
implemented in plugins. Thus ``pytest`` today is a small, universally runnable
|
2012-06-17 16:59:30 +08:00
|
|
|
and customizable testing framework for Python. Note, however, that
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` uses metaprogramming techniques and reading its source is
|
2012-06-17 16:59:30 +08:00
|
|
|
thus likely not something for Python beginners.
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
A second "magic" issue was the assert statement debugging feature.
|
2015-11-28 14:46:45 +08:00
|
|
|
Nowadays, ``pytest`` explicitly rewrites assert statements in test modules
|
2012-06-17 16:59:30 +08:00
|
|
|
in order to provide more useful :ref:`assert feedback <assertfeedback>`.
|
2014-01-18 19:31:33 +08:00
|
|
|
This completely avoids previous issues of confusing assertion-reporting.
|
2015-02-11 15:52:48 +08:00
|
|
|
It also means, that you can use Python's ``-O`` optimization without losing
|
2012-06-17 16:59:30 +08:00
|
|
|
assertions in test modules.
|
|
|
|
|
2015-12-17 02:14:36 +08:00
|
|
|
``pytest`` contains a second, mostly obsolete, assert debugging technique
|
|
|
|
invoked via ``--assert=reinterpret``: When an ``assert`` statement fails, ``pytest`` re-interprets
|
2012-06-18 17:47:57 +08:00
|
|
|
the expression part to show intermediate values. This technique suffers
|
|
|
|
from a caveat that the rewriting does not: If your expression has side
|
|
|
|
effects (better to avoid them anyway!) the intermediate values may not
|
|
|
|
be the same, confusing the reinterpreter and obfuscating the initial
|
2014-01-18 19:31:33 +08:00
|
|
|
error (this is also explained at the command line if it happens).
|
2012-06-17 16:59:30 +08:00
|
|
|
|
|
|
|
You can also turn off all assertion interaction using the
|
2015-12-03 04:39:32 +08:00
|
|
|
``--assert=plain`` option.
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
.. _`py namespaces`: index.html
|
2009-11-05 10:18:55 +08:00
|
|
|
.. _`py/__init__.py`: http://bitbucket.org/hpk42/py-trunk/src/trunk/py/__init__.py
|
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
Why can I use both ``pytest`` and ``py.test`` commands?
|
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
pytest used to be part of the py package, which provided several developer
|
|
|
|
utilities, all starting with ``py.<TAB>``, thus providing nice TAB-completion.
|
|
|
|
If you install ``pip install pycmd`` you get these tools from a separate
|
|
|
|
package. Once ``pytest`` became a separate package, the ``py.test`` name was
|
|
|
|
retained due to avoid a naming conflict with another tool. This conflict was
|
|
|
|
eventually resolved, and the ``pytest`` command was therefore introduced. In
|
|
|
|
future versions of pytest, we may deprecate and later remove the ``py.test``
|
|
|
|
command to avoid perpetuating the confusion.
|
2012-06-17 16:59:30 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
pytest fixtures, parametrized tests
|
2010-10-11 05:45:45 +08:00
|
|
|
-------------------------------------------------------
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2012-07-02 19:13:48 +08:00
|
|
|
.. _funcargs: funcargs.html
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
Is using pytest fixtures versus xUnit setup a style question?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
For simple applications and for people experienced with nose_ or
|
2011-03-04 06:40:38 +08:00
|
|
|
unittest-style test setup using `xUnit style setup`_ probably
|
2015-02-12 03:27:51 +08:00
|
|
|
feels natural. For larger test suites, parametrized testing
|
2015-07-19 01:15:07 +08:00
|
|
|
or setup of complex test resources using fixtures_ may feel more natural.
|
|
|
|
Moreover, fixtures are ideal for writing advanced test support
|
|
|
|
code (like e.g. the monkeypatch_, the tmpdir_ or capture_ fixtures)
|
2010-07-27 03:15:15 +08:00
|
|
|
because the support code can register setup/teardown functions
|
|
|
|
in a managed class/module/function scope.
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2012-07-02 19:13:48 +08:00
|
|
|
.. _monkeypatch: monkeypatch.html
|
|
|
|
.. _tmpdir: tmpdir.html
|
|
|
|
.. _capture: capture.html
|
2015-07-19 01:15:07 +08:00
|
|
|
.. _fixtures: fixture.html
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
.. _`why pytest_pyfuncarg__ methods?`:
|
|
|
|
|
|
|
|
.. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration
|
|
|
|
|
2015-12-24 21:56:54 +08:00
|
|
|
Can I yield multiple values from a fixture function?
|
2010-10-11 05:45:45 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-05-06 02:19:02 +08:00
|
|
|
There are two conceptual reasons why yielding from a factory function
|
2010-07-27 03:15:15 +08:00
|
|
|
is not possible:
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
* If multiple factories yielded values there would
|
2010-07-27 03:15:15 +08:00
|
|
|
be no natural place to determine the combination
|
2009-08-19 01:04:57 +08:00
|
|
|
policy - in real-world examples some combinations
|
2010-07-27 03:15:15 +08:00
|
|
|
often should not run.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2013-10-01 18:37:11 +08:00
|
|
|
* Calling factories for obtaining test function arguments
|
|
|
|
is part of setting up and running a test. At that
|
|
|
|
point it is not possible to add new test calls to
|
|
|
|
the test collection anymore.
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
However, with pytest-2.3 you can use the :ref:`@pytest.fixture` decorator
|
2012-08-13 18:58:08 +08:00
|
|
|
and specify ``params`` so that all tests depending on the factory-created
|
|
|
|
resource will run multiple times with different parameters.
|
|
|
|
|
2015-10-27 08:46:35 +08:00
|
|
|
You can also use the ``pytest_generate_tests`` hook to
|
|
|
|
implement the `parametrization scheme of your choice`_. See also
|
|
|
|
:ref:`paramexamples` for more examples.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
.. _`parametrization scheme of your choice`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest interaction with other packages
|
2010-10-11 05:45:45 +08:00
|
|
|
---------------------------------------------------
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Issues with pytest, multiprocess and setuptools?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2015-02-11 15:52:48 +08:00
|
|
|
On Windows the multiprocess package will instantiate sub processes
|
2011-02-17 21:46:40 +08:00
|
|
|
by pickling and thus implicitly re-import a lot of local modules.
|
|
|
|
Unfortunately, setuptools-0.6.11 does not ``if __name__=='__main__'``
|
2009-11-02 20:00:48 +08:00
|
|
|
protect its generated command line script. This leads to infinite
|
|
|
|
recursion when running a test that instantiates Processes.
|
|
|
|
|
2015-02-11 15:52:48 +08:00
|
|
|
As of mid-2013, there shouldn't be a problem anymore when you
|
2013-08-01 15:42:44 +08:00
|
|
|
use the standard setuptools (note that distribute has been merged
|
|
|
|
back into setuptools which is now shipped directly with virtualenv).
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2010-11-01 06:28:31 +08:00
|
|
|
.. include:: links.inc
|