2010-10-11 05:45:45 +08:00
|
|
|
Frequently asked Questions
|
2009-08-19 01:04:57 +08:00
|
|
|
==================================
|
2009-09-08 15:57:19 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
On naming, nosetests, licensing and magic XXX
|
|
|
|
------------------------------------------------
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
Why the ``py.test`` naming, why not ``pytest``?
|
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++
|
2010-05-06 02:19:02 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
XXX
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2009-11-05 10:18:55 +08:00
|
|
|
because of TAB-completion under Bash/Shells. If you hit
|
|
|
|
``py.<TAB>`` you'll get a list of available development
|
|
|
|
tools that all share the ``py.`` prefix. Another motivation
|
2010-07-27 03:15:15 +08:00
|
|
|
was to unify the package ("py.test") and tool filename.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
What's py.test's relation to ``nosetests``?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2009-08-19 23:12:02 +08:00
|
|
|
py.test and nose_ share basic philosophy when it comes
|
2010-07-27 03:15:15 +08:00
|
|
|
to running Python tests. In fact,
|
|
|
|
with py.test-1.1.0 it is ever easier to run many test suites
|
|
|
|
that currently work with ``nosetests``. nose_ was created
|
|
|
|
as a clone of ``py.test`` when py.test was in the ``0.8`` release
|
|
|
|
cycle so some of the newer features_ introduced with py.test-1.0
|
|
|
|
and py.test-1.1 have no counterpart in nose_.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
.. _nose: http://somethingaboutorange.com/mrl/projects/nose/0.11.1/
|
|
|
|
.. _features: test/features.html
|
2009-11-05 10:18:55 +08:00
|
|
|
.. _apipkg: http://pypi.python.org/pypi/apipkg
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
What's this "magic" with py.test?
|
2010-10-11 05:45:45 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++
|
2009-11-05 10:18:55 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
Around 2007 it was claimed that py.test was magic implementation
|
|
|
|
wise XXX. It has been refactored.
|
2010-07-27 03:15:15 +08:00
|
|
|
|
|
|
|
* when an ``assert`` statement fails, py.test re-interprets the expression
|
2009-11-05 10:18:55 +08:00
|
|
|
to show intermediate values if a test fails. If your expression
|
|
|
|
has side effects the intermediate values may not be the same, obfuscating
|
2010-07-27 03:15:15 +08:00
|
|
|
the initial error (this is also explained at the command line if it happens).
|
|
|
|
``py.test --no-assert`` turns off assert re-intepretation.
|
|
|
|
Sidenote: it is good practise to avoid asserts with side effects.
|
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
|
|
|
|
2009-11-05 10:18:55 +08:00
|
|
|
function arguments, parametrized tests and setup
|
2010-10-11 05:45:45 +08:00
|
|
|
-------------------------------------------------------
|
2009-11-05 10:18:55 +08:00
|
|
|
|
|
|
|
.. _funcargs: test/funcargs.html
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Is using funcarg- versus xUnit-based setup a style question?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
XXX
|
|
|
|
For simple applications and for people experienced with nose_ or
|
|
|
|
unittest-style test setup using `xUnit style setup`_
|
|
|
|
feels natural. For larger test suites, parametrized testing
|
2009-11-05 10:18:55 +08:00
|
|
|
or setup of complex test resources using funcargs_ is recommended.
|
2010-07-27 03:15:15 +08:00
|
|
|
Moreover, funcargs are ideal for writing advanced test support
|
|
|
|
code (like e.g. the monkeypatch_, the tmpdir_ or capture_ funcargs)
|
|
|
|
because the support code can register setup/teardown functions
|
|
|
|
in a managed class/module/function scope.
|
2009-11-05 10:18:55 +08:00
|
|
|
|
|
|
|
.. _monkeypatch: test/plugin/monkeypatch.html
|
|
|
|
.. _tmpdir: test/plugin/tmpdir.html
|
|
|
|
.. _capture: test/plugin/capture.html
|
|
|
|
.. _`xUnit style setup`: test/xunit_setup.html
|
|
|
|
.. _`pytest_nose`: test/plugin/nose.html
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
.. _`why pytest_pyfuncarg__ methods?`:
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Why the ``pytest_funcarg__*`` name for funcarg factories?
|
2010-10-11 05:45:45 +08:00
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
When experimenting with funcargs an explicit registration mechanism
|
|
|
|
was considered. But lacking a good use case for this indirection and
|
|
|
|
flexibility we decided to go for `Convention over Configuration`_ and
|
|
|
|
allow to directly specify the factory. Besides removing the need
|
2010-07-27 03:15:15 +08:00
|
|
|
for an indirection it allows to "grep" for ``pytest_funcarg__MYARG``
|
|
|
|
and will safely find all factory functions for the ``MYARG`` function
|
|
|
|
argument. It helps to alleviate the de-coupling of function
|
|
|
|
argument usage and creation.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
.. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Can I yield multiple values from a factory 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
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
* Calling factories for obtaining test function arguments
|
|
|
|
is part of setting up and running a test. At that
|
2009-08-19 01:04:57 +08:00
|
|
|
point it is not possible to add new test calls to
|
2010-07-27 03:15:15 +08:00
|
|
|
the test collection anymore.
|
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
|
|
|
|
|
|
|
Use the `pytest_generate_tests`_ hook to solve both issues
|
|
|
|
and implement the `parametrization scheme of your choice`_.
|
|
|
|
|
|
|
|
.. _`pytest_generate_tests`: test/funcargs.html#parametrizing-tests
|
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
|
|
|
|
|
|
|
|
|
|
|
py.test interaction with other packages
|
2010-10-11 05:45:45 +08:00
|
|
|
---------------------------------------------------
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Issues with py.test, multiprocess and setuptools?
|
2010-10-11 05:45:45 +08:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
On windows the multiprocess package will instantiate sub processes
|
|
|
|
by pickling and thus implicitely re-import a lot of local modules.
|
|
|
|
Unfortuantely, 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.
|
2010-07-27 03:15:15 +08:00
|
|
|
There are these workarounds:
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
* `install Distribute`_ as a drop-in replacement for setuptools
|
|
|
|
and install py.test
|
2009-11-02 20:00:48 +08:00
|
|
|
|
|
|
|
* `directly use a checkout`_ which avoids all setuptools/Distribute
|
2010-07-27 03:15:15 +08:00
|
|
|
installation
|
2009-11-02 20:00:48 +08:00
|
|
|
|
2009-11-02 21:52:54 +08:00
|
|
|
If those options are not available to you, you may also manually
|
|
|
|
fix the script that is created by setuptools by inserting an
|
2010-07-27 03:15:15 +08:00
|
|
|
``if __name__ == '__main__'``. Or you can create a "pytest.py"
|
2009-11-02 21:52:54 +08:00
|
|
|
script with this content and invoke that with the python version::
|
|
|
|
|
|
|
|
import py
|
|
|
|
if __name__ == '__main__':
|
|
|
|
py.cmdline.pytest()
|
|
|
|
|
2009-11-02 20:00:48 +08:00
|
|
|
.. _`directly use a checkout`: install.html#directly-use-a-checkout
|
|
|
|
|
|
|
|
.. _`install distribute`: http://pypi.python.org/pypi/distribute#installation-instructions
|
|
|
|
|
|
|
|
|