允许您使用多个数据集运行测试
项目描述
关于
Genty,发音为“gen-tee”,代表“生成测试”。它促进生成测试,其中单个测试可以执行多种输入。Genty使这一切变得简单。
例如,考虑一个包含要测试的代码和测试的文件sample.py
from genty import genty, genty_repeat, genty_dataset
from unittest import TestCase
# Here's the class under test
class MyClass(object):
def add_one(self, x):
return x + 1
# Here's the test code
@genty
class MyClassTests(TestCase):
@genty_dataset(
(0, 1),
(100000, 100001),
)
def test_add_one(self, value, expected_result):
actual_result = MyClass().add_one(value)
self.assertEqual(expected_result, actual_result)
使用默认的unittest运行器运行MyClassTests
$ python -m unittest -v sample
test_add_one(0, 1) (sample.MyClassTests) ... ok
test_add_one(100000, 100001) (sample.MyClassTests) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
无需为各种测试用例编写多个独立的测试,可以使用genty进行重构和参数化!
它生成可读的测试。它生成可维护的测试。它生成富有表达力的测试。
另一个选项是多次运行相同的测试。这在压力测试或寻找竞态条件时很有用。这个特定的测试
@genty_repeat(3)
def test_adding_one_to_zero(self):
self.assertEqual(1, MyClass().add_one(0))
将运行3次,输出如下
$ python -m unittest -v sample
test_adding_one() iteration_1 (sample.MyClassTests) ... ok
test_adding_one() iteration_2 (sample.MyClassTests) ... ok
test_adding_one() iteration_3 (sample.MyClassTests) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
这两种技术可以结合使用
@genty_repeat(2)
@genty_dataset(
(0, 1),
(100000, 100001),
)
def test_add_one(self, value, expected_result):
actual_result = MyClass().add_one(value)
self.assertEqual(expected_result, actual_result)
还有更多选项可以探索,包括命名数据集和genty_args。
@genty_dataset(
default_case=(0, 1),
limit_case=(999, 1000),
error_case=genty_args(-1, -1, is_something=False),
)
def test_complex(self, value1, value2, optional_value=None, is_something=True):
...
将运行3个测试,输出如下
$ python -m unittest -v sample
test_complex(default_case) (sample.MyClassTests) ... ok
test_complex(limit_case) (sample.MyClassTests) ... ok
test_complex(error_case) (sample.MyClassTests) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.003s
OK
@genty_datasets可以链接在一起。例如,如果存在语义上不同的数据集,那么保持它们分开将有助于表达。
@genty_dataset(10, 100)
@genty_dataset('first', 'second')
def test_composing(self, parameter_value):
...
将运行4个测试,输出如下
$ python -m unittest -v sample
test_composing(10) (sample.MyClassTests) ... ok
test_composing(100) (sample.MyClassTests) ... ok
test_composing(u'first') (sample.MyClassTests) ... ok
test_composing(u'second') (sample.MyClassTests) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
有时测试的参数在模块加载时无法确定。例如,某些测试可能基于某些http请求的结果。首先,测试需要认证等。这可以通过如下使用@genty_dataprovider装饰器来实现
def setUp(self):
super(MyClassTests, self).setUp()
# http authentication happens
# And imagine that _some_function is actually some http request
self._some_function = lambda x, y: ((x + y), (x - y), (x * y))
@genty_dataset((1000, 100), (100, 1))
def calculate(self, x_val, y_val):
# when this is called... we've been authenticated
return self._some_function(x_val, y_val)
@genty_dataprovider(calculate)
def test_heavy(self, data1, data2, data3):
...
将运行4个测试,输出如下
$ python -m unittest -v sample
test_heavy_calculate(100, 1) (sample.MyClassTests) ... ok
test_heavy_calculate(1000, 100) (sample.MyClassTests) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
注意这里如何将辅助程序(calculate)的名称添加到两个执行的测试用例的名称中。
和@genty_dataset一样,@genty_dataprovider也可以链接在一起。
享受吧!
延迟参数化
参数化测试,其中最终参数直到测试执行时间才确定。它看起来是这样的
@genty_dataset((1000, 100), (100, 1))
def calculate(self, x_val, y_val):
# when this is called... we've been authenticated, perhaps in
# some Test.setUp() method.
# Let's imagine that _some_function requires that authentication.
# And it returns a 3-tuple, matching that signature of
# of the test(s) decorated with this 'calculate' method.
return self._some_function(x_val, y_val)
@genty_dataprovider(calculate)
def test_heavy(self, data1, data2, data3):
...
calculate()方法根据@genty_dataset装饰器被调用2次,其返回值定义了将传递给方法test_heavy(...)的最终参数。
安装
要安装,只需
pip install genty
贡献
设置
创建一个虚拟环境和安装包 -
mkvirtualenv genty
pip install -r requirements-dev.txt
测试
使用 - 运行所有测试
tox
tox测试包括通过pep8和pylint进行代码风格检查。
tox测试配置为在Python 2.6、2.7、3.3、3.4、3.5和PyPy 2.6上运行。
版权和许可
Copyright 2015 Box, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://apache.ac.cn/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。