Question

In a big ReStructured Text file with many Python doctests, I have a testsetup block I want to run before each doctest, and some testsetup blocks I want to run before only some of the doctests. I know how to do this with groups, but is there a way to specify in what order the testsetup blocks are run?

Here's a file, foo.rst:

.. testsetup:: *

  import sys
  sys.stderr.write('testsetup *' + chr(10))

.. testsetup:: my-group

  import sys
  sys.stderr.write('testsetup my-group' + chr(10))

.. doctest:: my-group

  >>> print 'test 1'
  test 1

When it's run, I see:

testsetup my-group
testsetup *

Is there some way to force the opposite order, so the most-widely-applicable setup is run before the group-specific setup?

Was it helpful?

Solution

Looking at the latest sphinx source code (see DocTestBuilder.test_doc), the answer is no, its not possible. At the moment, all the test blocks are collected in order and either assigned to a group or to a special add_to_all_groups. After this, the code in add_to_all_groups is appended to the list of code for each group, the result being that it will always run after group-specific code. It looks like it would be quite easy to write a patch to change this behaviour, or better still to provide it as an option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top