More Advanced Testing Scenarios
- Fixture is used which is nothing but the data that is created as an input and reusing them.
- Parameterization can be followed which is nothing but running the same test several times by passing different values and expecting the same result.
- Need to cover different scenarios like handling expected failures, writing integration tests, testing in multiple environments etc.
Below example shows how to write test for bad data type:
import unittest
class TestSumDifferentPatterns(unittest.TestCase):
def test_list_tuple_values(self):
# Summation of numbers present
# in a tuple value
data = (10*2, 200*2)
result = sum(data)
self.assertEqual(result, 600)
def test_bad_data_type(self):
data = "alpha value passed instead of numeric"
with self.assertRaises(TypeError):
result = sum(data)
if __name__ == '__main__':
unittest.main()
Output:
.F
======================================================================
FAIL: test_list_tuple_values (__main__.TestSumDifferentPatterns)
----------------------------------------------------------------------
Traceback (most recent call last):
File "......py", line 10, in test_list_tuple_values
self.assertEqual(result, 600)
AssertionError: 420 != 600
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
Getting Started With Unit Testing in Python
In Python, unit tests are the segments of codes that are written to test other modules and files that we refer to as a unit. Python Unit Testing is a very important part of the software development process that helps us to ensure that the code works properly without any errors. In this article, we will learn about Python Testing with the help of examples and proper explanations.
Table of Content
- What is Python Unit Testing?
- The Assert Statement
- Implementing Unit Tests | Unittest
- Implementing Unit Tests | Pytest
- Implementing Unit Tests | Nose
- Implementing Unit Tests | Doctest
- How to Write Assertions in Python Testing
- More Advanced Testing Scenarios
- Automating the Execution of Tests
Contact Us