Skip to content Skip to sidebar Skip to footer

Does Pytest Parametrized Test Work With Unittest Class Based Tests?

I've been trying to add parametrized @pytest.mark.parametrize tests to a class based unittest. class SomethingTests(unittest.TestCase): @pytest.mark.parametrize(('one', 'two'),

Solution 1:

According to pytest documentation:

unittest.TestCase methods cannot directly receive fixture function arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.

Solution 2:

There's a simple workaround to parameterize unittest-based Python tests by using "parameterized": https://pypi.org/project/parameterized/

Here's a simple example. First install "parameterized": pip install parameterized==0.8.1

import unittest
from parameterized import parameterized

classMyTestClass(unittest.TestCase):

    @parameterized.expand([
        ["One", "Two"],
        ["Three", "Four"],
        ["Five", "Six"],
    ])deftest_parameterized(self, arg1, arg2):
        print(arg1, arg2)

Now you can easily run your code with pytest

I've successfully used this technique to parameterize selenium browser tests that use the SeleniumBase framework on GitHub in this example.

Post a Comment for "Does Pytest Parametrized Test Work With Unittest Class Based Tests?"