2010-11-06 06:37:25 +08:00
Changing standard (Python) test discovery
===============================================
2011-09-06 17:43:42 +08:00
Changing directory recursion
2010-11-06 06:37:25 +08:00
-----------------------------------------------------
You can set the :confval:`norecursedirs` option in an ini-file, for example your ``setup.cfg`` in the project root directory::
# content of setup.cfg
[pytest]
norecursedirs = .svn _build tmp*
2014-01-18 19:31:33 +08:00
This would tell ``pytest`` to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory.
2010-11-06 06:37:25 +08:00
2010-11-25 05:01:04 +08:00
.. _`change naming conventions`:
2011-09-06 17:43:42 +08:00
Changing naming conventions
2010-11-25 05:01:04 +08:00
-----------------------------------------------------
You can configure different naming conventions by setting
2010-11-25 19:11:10 +08:00
the :confval:`python_files`, :confval:`python_classes` and
:confval:`python_functions` configuration options. Example::
2010-11-25 05:01:04 +08:00
# content of setup.cfg
# can also be defined in in tox.ini or pytest.ini file
[pytest]
python_files=check_*.py
python_classes=Check
2014-10-17 06:27:10 +08:00
python_functions=*_check
2010-11-25 05:01:04 +08:00
2014-10-17 06:27:10 +08:00
This would make ``pytest`` look for tests in files that match the ``check_*
.py`` glob-pattern, ``Check`` prefixes in classes, and functions and methods
that match ``*_check``. For example, if we have::
2010-11-25 05:01:04 +08:00
# content of check_myapp.py
class CheckMyApp:
2014-10-17 06:27:10 +08:00
def simple_check(self):
2010-11-25 05:01:04 +08:00
pass
2014-10-17 06:27:10 +08:00
def complex_check(self):
2010-11-25 05:01:04 +08:00
pass
then the test collection looks like this::
2013-08-01 23:32:19 +08:00
$ py.test --collect-only
2011-03-09 17:58:36 +08:00
=========================== test session starts ============================
2014-10-24 21:08:43 +08:00
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.6.4
2012-10-07 19:06:17 +08:00
collected 2 items
2010-11-25 05:01:04 +08:00
<Module 'check_myapp.py'>
<Class 'CheckMyApp'>
<Instance '()'>
2014-10-17 06:27:10 +08:00
<Function 'simple_check'>
<Function 'complex_check'>
2014-01-29 20:47:11 +08:00
2012-10-25 19:48:31 +08:00
============================= in 0.01 seconds =============================
2010-11-25 05:01:04 +08:00
2013-11-22 20:53:43 +08:00
.. note::
2014-01-18 19:31:33 +08:00
2014-10-17 06:27:10 +08:00
the ``python_functions`` and ``python_classes`` options has no effect
2013-11-22 20:53:43 +08:00
for ``unittest.TestCase`` test discovery because pytest delegates
detection of test case methods to unittest code.
2011-09-06 17:43:42 +08:00
Interpreting cmdline arguments as Python packages
2010-11-07 07:22:16 +08:00
-----------------------------------------------------
2014-01-18 19:31:33 +08:00
You can use the ``--pyargs`` option to make ``pytest`` try
2010-11-07 07:22:16 +08:00
interpreting arguments as python package names, deriving
2010-11-25 05:01:04 +08:00
their file system path and then running the test. For
example if you have unittest2 installed you can type::
py.test --pyargs unittest2.test.test_skipping -q
2010-11-25 19:11:10 +08:00
which would run the respective test module. Like with
2010-11-25 05:01:04 +08:00
other options, through an ini-file and the :confval:`addopts` option you
can make this change more permanently::
2010-11-07 07:22:16 +08:00
2010-11-21 04:35:55 +08:00
# content of pytest.ini
2010-11-07 07:22:16 +08:00
[pytest]
addopts = --pyargs
2010-11-06 06:37:25 +08:00
2010-11-25 05:01:04 +08:00
Now a simple invocation of ``py.test NAME`` will check
if NAME exists as an importable package/module and otherwise
treat it as a filesystem path.
2011-09-06 17:43:42 +08:00
Finding out what is collected
2010-11-06 06:37:25 +08:00
-----------------------------------------------
You can always peek at the collection tree without running tests like this::
2013-08-01 23:32:19 +08:00
. $ py.test --collect-only pythoncollection.py
2011-03-09 17:58:36 +08:00
=========================== test session starts ============================
2014-10-24 21:08:43 +08:00
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.6.4
2012-10-07 19:06:17 +08:00
collected 3 items
2010-11-21 04:35:55 +08:00
<Module 'pythoncollection.py'>
2010-11-06 18:38:53 +08:00
<Function 'test_function'>
<Class 'TestClass'>
<Instance '()'>
<Function 'test_method'>
<Function 'test_anothermethod'>
2014-01-29 20:47:11 +08:00
2012-11-20 21:01:31 +08:00
============================= in 0.01 seconds =============================
2012-06-07 18:39:53 +08:00
2012-06-11 22:24:42 +08:00
customizing test collection to find all .py files
2012-06-07 18:39:53 +08:00
---------------------------------------------------------
.. regendoc:wipe
2014-01-18 19:31:33 +08:00
You can easily instruct ``pytest`` to discover tests from every python file::
2012-06-07 18:39:53 +08:00
# content of pytest.ini
[pytest]
python_files = *.py
However, many projects will have a ``setup.py`` which they don't want to be imported. Moreover, there may files only importable by a specific python version.
2014-01-18 19:31:33 +08:00
For such cases you can dynamically define files to be ignored by listing
them in a ``conftest.py`` file::
2012-06-07 18:39:53 +08:00
# content of conftest.py
import sys
collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
collect_ignore.append("pkg/module_py2.py")
And then if you have a module file like this::
# content of pkg/module_py2.py
def test_only_on_python2():
try:
assert 0
except Exception, e:
pass
and a setup.py dummy file like this::
# content of setup.py
0/0 # will raise exeption if imported
2014-01-18 19:31:33 +08:00
then a pytest run on python2 will find the one test when run with a python2
2012-06-07 18:39:53 +08:00
interpreters and will leave out the setup.py file::
2014-01-18 19:31:33 +08:00
2013-08-01 23:32:19 +08:00
$ py.test --collect-only
2012-06-07 18:39:53 +08:00
=========================== test session starts ============================
2014-10-24 21:08:43 +08:00
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.6.4
2014-08-08 03:41:51 +08:00
collected 0 items
2014-01-29 20:47:11 +08:00
2014-10-24 21:08:43 +08:00
============================= in 0.00 seconds =============================
2012-06-07 18:39:53 +08:00
If you run with a Python3 interpreter the moduled added through the conftest.py file will not be considered for test collection.