Questions tagged [doctest]

The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. The test cases and expected output can be copied from an interactive Python interpreter session. During regression testing doctest alerts about failed cases.

The following overview is from the Python Standard Library documentation doctest page. The examples in the official documentation provide also a nice tutorial to using doctest.

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.
  • To write tutorial documentation for a package, liberally illustrated with input-output examples. Depending on whether the examples or the expository text are emphasized, this has the flavor of “literate testing” or “executable documentation”.
446 questions
0
votes
1 answer

How to doctest random.sample() when used on a set?

I am trying to write a doctest for a function that calls random.sample() on a set. Unfortunately, it seems that seeding is not sufficient to guarantee an output. Consider the following: >>> import random >>> random.seed(1) >>> s = set(('Ut', 'Duis',…
nimble agar
  • 417
  • 8
  • 15
0
votes
1 answer

Arrays as expected results in doctest. Error with text 'Expected nothing'. Python 3.3.2

I've tried to use doctest for following source: def add_greeting(L=[]): """ (list) -> NoneType Append 'hello' to L and print L. >>> greetings_list = ['hi', 'bonjour'] >>> add_greeting(greetings_list) >>> greetings_list …
mnlaptev
  • 113
  • 1
  • 6
0
votes
1 answer

saving stdout into a variable in a doctest

I have a doctest, and I want to save what a command I run into a variable, to use it in another function >>> sh("git finish", return_output=True).split("#")[1].split(":")[0] (here comes an int i want to save, say 900) the sh is a command line…
Confidence
  • 2,283
  • 3
  • 33
  • 57
0
votes
1 answer

Doctest error with simple case

With def show(a): """ Shows a string >>> show(a) a """ print(a) def test(): import doctest doctest.testmod() if __name__ == '__main__': test() I am getting an error while trying to learn how a docstring works. Both this method…
0
votes
1 answer

How to use doctest on a Client script?

I am playing with Google Calendar API, creating some useful function. I another hand, I want to do it right puting some useful doctest and starting agile development. How to write doctest since the result of each function is not really predictible…
Natim
  • 17,274
  • 23
  • 92
  • 150
0
votes
1 answer

python doctest check for a specific line

I'd like to write a doctest like this: """ >>> checking() some random text some more random text ... test is passed ##ignore all above/below lines except this one more and more randomness ... finished. """ I don't really care about first few…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
0
votes
2 answers

Doctest failing inspite of having correct output

My function is def validate_latitude(lat): """Enforce latitude is in range >>> validate_latitude(65) 65 >>> validate_latitude(91) 90 >>> validate_latitude(-91) -90 """ lat = min(lat, 90) lat = max(lat,…
agiliq
  • 7,518
  • 14
  • 54
  • 74
0
votes
2 answers

DOCTEST==argv[0] as a convention?

In a bit of Python I'm writing (a command line and filter testing tool: claft) I wanted a simple way to invoke the built-in test suite (doctest) and I decided on the following: if 'DOCTEST' in os.environ and os.environ['DOCTEST']==sys.argv[0]: …
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
0
votes
1 answer

Loading of initial data before tests in Zope 3

How can I fill database with test data in Zope 3 project? I want this data to be loaded before any tests began (doctest, unit test...), I don't want to load it manually in SetUp methods...Thanks!
Gleb
  • 731
  • 1
  • 8
  • 14
0
votes
2 answers

Why would I write doctests in restructured text?

Another way to ask this: If I wrote doctests in reST, can I use it for Sphinx or other automatic documentation efforts? Background: I don't know how to use Sphinx and have not much experience with reST either, so I am wondering if I can use…
K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
0
votes
1 answer

Doctest's output different from shell's output

Using the same code in a doctest and in the shell yields different outputs. I have a function called a() that runs through a few tests. Those same tests are used in a doctest (test() ). With a() I get OBJECT-BLANKLINE-OBJECT while the test() gives…
-1
votes
1 answer

How to unit test Python function that returns a relative path?

I wrote a Python function that produces a filepath for a dataset. I'd like to include a doctest for the function. However, since each machine will have a different relative filepath, I'm not sure how to write the test so that it will pass on any…
Brian_E
  • 34
  • 2
-1
votes
2 answers

Multiline return value statements in Python doctests

I am testing a function that produces results that are very long when represented, and I don't know how to fit that result into the doctest. In this example, I'm using pscyopg2, which has verbose representations for its composed queries (details as…
rovyko
  • 4,068
  • 5
  • 32
  • 44
-1
votes
1 answer

Adding Python package to path for VSCode terminal

I'm writing python classes in a package using VScode. I want to add this package's parent directory to the Python path when using the VScode Terminal so I can import the package (regardless of the directory of the file that's being run). I've tried…
Justin
  • 1,980
  • 1
  • 16
  • 25
-1
votes
2 answers

doctest is failing while output seems correct

I am trying to run a python code and the output seems to be correct but is not passing the doctest test cases I need help in this. My output is perfect and the output looks the same even it looks the same as that of the testcases provided in…
1 2 3
29
30