[英]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. 我一直在尝试将参数化的@pytest.mark.parametrize
测试添加到基于类的@pytest.mark.parametrize
测试中。
class SomethingTests(unittest.TestCase):
@pytest.mark.parametrize(('one', 'two'), [
(1, 2), (2, 3)])
def test_default_values(self, one, two):
assert one == (two + 1)
But the parametrized stuff didn't kick in : 但是参数化的东西没有起作用 :
TypeError: test_default_values() takes exactly 3 arguments (1 given)
I've switched to simple class based tests (without unittest). 我已经改用基于简单类的测试(没有单元测试)。 But I'd like to know if anyone tried it and it worked. 但是我想知道是否有人尝试过它并且有效。
According to pytest documentation : 根据pytest文档 :
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. unittest.TestCase方法不能直接接收fixture函数参数作为实现可能会导致运行通用unittest.TestCase测试套件的能力。
There's a simple workaround to parameterize unittest-based Python tests by using "parameterized": https://pypi.org/project/parameterized/ 有一个简单的解决方法,通过使用“参数化”来参数化基于单元测试的Python测试: https : //pypi.org/project/parameterized/
Here's a simple example. 这是一个简单的例子。 First install "parameterized": pip install parameterized==0.7.0
首先安装“参数化”: 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
现在,您可以使用pytest
轻松运行代码
I've successfully used this technique to parameterize selenium browser tests that use the SeleniumBase framework on GitHub in this example . 我已经成功地使用这种技术来参数化在这个例子中使用GitHub上的SeleniumBase框架的selenium浏览器测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.