Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to only test specific functions with doctest in a module?

I am trying to get into testing in Python using the doctest module. At the moment I do

  1. Write the tests for the functions.
  2. implement the functions code.
  3. If Tests pass, write more tests and more code.
  4. When the function is done move on to the next function to implement.

So after 3 or 4 (independent) functions in the same module with many tests I get a huge output by doctest. And it is a little annoysing.

Is there a way to tell doctest "don't test functions a(), b() and c()", so that it runs only the unmarked functions?

I only found the doctest.SKIP flag, which is not sufficient for my needs. I would have to place this flag in a lot of lines. And if I would want to check a marked function again, I would have to go manually through the code and remove any flag I set inside.

like image 706
Aufwind Avatar asked Apr 09 '12 21:04

Aufwind


People also ask

Can we handle unpredictable output using doctest in Python?

When the tests include values that are likely to change in unpredictable ways, and where the actual value is not important to the test results, you can use the ELLIPSIS option to tell doctest to ignore portions of the verification value.

Can doctest be used to test docstrings?

There are several common ways to use doctest: To check that a module's docstrings are up-to-date by verifying that all interactive examples still work as documented. To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.

What is the correct way to run all the doctest in a given file?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.


2 Answers

looks like you could pass the function to run_docstring_examples:

def f(a, b, c):
    '''
    >>> f(1,2,3)
    42
    '''

if __name__ == '__main__':
    import doctest
#    doctest.testmod()
    doctest.run_docstring_examples(f, globals())

example found via google.

like image 182
andrew cooke Avatar answered Oct 04 '22 21:10

andrew cooke


I put together a helper script to make this a little less painful. It can be installed using:

pip install doctestfn

It can then be used as follows:

usage: doctestfn [-h] [-v] module function

Run doctests for one function

positional arguments:
  module         Module to load
  function       Function to test

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  Enable verbose doctest output
like image 2
jncraton Avatar answered Oct 04 '22 19:10

jncraton