Does pytest parametrized test work with unittest class based tests?

筅森魡賤 提交于 2019-12-03 04:25:44

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.

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.7.0

import unittest
from parameterized import parameterized

class MyTestClass(unittest.TestCase):

    @parameterized.expand([
        ["One", "Two"],
        ["Three", "Four"],
        ["Five", "Six"],
    ])
    def test_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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!