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
-1
votes
3 answers

Why do semicolons not suppress output in doctests?

Why do semicolons not suppress output in doctests? A workaround is to assign the result, but I am curious as to why this does not work. """ >>> 1+1; # Semicolons typically suppress output, but this fails >>> x = 1+1 # Workaround: assign result…
mforbes
  • 7,062
  • 3
  • 16
  • 21
-1
votes
1 answer

Python Unit Testing using Doctest not working as expected

I have created a python module to generate weather data(Latitude, Longitude, Elevation and other details) by taking particular location as input. Updated it as per standards and "pycodestyle" package for checking PEP8 standards does not throw any…
JKC
  • 2,498
  • 6
  • 30
  • 56
-1
votes
3 answers

How to return the x amount of vowels in a text file

The function takes a filename and x (which is meant to return the first 2 or 4 vowels in filename). The code I have written returns vowels but I'm not exactly sure what it is returning. The code should pass a doctest. I'm still trying to figure it…
-1
votes
2 answers

Doctest expecting True but keeps on receiving False

Ok so I need to pass the following doctests: """ Does it work on files where no error checking is needed on the fields >>> sumRows("rows1.csv") == {'tim': 36.0, 'bob': 11.0, 'anna': 54.0} True Does it ignore headers if requested? >>>…
Classydogg
  • 61
  • 1
  • 8
-1
votes
3 answers

Failed test - python palindrome checker

I have created a very simple palindrome checker with doctests. I'm having problem with the last doctest. It fails and is not carrying out the ignorecase=True. I cannot work out why the last test is failing. Code: # This Python file uses the…
user2987377
  • 91
  • 2
  • 9
-2
votes
1 answer

Module doctest does not run

I am trying to use the doctest module to test code. I tried this example: import doctest def areaTriangulo(base, altura): return 'El area del triangulo es: '+str((base*altura)/2) """ funcion que nos devuelve el area de un triangulo …
-2
votes
3 answers

unit testing of defined class using doctests

i wrote a docstring for doctests and i also wrote function correctly . But when i run this on hacker rank then it shows Runtime error I tried using docstring class Circle(): def __init__(self, radius): # Define the doctests for…
-2
votes
1 answer

error in function: 'str' object is not an iterator

I have a problem with the following function in python (where swap is a function that I have previously created and that works fine): def swap (cards): """ >>> swap('FBFFFBFFBF') 'BFBBBFBBFB' >>> swap('BFFBFBFFFBFBBBFBBBBFF') …
-3
votes
1 answer

Why does the doctest fail?

I made a class in Python to perform some basic operations. class perform(object): def add(self, first, second): """Adds two numbers >>>perform().add(1, 2) 3 Also adds string >>>perform().add('some',…
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
-3
votes
1 answer

How would I get this doctest to pass

I'm trying to get this doctest to pass by adding a function body but I don't know how to do that can anyone help me def reverse(s): """ >>> reverse("happy") 'yppah' >>> reverse("Python") 'nohtyP' >>> reverse("") '' …
1 2 3
29
30