コンテンツにスキップ

「python -m doctest」コマンドの使い方と修正方法を簡単に解説

[

Using doctest | Python Learn


Python Learn is an online course that focuses on teaching software engineering principles in Python. In this course, you will learn about various concepts and techniques that can enhance your skills as a data scientist and revolutionize your data science workflow. One particular topic covered in the course is the usage of doctest in Python for testing and debugging.

Writing a Python Module

The first module in the Python Learn course covers the basics of writing a Python package. By the end of this module, you will be able to develop your own Python package with proper structure and code organization. This will enable you to distribute and use your package similar to popular packages such as NumPy and Pandas.

Utilizing Classes

The second module dives into the concept of object-oriented programming (OOP) in Python. By leveraging classes and inheritance, you will learn how to make your Python package more powerful and versatile for your users. These techniques allow for better code organization, reusability, and scalability.

Maintainability

Once you have written a fully functional Python package, it is crucial to focus on its maintainability. The third module of the course introduces best practices for maintaining your projects, including proper documentation and unit testing.

Documentation

Proper documentation is essential for understanding and maintaining your code. The module covers topics such as identifying good comments, writing docstrings, utilizing good function and variable names, and refactoring code for improved readability. Following these practices will make your code more understandable and maintainable.

Unit Testing

Unit testing is a critical aspect of ensuring the stability and correctness of your code. In this module, you will learn about using doctest and pytest for testing your Python functions. doctest can be especially useful when writing comprehensive docstrings with examples. It provides a simple way to test your functions by executing the examples within docstrings.

Using doctest

To demonstrate the usage of doctest, let’s consider the following example:

def sum_counters(counters):
"""
Sums all the counters in a list.
>>> c1 = Counter({'a': 1, 'b': 2})
>>> c2 = Counter({'b': 3, 'c': 1})
>>> counters = [c1, c2]
>>> sum_counters(counters)
Counter({'a': 1, 'b': 5, 'c': 1})
"""
total_counter = Counter()
for counter in counters:
total_counter += counter
return total_counter

In this example, the sum_counters function takes a list of counters and sums them up using the += operator. The doctest example demonstrates the expected output of the function when provided with specific counters.

To test this example using doctest, follow these steps:

  1. Complete the input code of the example in the docstring for sum_counters. Make sure to provide valid counter objects.
  2. Fill in the expected output in the docstring example.
  3. Run the testmod function from doctest to test the example code of the function.

Running the testmod function will compare the actual output of the function with the expected output provided in the docstring example. If the outputs match, the example passes; otherwise, an assertion error will be raised.

By utilizing doctest, you can easily test your functions with minimal effort by including comprehensive examples in your docstrings. This approach helps in catching bugs and verifying the correctness of your code.


Python Learn offers various exercises and tutorials to enhance your Python skills. Whether you are a beginner or an experienced programmer, this course provides valuable insights into software engineering principles and techniques in Python. Start your journey with Python Learn today to become a proficient Python developer.