Often, there is a need to run the same test multiple times, but with different arguments. It could be done from within the test, but then there is no way of running that test for just one set of arguments.
Often, there is a need to run the same test multiple times, but with different arguments. It could be done from within the test, but then there is no way of running that test for just one set of arguments.
.. code-block:: python
.. code-block:: python
# test_this1.py
import unittest
from parameterized import parameterized
from parameterized import parameterized
class TestMathUnitTest(unittest.TestCase):
class TestMathUnitTest(unittest.TestCase):
@parameterized.expand([
@parameterized.expand([
("negative", -1.5, -2.0),
("negative", -1.5, -2.0),
("integer", 1, 1.0),
("integer", 1, 1.0),
("large fraction", 1.6, 1),
("large fraction", 1.6, 1),
])
])
def test_floor(self, name, input, expected):
def test_floor(self, name, input, expected):
assert_equal(math.floor(input), expected)
assert_equal(math.floor(input), expected)
Now this test will be run three times, and you could run just the ``negative`` and ``integer`` sets of params with:
Now, by default this test will be run 3 times, each time with the last 3 arguments of ``test_floor`` being assigned the corresponding arguments in the parameter list.
and you could run just the ``negative`` and ``integer`` sets of params with:
.. code-block:: bash
.. code-block:: bash
...
@@ -512,11 +516,75 @@ Now this test will be run three times, and you could run just the ``negative`` a
...
@@ -512,11 +516,75 @@ Now this test will be run three times, and you could run just the ``negative`` a
or all but ``negative`` sub-tests, with:
or all but ``negative`` sub-tests, with:
.. code-block:: bash
.. code-block:: bash
pytest -k "not negative" tests/test_mytest.py
pytest -k "not negative" tests/test_mytest.py
Besides using the ``-k`` filter that was just mentioned, you can find out the exact name of each sub-test and run any or all of them using their exact names.
The module `parameterized <https://pypi.org/project/parameterized/>`__ which is already in the developer dependencies of ``transformers`` works for both: ``unittests`` and ``pytest`` tests.
If, however, the test is not a ``unittest``, you may use ``pytest.mark.parametrize`` (or you may see it being used in some existing tests, mostly under ``examples``).
Here is the same example, this time using ``pytest``'s ``parametrize`` marker:
.. code-block:: python
# test_this2.py
import pytest
@pytest.mark.parametrize(
"name, input, expected",
[
("negative", -1.5, -2.0),
("integer", 1, 1.0),
("large fraction", 1.6, 1),
],
)
def test_floor(name, input, expected):
assert_equal(math.floor(input), expected)
Same as with ``parameterized``, with ``pytest.mark.parametrize`` you can have a fine control over which sub-tests are run, if the ``-k`` filter doesn't do the job. Except, this parametrization function creates a slightly different set of names for the sub-tests. Here is what they look like: