Integrating with distutils / ``python setup.py test``
easily integrate test runs into your distutils or
+You can integrate test runs into your distutils or
setuptools based project. Use the `genscript method`_
to generate a standalone py.test script::
* ``Test`` prefixed test classes (without an ``__init__`` method)
* ``test_`` prefixed test functions or methods are test items
-For examples of how to c
nd cusotmize your test discovery :doc:`example/pythoncollection`.
+For examples of how to cmize your test discovery :doc:`example/pythoncollection`.
py.test additionally discovers tests using the standard
+ discovers tests using the standard
:ref:`unittest.TestCase <unittest.TestCase>` subclassing technique.
Choosing a test layout / import rules
-You can always run your tests by pointing to it::
+In both cases you usually need to make sure that ``mypkg`` is importable,
+for example by using the setuptools ``python setup.py develop`` method.
+You can run your tests by pointing to it::
py.test tests/test_app.py # for external test dirs
py.test mypkg/test/test_app.py # for inlined test dirs
- Test modules are imported under their fully qualified name as follows:
+ If py.test finds a "a/b/test_module.py" test file while
+ recursing into the filesystem it determines the import name
* find ``basedir`` -- this is the first "upward" (towards the root)
- directory not containing an ``__init__.py``
+ directory not containing an ``__init__.py``. If both the ``a``
+ and ``b`` directories contain an ``__init__.py`` the basedir will
+ be the parent dir of ``a``.
- * perform ``sys.path.insert(0, basedir)`` to make the fully
- qualified test module path importable.
+ * perform ``sys.path.insert(0, basedir)`` to make the test module
+ importable under the fully qualified import name.
- * ``import path.to.test_module`` where the path is determined
- by converting path separators into "." files. This means
+ * ``import a.b.test_module`` where the path is determined
+ by converting path separators ``/`` into "." characters. This means
you must follow the convention of having directory and file
- names map to the import names.
+ names map directly to the import names.
+ The reason for this somewhat evolved importing technique is
+ that in larger projects multiple test modules might import
+ from each other and thus deriving a canonical import name helps
+ to avoid surprises such as a test modules getting imported twice.