Package logilab :: Package common :: Module pytest
[frames] | no frames]

Module pytest

source code

pytest is a tool that eases test running and debugging.

To be able to use pytest, you should either write tests using
the logilab.common.testlib's framework or the unittest module of the
Python's standard library.

You can customize pytest's behaviour by defining a ``pytestconf.py`` file
somewhere in your test directory. In this file, you can add options or
change the way tests are run.

To add command line options, you must define a ``update_parser`` function in
your ``pytestconf.py`` file. The function must accept a single parameter
that will be the OptionParser's instance to customize.

If you wish to customize the tester, you'll have to define a class named
``CustomPyTester``. This class should extend the default `PyTester` class
defined in the pytest module. Take a look at the `PyTester` and `DjangoTester`
classes for more information about what can be done.

For instance, if you wish to add a custom -l option to specify a loglevel, you
could define the following ``pytestconf.py`` file ::

    import logging
    from logilab.common.pytest import PyTester

    def update_parser(parser):
        parser.add_option('-l', '--loglevel', dest='loglevel', action='store',
                          choices=('debug', 'info', 'warning', 'error', 'critical'),
                          default='critical', help="the default log level possible choices are "
                          "('debug', 'info', 'warning', 'error', 'critical')")
        return parser


    class CustomPyTester(PyTester):
        def __init__(self, cvg, options):
            super(CustomPyTester, self).__init__(cvg, options)
            loglevel = options.loglevel.upper()
            logger = logging.getLogger('erudi')
            logger.setLevel(logging.getLevelName(loglevel))


In your TestCase class you can then get the value of a specific option with
the ``optval`` method::

    class MyTestCase(TestCase):
        def test_foo(self):
            loglevel = self.optval('loglevel')
            # ...


You can also tag your tag your test for fine filtering

With those tag::

    from logilab.common.testlib import tag, TestCase

    class Exemple(TestCase):

        @tag('rouge', 'carre')
        def toto(self):
            pass

        @tag('carre', 'vert')
        def tata(self):
            pass

        @tag('rouge')
        def titi(test):
            pass

you can filter the function with a simple python expression

 * ``toto`` and ``titi`` match ``rouge``

 * ``toto``, ``tata`` and ``titi``, match ``rouge or carre``

 * ``tata`` and ``titi`` match``rouge ^ carre``

 * ``titi`` match ``rouge and not carre``






:copyright: 2000-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: General Public License version 2 - http://www.gnu.org/licenses

Classes
  DjangoTester
  GlobalTestReport
this class holds global test statistics
  PyTester
encapsulates testrun logic
  TraceController
Functions
 
load_pytest_conf(path, parser)
loads a ``pytestconf.py`` file and update default parser and / or tester.
source code
 
make_parser()
creates the OptionParser instance...
source code
 
nocoverage(func) source code
 
parseargs(parser)
Parse the command line and return (options processed), (options to pass to unittest_main()), (explicitfile or None).
source code
 
project_root(parser, projdir='/build/buildd/logilab-common-0.45.2/doc')
try to find project's root and add it to sys.path
source code
 
remove_local_modules_from_sys(testdir)
remove all modules from cache that come from `testdir`
source code
 
run() source code
 
this_is_a_testdir(dirpath)
returns True if `filename` seems to be a test directory
source code
 
this_is_a_testfile(filename)
returns True if `filename` seems to be a test file
source code
Variables
  CONF_FILE = 'pytestconf.py'
  DJANGO_FOUND = False
  PYTEST_DOC = '%prog [OPTIONS] [testfile [testpattern]]\n\nexam...
  TESTDIR_RE = re.compile(r'^(unit)?tests?$')
  TESTFILE_RE = re.compile(r'^((unit)?test.*|smoketest)\.py$')
  __package__ = 'logilab.common'
Function Details

make_parser()

source code 
creates the OptionParser instance
    

remove_local_modules_from_sys(testdir)

source code 
remove all modules from cache that come from `testdir`

This is used to avoid strange side-effects when using the
testall() mode of pytest.
For instance, if we run pytest on this tree::

  A/test/test_utils.py
  B/test/test_utils.py

we **have** to clean sys.modules to make sure the correct test_utils
module is ran in B


Variables Details

PYTEST_DOC

Value:
'''%prog [OPTIONS] [testfile [testpattern]]

examples:

pytest path/to/mytests.py
pytest path/to/mytests.py TheseTests
pytest path/to/mytests.py TheseTests.test_thisone
pytest path/to/mytests.py -m \'(not long and database) or regr\'
...