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*
|
|
|
|
|
|
|
|
This would tell py.test to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory.
|
|
|
|
|
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
|
|
|
|
python_functions=check
|
|
|
|
|
|
|
|
This would make py.test look for ``check_`` prefixes in
|
|
|
|
Python filenames, ``Check`` prefixes in classes and ``check`` prefixes
|
|
|
|
in functions and classes. For example, if we have::
|
|
|
|
|
|
|
|
# content of check_myapp.py
|
|
|
|
class CheckMyApp:
|
|
|
|
def check_simple(self):
|
|
|
|
pass
|
|
|
|
def check_complex(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
then the test collection looks like this::
|
|
|
|
|
|
|
|
$ py.test --collectonly
|
2011-03-09 17:58:36 +08:00
|
|
|
=========================== test session starts ============================
|
2011-11-19 02:32:11 +08:00
|
|
|
platform darwin -- Python 2.7.1 -- pytest-2.2.0
|
2011-03-09 17:58:36 +08:00
|
|
|
collecting ... collected 2 items
|
2010-11-25 05:01:04 +08:00
|
|
|
<Module 'check_myapp.py'>
|
|
|
|
<Class 'CheckMyApp'>
|
|
|
|
<Instance '()'>
|
|
|
|
<Function 'check_simple'>
|
|
|
|
<Function 'check_complex'>
|
2011-03-09 17:58:36 +08:00
|
|
|
|
2011-10-19 02:07:45 +08:00
|
|
|
============================= in 0.02 seconds =============================
|
2010-11-25 05:01:04 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Interpreting cmdline arguments as Python packages
|
2010-11-07 07:22:16 +08:00
|
|
|
-----------------------------------------------------
|
|
|
|
|
|
|
|
You can use the ``--pyargs`` option to make py.test try
|
|
|
|
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::
|
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
. $ py.test --collectonly pythoncollection.py
|
2011-03-09 17:58:36 +08:00
|
|
|
=========================== test session starts ============================
|
2011-11-19 02:32:11 +08:00
|
|
|
platform darwin -- Python 2.7.1 -- pytest-2.2.0
|
2011-03-09 17:58:36 +08:00
|
|
|
collecting ... 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'>
|
2011-03-09 17:58:36 +08:00
|
|
|
|
2011-05-01 18:38:56 +08:00
|
|
|
============================= in 0.01 seconds =============================
|