parameterized Auxiliary library for Python unit tests. When you want to test the same method with different values, you don't have to write a different test method.
It supports unittest, pytest, nose, etc. The writing style is slightly different between unittest and pytest.
I've only used unittest, so how to use it with unittest.
install
pip install parameterized
usage
Just addition.
def add(a, b):
return a + b
import unittest
from parameterized import parameterized
class TestAdd1(unittest.TestCase):
@parameterized.expand([
(2, 3, 5),
(1, 1, 2),
(3, -1, 2)
])
def test_add(self, a, b, exp):
self.assertEqual(add(a, b), exp)
Values are passed to a, b, and exp, respectively. If you want to correspond to the keyword argument, do the following. Use the param function.
import unittest
from parameterized import parameterized, param
class TestAdd2(unittest.TestCase):
@parameterized.expand([
param(2, -2),
param(1, -1),
param(3, -1, exp=2)
])
def test_add(self, a, b, exp=0):
self.assertEqual(add(a, b), exp)
Recommended Posts