@parameterized Test Example

Remember to install the parameterized module using pip before using it in your code.

Example

Here’s an example test to demonstrate how to use parameterization in
Python PyUnit tests:

  • Launch Eclipse IDE.
  • Create a Python script file with the .py extension
  • Add the following code.
  • Run the Python unit test. ( Run As >> Python unit-test )

 

# PyUnit Test Parameterization Example
# www.TestingDocs.com

import unittest
from parameterized import parameterized

class TestParameterization(unittest.TestCase):

    @parameterized.expand([
        (3, 2, 5),
        (5, 5, 10),
        (20, 16, 36)
    ])
    
    
    def test_addition(self, a, b, expected_result):
        result = a + b
        self.assertEqual(result, expected_result)

if __name__ == '__main__':
    unittest.main()

Screenshot

Screenshot in Eclipse IDE.

@parameterized Test Example

 

You can notice that the test runs three times for each test data.