The 'unittest' module
The `unittest` module is a built-in Python module that provides a framework for writing and running automated tests. It is based on the xUnit architecture and offers a set of tools to structure and organize your tests.
To use the `unittest` module, you need to create a test class that inherits from `unittest.TestCase`. Inside the test class, you define test methods that contain the actual test steps. Each test method should have a name that starts with `test_` to be recognized by the `unittest` runner.
For example :
Python
import unittest
def add_numbers(a, b):
return a + b
class TestAddNumbers(unittest.TestCase):
def test_positive_numbers(self):
result = add_numbers(3, 5)
self.assertEqual(result, 8)
def test_negative_numbers(self):
result = add_numbers(-2, -7)
self.assertEqual(result, -9)
if __name__ == '__main__':
unittest.main()

You can also try this code with Online Python Compiler
Run Code
Output
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
In this example, we have a function `add_numbers` that takes two numbers and returns their sum. We create a test class `TestAddNumbers` that inherits from `unittest.TestCase`. Inside the test class, we define two test methods: `test_positive_numbers` and `test_negative_numbers`. Each test method calls the `add_numbers` function with different inputs and uses the `assertEqual` method to check if the result matches the expected value.
To run the tests, we include the `if __name__ == '__main__':` block and call `unittest.main()`. This will discover and run all the test methods in the test class.
The `unittest` module provides several assert methods to check different conditions, such as `assertEqual`, `assertTrue`, `assertFalse`, `assertRaises`, and more. These assert methods help you verify the expected behavior of your code and detect any failures.
The "nose2" module
nose2 is a Python library that extends the functionality of the `unittest` module and provides additional features for test discovery, organization, and execution. It is compatible with tests written using the `unittest` framework and can be used as a drop-in replacement for the default test runner.
To use nose2, you need to install it using pip:
pip install nose2
Once installed, you can run your tests using the `nose2` command. nose2 will automatically discover and run all the tests in your project based on certain conventions, such as test files named `test_*.py` or `*_test.py`.
For example :
import unittest
def multiply_numbers(a, b):
return a * b
class TestMultiplyNumbers(unittest.TestCase):
def test_positive_numbers(self):
result = multiply_numbers(3, 5)
self.assertEqual(result, 15)
def test_zero_multiplication(self):
result = multiply_numbers(0, 7)
self.assertEqual(result, 0)
To run the tests using nose2, you can simply execute the following command in your terminal:
nose2
nose2 will discover and run the tests in the `TestMultiplyNumbers` class, providing a detailed test report with the results.
nose2 offers several features and plugins that enhance the testing experience, such as:
- Test discovery: nose2 automatically discovers tests based on naming conventions.
- Test filtering: You can selectively run tests based on patterns or attributes.
- Parallel execution: nose2 supports running tests in parallel to speed up the test execution.
- Plugins: nose2 has a plugin system that allows you to extend its functionality, such as generating HTML reports, measuring test coverage, and more.
The "pytest" module
pytest is a popular testing framework for Python that provides a simple and expressive way to write and run tests. It is known for its ease of use, flexibility, and extensive plugin ecosystem. pytest is not part of the Python standard library, so you need to install it separately using pip:
pip install pytest
Unlike `unittest`, pytest does not require you to create test classes. Instead, you can write test functions directly. pytest discovers and runs tests based on naming conventions, such as test files named `test_*.py` or `*_test.py` and test functions named `test_*`.
For example :
def divide_numbers(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def test_divide_numbers():
assert divide_numbers(10, 2) == 5
def test_divide_by_zero():
with pytest.raises(ValueError):
divide_numbers(10, 0)
In this example, we have a function `divide_numbers` that divides two numbers and raises a `ValueError` if the second number is zero. We define two test functions: `test_divide_numbers` and `test_divide_by_zero`. The `test_divide_numbers` function calls `divide_numbers` with valid inputs and uses the `assert` statement to check if the result is as expected. The `test_divide_by_zero` function uses the `pytest.raises` context manager to check if the `divide_numbers` function raises a `ValueError` when dividing by zero.
To run the tests using pytest, you can execute the following command in your terminal:
pytest
pytest will discover and run the test functions, providing a detailed test report with the results.
pytest offers several features that make testing more enjoyable and efficient, like:
- Fixture support: pytest provides a powerful fixture system that allows you to set up and tear down test dependencies easily.
- Parametrized tests: You can parametrize test functions to run with different sets of inputs, reducing code duplication.
- Assertion introspection: pytest provides detailed information about assertion failures, making it easier to debug failing tests.
- Plugins: pytest has a rich plugin ecosystem that extends its functionality, such as generating HTML reports, measuring test coverage, mocking, and more.
Frequently Asked Questions
Can I use Python's built-in unittest module for automation testing?
Yes, the unittest module is a built-in framework in Python that provides a solid foundation for writing and running automated tests. It offers a range of tools and assert methods to structure and verify test cases effectively.
What are some popular third-party Python testing frameworks?
Some popular third-party Python testing frameworks include pytest, nose2, and Robot Framework. These frameworks build upon the functionality of unittest and provide additional features, such as test discovery, parallel execution, and plugin support, to enhance the testing experience.
How can I run my Python automation tests on different browsers and platforms?
To run your Python automation tests on different browsers and platforms, you can use cloud-based testing platforms like Lambda test. These platforms allow you to execute your tests on a wide range of environments without the need to set up and maintain the infrastructure yourself.
Conclusion
In this article, we learned about Python automation testing and various modules and frameworks used for writing and running automated tests. We discussed the built-in unittest module, which provides a foundation for structuring and organizing tests. We also looked at popular third-party frameworks like nose2 and pytest, which offer additional features and benefits. Additionally, we covered Lambda test, a cloud-based testing platform that enables running tests on different browsers and platforms.
Recommended Readings: