| 1 |
=============== |
| 2 |
flufl.testing |
| 3 |
=============== |
| 4 |
|
| 5 |
This is a small collection of test helpers that I use in almost all my |
| 6 |
packages. Specifically, plugins for the following test tools are provided: |
| 7 |
|
| 8 |
* nose2_ |
| 9 |
* flake8_ |
| 10 |
|
| 11 |
Python 3.4 is the minimum supported version. |
| 12 |
|
| 13 |
|
| 14 |
Using test helpers |
| 15 |
================== |
| 16 |
|
| 17 |
You can use each of the plugins independently. For example, if you use flake8 |
| 18 |
but you don't use nose2, just enable the flake8 plugin and ignore the rest. |
| 19 |
|
| 20 |
|
| 21 |
flake8 import order plugin |
| 22 |
-------------------------- |
| 23 |
|
| 24 |
This flake8_ plugin enables import order checks as are used in the `GNU |
| 25 |
Mailman`_ project. Specifically, it enforces the following rules: |
| 26 |
|
| 27 |
* Non-``from`` imports must precede ``from``-imports, except import from |
| 28 |
``__future__`` module, which should be on the top. |
| 29 |
* Exactly one line must separate the block of non-``from`` imports from the |
| 30 |
block of ``from`` imports. |
| 31 |
* Import exactly one module per non-``from`` import line. |
| 32 |
* Lines in the non-``from`` import block are sorted by length first, then |
| 33 |
alphabetically. Dotted imports count toward both restrictions. |
| 34 |
* Lines in the ``from`` import block are sorted alphabetically. |
| 35 |
* Multiple names can be imported in a ``from`` import line, but those names |
| 36 |
must be sorted alphabetically. |
| 37 |
* Names imported from the same module in a ``from`` import must appear in the |
| 38 |
same import statement. |
| 39 |
|
| 40 |
It's so much easier to see an example:: |
| 41 |
|
| 42 |
from __future__ import generator_stop |
| 43 |
|
| 44 |
import copy |
| 45 |
import socket |
| 46 |
import logging |
| 47 |
import smtplib |
| 48 |
|
| 49 |
from mailman import public |
| 50 |
from mailman.config import config |
| 51 |
from mailman.interfaces.mta import IMailTransportAgentDelivery |
| 52 |
from mailman.mta.connection import Connection |
| 53 |
from zope.interface import implementer |
| 54 |
|
| 55 |
To enable this plugin [#]_, add the following to your ``tox.ini`` or any other |
| 56 |
`flake8 recognized configuration file`_:: |
| 57 |
|
| 58 |
[flake8] |
| 59 |
enable-extensions = U4 |
| 60 |
|
| 61 |
|
| 62 |
nose2 plugin |
| 63 |
------------ |
| 64 |
|
| 65 |
The `nose2`_ plugin enables a few helpful things for folks who use that test |
| 66 |
runner: |
| 67 |
|
| 68 |
* Implements better support for doctests, including supporting layers. |
| 69 |
* Enables sophisticated test pattern matching. |
| 70 |
* Provides test tracing. |
| 71 |
* A *log to stderr* flag that you can check. [#]_ |
| 72 |
* Pluggable doctest setup/teardowns. |
| 73 |
|
| 74 |
To enable this plugin, add the following to your ``unittest.cfg`` file, in the |
| 75 |
``[unittest]`` section:: |
| 76 |
|
| 77 |
plugins = flufl.testing.nose |
| 78 |
|
| 79 |
You also need to add this section to ``unittest.cfg``, where *<package>* names |
| 80 |
the top-level package you want to test:: |
| 81 |
|
| 82 |
[flufl.testing] |
| 83 |
always-on = True |
| 84 |
package = <package> |
| 85 |
|
| 86 |
Now when you run your tests, you can include one or more ``-P`` options, which |
| 87 |
provide patterns to match your tests against. If given, only tests matching |
| 88 |
the given pattern are run. This is especially helpful if your test suite is |
| 89 |
huge. These patterns can match a test name, class, module, or filename, and |
| 90 |
follow Python's regexp syntax. |
| 91 |
|
| 92 |
The following options are also available by setting configuration variables in |
| 93 |
your ``unittest.cfg`` file, under the ``[flufl.testing]`` section. |
| 94 |
|
| 95 |
|
| 96 |
Doctests |
| 97 |
~~~~~~~~ |
| 98 |
|
| 99 |
The plugin also provides some useful features for doctests. If you make a |
| 100 |
directory a package in your source tree (i.e. by adding an `__init__.py`), you |
| 101 |
can optionally also add specify a `nose2 layer`_ to use for the doctest. Bind |
| 102 |
the layer object you want to the ``layer`` attribute in the ``__init__.py`` |
| 103 |
and it will be automatically assigned to the doctest's ``layer`` attribute for |
| 104 |
nose2 to find. |
| 105 |
|
| 106 |
Also for doctests, you can specify the ``setUp()`` and ``tearDown()`` methods |
| 107 |
you want by adding the following:: |
| 108 |
|
| 109 |
setup = my.package.namespace.setup |
| 110 |
teardown = my.package.other.namespace.teardown |
| 111 |
|
| 112 |
The named packages will be imported, with the last path component naming an |
| 113 |
attribute in the module. This attribute should be a function taking a single |
| 114 |
argument, in the style used by the stdlib ``doctest.DocFileTest`` class [#]_. |
| 115 |
|
| 116 |
You can also name a default layer by setting:: |
| 117 |
|
| 118 |
default_layer = my.package.layers.DefaultLayer. |
| 119 |
|
| 120 |
This has the same format as the ``setup`` and ``teardown`` settings, except |
| 121 |
that it should name a class. |
| 122 |
|
| 123 |
|
| 124 |
Pre-test initialization |
| 125 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 126 |
|
| 127 |
If you need to do anything before the tests starts, such as initialize |
| 128 |
database connections or acquire resources, set this:: |
| 129 |
|
| 130 |
start_run = my.package.initializer |
| 131 |
|
| 132 |
This has the same format as the ``setup`` and ``teardown`` settings, except |
| 133 |
that it takes a single argument which is the plugin instance. You can use |
| 134 |
this plugin instance for example to check if the ``-E`` option was given on |
| 135 |
the command line. This flag sets the ``stderr`` attribute to True on the |
| 136 |
plugin instance. |
| 137 |
|
| 138 |
|
| 139 |
Tracing |
| 140 |
~~~~~~~ |
| 141 |
|
| 142 |
If you add this the plugin will also print some additional tracers to stderr |
| 143 |
for ever test as it starts and stops:: |
| 144 |
|
| 145 |
trace = True |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
Author |
| 150 |
====== |
| 151 |
|
| 152 |
``flufl.testing`` is Copyright (C) 2013-2018 Barry Warsaw <[email protected]> |
| 153 |
|
| 154 |
Licensed under the terms of the Apache License, Version 2.0. |
| 155 |
|
| 156 |
|
| 157 |
Project details |
| 158 |
=============== |
| 159 |
|
| 160 |
* Project home: https://gitlab.com/warsaw/flufl.testing |
| 161 |
* Report bugs at: https://gitlab.com/warsaw/flufl.testing/issues |
| 162 |
* Code hosting: https://gitlab.com/warsaw/flufl.testing.git |
| 163 |
* Documentation: https://gitlab.com/warsaw/flufl.testing/tree/master |
| 164 |
|
| 165 |
|
| 166 |
Footnotes |
| 167 |
========= |
| 168 |
|
| 169 |
.. [#] Note that flake8 3.1 or newer is required. |
| 170 |
.. [#] It's up to your application to do something with this flag. |
| 171 |
.. [#] This class is undocumented, so use the doctest_ module source to grok |
| 172 |
the details. |
| 173 |
|
| 174 |
|
| 175 |
.. _flake8: http://flake8.pycqa.org/en/latest/index.html |
| 176 |
.. _`GNU Mailman`: http://www.list.org |
| 177 |
.. _`flake8 recognized configuration file`: http://flake8.pycqa.org/en/latest/user/configuration.html |
| 178 |
.. _nose2: http://nose2.readthedocs.io/en/latest/index.html |
| 179 |
.. _`nose2 layer`: http://nose2.readthedocs.io/en/latest/plugins/layers.html |
| 180 |
.. _doctest: https://docs.python.org/3/library/doctest.html |