2016-08-23 10:35:41 +08:00
.. _`noseintegration`:
2021-03-11 03:18:21 +08:00
How to run tests written for nose
2010-11-06 18:38:53 +08:00
=======================================
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
2022-10-10 04:16:33 +08:00
.. warning ::
This functionality has been deprecated and is likely to be removed in `` pytest 8.x `` .
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
----------------------
2022-10-07 22:10:28 +08:00
* `` setup() `` and `` teardown() `` at module/class/method level: any function or method called `` setup `` will be called during the setup phase for each test, same for `` teardown `` .
* `` SkipTest `` exceptions and markers
2010-11-06 18:38:53 +08:00
* setup/teardown decorators
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
2021-11-06 17:16:11 +08:00
plain classes please post to :issue: `377` .
2013-11-22 22:12:12 +08:00
- nose imports test modules with the same import path (e.g.
2019-06-13 12:01:30 +08:00
`` tests.test_mode `` ) but different file system paths
2013-11-22 22:12:12 +08:00
(e.g. `` tests/test_mode.py `` and `` other/tests/test_mode.py `` )
by extending sys.path/import semantics. pytest does not do that
2021-11-06 17:16:11 +08:00
but there is discussion in :issue: `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.
2020-05-12 04:36:08 +08:00
- `` yield `` -based methods are unsupported as of pytest 4.1.0. They are
fundamentally incompatible with pytest because they don't support fixtures
properly since collection and test execution are separated.
2020-03-06 10:11:24 +08:00
2020-11-19 20:54:40 +08:00
Migrating from nose to pytest
2020-11-19 03:24:19 +08:00
------------------------------
`nose2pytest <https://github.com/pytest-dev/nose2pytest> `_ is a Python script
2020-11-19 20:54:40 +08:00
and pytest plugin to help convert Nose-based tests into pytest-based tests.
2020-11-19 03:24:19 +08:00
Specifically, the script transforms nose.tools.assert_* function calls into
raw assert statements, while preserving format of original arguments
as much as possible.
2020-03-06 10:11:24 +08:00
.. _nose: https://nose.readthedocs.io/en/latest/