gra*_*fic 28 python unit-testing pytest
我一直在尝试将参数化@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)
Run Code Online (Sandbox Code Playgroud)
但是参数化的东西没有起作用:
TypeError: test_default_values() takes exactly 3 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
我已经改用基于简单类的测试(没有单元测试).但是我想知道是否有人尝试过它并且有效.
有一个简单的解决方法可以使用“参数化”参数化基于单元测试的 Python 测试:https : //pypi.org/project/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)
Run Code Online (Sandbox Code Playgroud)
现在您可以轻松地运行您的代码 pytest
在这个例子中,我已经成功地使用这种技术来参数化使用GitHub 上的SeleniumBase框架的selenium 浏览器测试。
归档时间: |
|
查看次数: |
3747 次 |
最近记录: |