[TIP] doctest fixtures in nose

Barry Warsaw barry at python.org
Mon Jan 7 09:53:53 PST 2013


On Jan 07, 2013, at 05:19 PM, Benji York wrote:

>On Mon, Jan 7, 2013 at 4:35 PM, Barry Warsaw <barry at python.org> wrote:
>> I opened this issue in nose's tracker:
>>
>> https://github.com/nose-devs/nose/issues/594
>>
>> and my thoughts were to add a --doctest-fixture (singular) option which would
>> name a module using dotted-notation that would be the generic fixture.  So
>> e.g. I could add:
>>
>> --doctest-fixture=mypackage.subpkg.testing.doctest_fixture
>>
>> and nose would import that module as the fixture (i.e. it would be on
>> sys.path).  I suggest allowing --doctest-fixtures (plural) to still allow for
>> overriding doctest fixtures on a per-doctest basis.
>
>How would that work if you have several sets of tests, each with their
>own fixtures?  Say, organized in a fixture-per-directory arrangement?

That's a good question.

In my own use cases, I have one fixture to rule all doctests, although I do
occasionally use a different zope.testing layer for some doctests in a
directory.  I hack this in by adding a `layer` attribute in the __init__.py in
the doctest directory.

So another thought could be per-directory/subpackage fixtures defined in the
__init__.py file.  This wouldn't be configurable, and I'm not sure it would be
useful to make switchable via cli option.  But given this, here's the order of
fixture overrides:

 * --doctest-fixtures path/to/doctest/DOCTEST_fixture.py
 * path/to/doctest/subpackage/__init__.py if non-empty
 * --doctest-fixture=path.to.fixture.module

A different idea would involve the use of nose.cfg files with fixture
variables.  For example, in my top level nose.cfg (although I use one
top-level setup.cfg files currently), I could do something like this:

[nose]
...
doctest-fixture: path.to.fixture.module

for the global fixture.

Now, let's say I have a subpackage that needs a different fixture.  In that
subpackage's docs directory I could have a nose.cfg file with something like:

[fixture]
doctest-fixture: path.to.a.different.fixture.module

which would override the global one for doctests in this directory.

This would still let me define --doctest-fixtures suffixes to allow for
per-doctest fixtures.

FWIW, in my biggest project, I currently run all my doctests by having one big
test_suite() that manually searches the file system and adds DocFileSuites for
each .rst file found.  If I can solve the explosion-of-fixture-files problem I
can eliminate this.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130107/4128cd00/attachment.pgp>


More information about the testing-in-python mailing list