By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Doctest is a module in Python that allows you to embed tests within the docstrings of your functions and classes. This practice is crucial for maintaining code quality and documentation accuracy. By integrating tests directly into docstrings, you can verify that your code behaves as expected and that your documentation remains up-to-date. Incorrect usage can lead to undetected bugs and misleading documentation, which can be particularly problematic in collaborative projects or when onboarding new team members.
Example: ```python def add(a, b): """ Return the sum of a and b.
>>> add(2, 3) 5 """ return a + b
``` - ⚠️ Common Pitfall: Forgetting to include the example in the docstring.
Run Doctests
doctest.testmod()
python if __name__ == "__main__": import doctest doctest.testmod()
⚠️ Common Pitfall: Not importing doctest or forgetting to call doctest.testmod().
doctest
Handle Floating-Point Numbers
...
Example: ```python def divide(a, b): """ Return the division of a by b.
>>> divide(10, 3) 3.33... """ return a / b
`` - ⚠️ Common Pitfall: Not using...` for floating-point outputs, leading to test failures.
`` - ⚠️ Common Pitfall: Not using
Test External Files
doctest.testfile()
python import doctest doctest.testfile("example.txt")
Experts view doctest as a dual-purpose tool: it not only verifies code correctness but also ensures that documentation remains accurate and useful. By embedding tests within docstrings, experts maintain a seamless integration of testing and documentation, leading to more reliable and maintainable codebases.
Exam trap: Questions that ask for the output of a function without a docstring example.
The mistake: Not running doctest.testmod().
Exam trap: Code snippets that omit doctest.testmod().
The mistake: Ignoring floating-point precision.
Exam trap: Questions that involve floating-point arithmetic without using ....
The mistake: Incorrect file paths for doctest.testfile().
Scenario: You need to document and test a function that calculates the factorial of a number. Question: Write the function with a doctest example. Solution: ```python def factorial(n): """ Return the factorial of n.
factorial(5) 120 """ if n == 0: return 1 else: return n * factorial(n-1) ``` Answer: The function correctly calculates the factorial. Why it works: The docstring includes a testable example, verifying the function's correctness.
Scenario: You have a function that returns the square root of a number. Question: Write the function with a doctest example that handles floating-point precision. Solution: ```python import math
def sqrt(x): """ Return the square root of x.
>>> sqrt(2) 1.41... """ return math.sqrt(x)
`` Answer: The function correctly calculates the square root. Why it works: Using...` makes the test flexible for floating-point outputs.
`` Answer: The function correctly calculates the square root. Why it works: Using
python import doctest doctest.testfile("tests.txt")
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.