Parameterized

0.9.0 · active · verified Sun Apr 05

Parameterized is a Python library that provides parameterized testing capabilities for various test frameworks like unittest, pytest, and nose. It simplifies writing data-driven tests by allowing the same test logic to be run with multiple sets of input data, reducing duplication and improving test coverage. The current version is 0.9.0, released in March 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `parameterized` with Python's built-in `unittest` framework. It shows examples for parameterizing individual test methods using `@parameterized` and `@parameterized.expand`, and how to parameterize an entire test class using `@parameterized_class`.

import unittest
from parameterized import parameterized, param
import math

class TestMath(unittest.TestCase):

    @parameterized([
        (2, 2, 4),
        (2, 3, 8),
        (1, 9, 1),
        (0, 9, 0),
    ])
    def test_pow(self, base, exponent, expected):
        self.assertEqual(math.pow(base, exponent), expected)

    @parameterized.expand([
        ("negative", -1.5, -2.0),
        ("integer", 1, 1.0),
        ("large fraction", 1.6, 1),
    ])
    def test_floor(self, name, input_val, expected):
        self.assertEqual(math.floor(input_val), expected)

    @parameterized_class(('a', 'b', 'expected_sum'), [
        (1, 2, 3),
        (5, 5, 10),
    ])
    class TestMathClass(unittest.TestCase):
        def test_add(self):
            self.assertEqual(self.a + self.b, self.expected_sum)

# To run these tests, you would typically use:
# unittest.main(argv=['first-arg-is-ignored'], exit=False)
# or a test runner like pytest/nose.

view raw JSON →