Difference between Unittest and Doctest29 Aug 2024 | 5 min read In this tutorial, we will discuss the difference between the doctest and unittest and see some cases. Testing is an important phase of software development, and it helps to identify an error, agile code, and code reusability. A codebase is tested with several test cases to prevent breaking and minimize exposure to vulnerability. Python provides the two main testing frameworks, doctest, and unittest. Let's get familiar with both frameworks. Introduction to DoctestDoctest is an in-built framework which means it comes with the Python installation, or we don't need to install it separately. The doctest module is used to identify and execute code snippets that mimic interactive Python sessions, and it tests to ensure that the results match the expected output as described. The doctest is most commonly used to test the documentation. It checks the statement sequence in a docstring, re-executes the extracted command, and compares it with the command line input in the docstring. By default, while using the doctest module, the output is not displayed when a test case passes. However, this behavior can be altered using options in the doctest runner. Moreover, the compatibility of doctest with the Python unittest framework allows us to execute doctests as regular unittest test cases. If you want to learn more about the doctest module, you can check our Python Doctest Module tutorial. Introduction to UnittestThe test runners in unittest provide additional options when executing test cases, including the ability to generate statistics reports on the results, including the number of test cases that have passed and failed. In the unittest, the methods created in the class manage the tests. It supports the setup, automation, and shutdown code when testing. It comes with various in-built, rich features, including generators and group fixture managers that are not included in the doctest. Since that unittest follows an object-oriented approach, it is better suited for testing class-based methods in a non-production environment. On the other hand, continuous delivery tools such as Jenkins or Travis CI are more appropriate for production environments. In the following sections, we will demonstrate a real-world example of code that deals with employee information and their efs on salary and test it using both doctest and unittest. After conducting the tests, we will evaluate the results and suggest ways to enhance the testing process further. Coding Example - Using UnittestLet's implement the Employee class and create some methods to tests using the unittest. Let's see the following code. Example - In the above code, we import the unittest from the Python package and import the Employee class. In the TestEmployee class, we use the "TestCase" from unittest as it allows us to verify specific outputs for each set of inputs. We then design three separate tests using methods that start with the "test" prefix. The "test" prefix signals the test runner to identify which methods to run as tests. test_employee.py In the above code, we import the unittest from the Python package and import the Employee class. In the TestEmployee class, we use the "TestCase" from unittest as it provides us with the ability to verify specific outputs for each set of inputs. We then design three separate tests using methods that start with the "test" prefix. The "test" prefix signals the test runner to identify which methods to run as tests. The setUp() method is executed before any other test method, and it is where we create instances of the customer. We use the assertEqual() method from the assert() method to compare expected results with actual results in each of the test methods. When we run the above test, we get the following result. ... ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK If we make alter the expected output, we will see the following result. C:\Users\User\Desktop\my_project\unittest>python test_employee.py F.. ====================================================================== FAIL: test_apply_epf (__main__.TestEmployee) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_employee.py", line 22, in test_apply_epf self.assertEqual(self.employee_1.salary, 47500) AssertionError: 475000 != 47500 ---------------------------------------------------------------------- Ran 3 tests in 0.001s FAILED (failures=1) This type of testing allows for quick identification of bugs and helps pinpoint where in the code the error occurs. By utilizing unittest and crafting tests that encompass all possible scenarios, we can enhance our program's clarity, functionality, and logical flow. Using DoctestCompared to unittest, using doctest is simpler and requires fewer steps. Despite its ease of use, it is important to exercise caution when utilizing a doctest, as it has certain limitations. Let's now demonstrate the previous example using doctest. Example - Now, we will perform the following operations in the terminal. We run the python doctest_employee -v command in the terminal and it returns the following output. Output - Trying: employee_1 = Employee("John", "Brad", 5000) Expecting nothing ok Trying: employee_2 = Employee("Tina", "Smith", 3000) Expecting nothing ok Trying: employee_1.employee_mail() Expecting: '[email protected]' ok Trying: employee_2.employee_mail() Expecting: '[email protected]' ok Trying: employee_1.employee_fullname() Expecting: 'John Brad' ok Trying: employee_2.employee_fullname() Expecting: 'Tina Smith' ok Trying: employee_1.apply_epf() Expecting: 4750 ok Trying: employee_2.apply_epf() Expecting: 2850 ok 5 items had no tests: __main__ __main__.employee.__init__ __main__.employee.apply_epf __main__.employee.employee_fullname __main__.employee.employee_mail 1 items passed all tests: 8 tests in __main__.employee 8 tests in 6 items. 8 passed and 0 failed. Test passed. As we can see in the above output, all tests are passed. The doctest makes it easy and more suitable for writing executable documentation for a package while the unittest is better suited for testing documentation. ConclusionThis tutorial included the difference between doctest and unittest. The unittest framework provides an object-oriented method for testing code, including features such as automation, setup, and tear down of test code. On the other hand, doctest is more appropriate for documentation as it can be embedded within the code's docstrings. Next TopicImage Filter with Python | OpenCV |
In this tutorial, we will learn how to convert the human language text into human-like speech. Sometimes we prefer listening to the content instead of reading. We can do multitasking while listening to the critical file data. Python provides many APIs to convert text to speech. The...
4 min read
Finding the index of an item makes sense since, in Python, lists' indexes are utilized to retrieve their elements. This could appear simple when working with a short list, but it might get tiresome as the list gets longer. Python includes a built-in method called index that...
4 min read
In data analysis and decision-making, statistics is essential, offering insights into the complex world of uncertainty and variability. From predicting stock market trends to understanding genetic traits, statistical distributions are the building blocks of modeling and analysis. One such distribution that plays a crucial role in...
9 min read
An expression's data type typically depends on type of its parameters. The majority of operators follow this requirement; for example, when adding two numbers, the outcome must also be an integer. Due to the two conflicting assumptions, this doesn't work out well in the situation of...
3 min read
This tutorial will discuss about an interesting project that involves complicated numbers by using Python. We will learn about fractals and make incredible artwork using an illustration of the Mandelbrot Set with Python's Matplotlib and Pillow libraries. We will also find out the process by...
14 min read
This article shows how to utilize Python to settle straightforward Laplace conditions with the Numpy library and Matplotlib to plot the arrangement of the situation. We'll likewise see that we can compose less code and accomplish more with Python. Presentation Laplace condition is a straightforward second-request incomplete differential...
9 min read
In this tutorial, we will learn about the toolz module, its function and how we can it solve complex programming language. The Toolz package offers many functions to work with lists, functions, and dictionaries. It adds more features to the library provided by the regular Python libraries...
14 min read
Librosa is valuable Python music and sound investigation library that helps programming designers to fabricate applications for working with sound and music document designs utilizing Python. This Python bundle for music and sound examination is essentially utilized when we work with sound information, like in the...
4 min read
If you are a developer, you may know that while working on any Python project or data science project, it is essential to always work in an environment that makes your project reusable and repeatable without creating an issue for anyone that picks up your project....
6 min read
Selenium is an effective automation tool widely used for net application trying out and net scraping. While Selenium provides diverse strategies to engage with web elements and navigate net pages, occasionally you want extra advanced functionality to obtain specific duties. One such superior feature is the...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India