Python > Testing in Python > Unit Testing with `unittest` > Running Tests
Basic Unit Testing with unittest
This snippet demonstrates how to write and run basic unit tests in Python using the unittest
module. It includes setting up test cases, defining test methods, and running the tests to verify the functionality of a simple function.
Defining a Simple Function
This code defines a simple function add(x, y)
that takes two numbers as input and returns their sum. This is the function we will be testing.
def add(x, y):
"""Adds two numbers together."""
return x + y
Creating a Test Case
Here, we create a test case class TestAddFunction
that inherits from unittest.TestCase
. Inside this class, we define several test methods, each starting with the prefix test_
. These methods use assertion methods like assertEqual
to verify that the function add
behaves as expected for different inputs. Each test function is designed to test different scenario.
import unittest
class TestAddFunction(unittest.TestCase):
def test_add_positive_numbers(self):
self.assertEqual(add(2, 3), 5)
def test_add_negative_numbers(self):
self.assertEqual(add(-1, -1), -2)
def test_add_mixed_numbers(self):
self.assertEqual(add(5, -2), 3)
def test_add_zero(self):
self.assertEqual(add(0, 5), 5)
Running the Tests
This code block ensures that the tests are run only when the script is executed directly (not imported as a module). unittest.main()
discovers and runs all the test cases defined in the module.
if __name__ == '__main__':
unittest.main()
Concepts Behind the Snippet
This snippet illustrates the core principles of unit testing: isolating individual units of code (in this case, a function) and verifying their correctness through automated tests. The unittest
framework provides the tools for creating test cases, defining test methods, and running the tests.
Real-Life Use Case Section
Imagine you are building a complex calculator application. Before integrating the add
function into the larger application, you would use unit tests like these to ensure that the function is working correctly in isolation. This helps to catch bugs early in the development process and prevents them from propagating to other parts of the application.
Best Practices
Interview Tip
When discussing unit testing in an interview, be prepared to explain the benefits of testing (e.g., catching bugs early, improving code quality, facilitating refactoring), the basic concepts of unit testing (e.g., test cases, test methods, assertions), and the tools and techniques you have used for testing in Python (e.g., unittest
, pytest
, mock objects).
When to Use Them
Unit tests should be written for every function and method that implements a distinct feature. Especially useful for functions implementing complex logic.
Alternatives
pytest
is a popular alternative to unittest
, offering a more concise and flexible syntax. doctest
allows you to embed test cases directly within docstrings.
Pros
unittest
is included with Python, so no additional installation is required.unittest
is a mature and widely used testing framework.
Cons
unittest
can require more boilerplate code compared to other testing frameworks like pytest
.unittest
may be less flexible than some other frameworks in terms of test discovery and configuration.
FAQ
-
How do I run tests from the command line?
You can run tests from the command line by navigating to the directory containing your test file and running the commandpython -m unittest
..py -
How do I skip a test?
You can skip a test using the@unittest.skip
decorator or theself.skipTest()
method within the test method.