Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify order to run testsetup blocks in doctest suite

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?

like image 382
A. Jesse Jiryu Davis Avatar asked Dec 07 '25 13:12

A. Jesse Jiryu Davis


1 Answers

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.

like image 65
aquavitae Avatar answered Dec 10 '25 03:12

aquavitae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!