2016-08-23 10:35:41 +08:00
|
|
|
.. _`noseintegration`:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Running tests written for nose
|
2010-11-06 18:38:53 +08:00
|
|
|
=======================================
|
|
|
|
|
|
|
|
.. include:: links.inc
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` has basic support for running tests written for nose_.
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
.. _nosestyle:
|
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
Usage
|
|
|
|
-------------
|
|
|
|
|
2019-02-15 21:10:37 +08:00
|
|
|
After :ref:`installation` type:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2013-08-23 18:59:57 +08:00
|
|
|
python setup.py develop # make sure tests can import our package
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest # instead of 'nosetests'
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-18 21:56:16 +08:00
|
|
|
and you should be able to run your nose style tests and
|
2014-01-18 19:31:33 +08:00
|
|
|
make use of pytest's capabilities.
|
2010-11-06 18:38:53 +08:00
|
|
|
|
|
|
|
Supported nose Idioms
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
* setup and teardown at module/class/method level
|
|
|
|
* SkipTest exceptions and markers
|
|
|
|
* setup/teardown decorators
|
2017-07-20 08:11:17 +08:00
|
|
|
* ``yield``-based tests and their setup (considered deprecated as of pytest 3.0)
|
2014-04-10 18:46:27 +08:00
|
|
|
* ``__test__`` attribute on modules/classes/functions
|
2010-11-06 18:38:53 +08:00
|
|
|
* general usage of nose utilities
|
|
|
|
|
2010-11-18 21:56:16 +08:00
|
|
|
Unsupported idioms / known issues
|
2010-11-06 18:38:53 +08:00
|
|
|
----------------------------------
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
- unittest-style ``setUp, tearDown, setUpClass, tearDownClass``
|
2013-11-22 21:07:56 +08:00
|
|
|
are recognized only on ``unittest.TestCase`` classes but not
|
|
|
|
on plain classes. ``nose`` supports these methods also on plain
|
2014-01-18 19:31:33 +08:00
|
|
|
classes but pytest deliberately does not. As nose and pytest already
|
2013-11-22 21:07:56 +08:00
|
|
|
both support ``setup_class, teardown_class, setup_method, teardown_method``
|
|
|
|
it doesn't seem useful to duplicate the unittest-API like nose does.
|
|
|
|
If you however rather think pytest should support the unittest-spelling on
|
|
|
|
plain classes please post `to this issue
|
2015-06-16 05:28:31 +08:00
|
|
|
<https://github.com/pytest-dev/pytest/issues/377/>`_.
|
2013-11-22 22:12:12 +08:00
|
|
|
|
|
|
|
- nose imports test modules with the same import path (e.g.
|
|
|
|
``tests.test_mod``) but different file system paths
|
|
|
|
(e.g. ``tests/test_mode.py`` and ``other/tests/test_mode.py``)
|
|
|
|
by extending sys.path/import semantics. pytest does not do that
|
2017-04-07 10:01:26 +08:00
|
|
|
but there is discussion in `#268 <https://github.com/pytest-dev/pytest/issues/268>`_ for adding some support. Note that
|
2016-05-19 00:12:39 +08:00
|
|
|
`nose2 choose to avoid this sys.path/import hackery <https://nose2.readthedocs.io/en/latest/differences.html#test-discovery-and-loading>`_.
|
2014-01-18 19:31:33 +08:00
|
|
|
|
2017-04-07 09:01:03 +08:00
|
|
|
If you place a conftest.py file in the root directory of your project
|
|
|
|
(as determined by pytest) pytest will run tests "nose style" against
|
2017-04-07 10:01:26 +08:00
|
|
|
the code below that directory by adding it to your ``sys.path`` instead of
|
2017-04-07 09:01:03 +08:00
|
|
|
running against your installed code.
|
|
|
|
|
2017-04-07 10:01:26 +08:00
|
|
|
You may find yourself wanting to do this if you ran ``python setup.py install``
|
|
|
|
to set up your project, as opposed to ``python setup.py develop`` or any of
|
2017-04-07 09:01:03 +08:00
|
|
|
the package manager equivalents. Installing with develop in a
|
2018-05-25 15:54:03 +08:00
|
|
|
virtual environment like tox is recommended over this pattern.
|
2017-04-07 09:01:03 +08:00
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
- nose-style doctests are not collected and executed correctly,
|
2010-11-18 21:56:16 +08:00
|
|
|
also doctest fixtures don't work.
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2016-07-12 23:41:40 +08:00
|
|
|
- no nose-configuration is recognized.
|
|
|
|
|
|
|
|
- ``yield``-based methods don't support ``setup`` properly because
|
|
|
|
the ``setup`` method is always called in the same class instance.
|
|
|
|
There are no plans to fix this currently because ``yield``-tests
|
|
|
|
are deprecated in pytest 3.0, with ``pytest.mark.parametrize``
|
|
|
|
being the recommended alternative.
|