蟒蛇快速执行
paycheck的Python项目详细描述
工资单是 ScalaCheck,它本身就是 实施 QuickCheck用于 哈斯克尔。paycheck对于定义 功能 应该这样做,而不是测试给定输入的结果。
感谢gcross的一些more recent changes
安装支票
<code> sudo easy_install paycheck </code>
就这样。开始吧。
一个简单的例子
让我们从Scalacheck那里偷一个例子。这是绳子 功能 转移到工资单上。看看怎么回事?我们正在定义 参数,然后传递该类型的值 致 功能。
<code> import unittest from paycheck import with_checker class TestStrings(unittest.TestCase): """ More-or-less a direct port of the string testing example from the ScalaCheck doc at: http://code.google.com/p/scalacheck/ """ @with_checker(str, str) def test_starts_with(self, a, b): self.assertTrue((a+b).startswith(a)) @with_checker(str, str) def test_ends_with(self, a, b): self.assertTrue((a+b).endswith(b)) # Is this really always true? @with_checker(str, str) def test_concat(self, a, b): self.assertTrue(len(a+b) > len(a)) self.assertTrue(len(a+b) > len(b)) @with_checker(str, str) def test_substring2(self, a, b): self.assertEquals( (a+b)[len(a):], b ) @with_checker(str, str, str) def test_substring3(self, a, b, c): self.assertEquals((a+b+c)[len(a):len(a)+len(b)], b) if __name__ == '__main__': unittest.main() </code>
然后测试一下。您可能会看到一个问题:
<code> $ python test_strings.py F.... ====================================================================== FAIL: test_concat (__main__.TestStrings) ---------------------------------------------------------------------- Traceback (most recent call last): File "paycheck/checker.py", line 11, in wrapper test_func(self, *v) File "test_strings.py", line 20, in test_concat self.assertTrue(len(a+b) > len(a)) AssertionError: Failed for input ('UGzo2LP<(9Gl_*o*GH$H<+{wPiNk?', '') ---------------------------------------------------------------------- Ran 5 tests in 0.051s FAILED (failures=1) </code>
正如所预测的,test_concat已经爆炸了;注意薪水是不错的 足以告诉你是哪些输入导致了问题。在这种情况下,我们 请查看空字符串的property test_concat失败,这是由 因为我们使用了“>;”而不是“>;=”。
嵌套和更复杂的类型
容器和嵌套类型被指定为paycheck“类比”。 也就是说,与标量类型不同,标量类型是通过指定 根据您的需要,容器类型是通过创建非空 所需类型的容器,其中包含 希望生成的容器包含的类型;请注意 包含的元素本身允许是一个容器,允许 任意嵌套类型。工资单会从你的集装箱里推断出来 您想要的容器类型和包含的元素 生成。这包括字典:它将首先查看 它看到的键/值映射,并从中推断键类型和 值类型。将生成0到之间的容器 paycheck.generator.list_len元素。
下面的示例说明了这是如何工作的:
<code> import unittest from paycheck import with_checker class TestTypes(unittest.TestCase): @with_checker(int) def test_int(self, i): self.assertTrue(isinstance(i, int)) @with_checker([int]) def test_get_list(self, list_of_ints): self.assertTrue(isinstance(list_of_ints, list)) for i in list_of_ints: self.assertTrue(isinstance(i, int)) @with_checker([{str: int}]) def test_list_of_dict_of_int_string(self, list_of_dict_of_int_string): self.assertTrue(isinstance(list_of_dict_of_int_string, list)) for dict_of_int_string in list_of_dict_of_int_string: self.assertTrue(isinstance(dict_of_int_string, dict)) for key, value in dict_of_int_string.items(): self.assertTrue(isinstance(key, str)) self.assertTrue(isinstance(value, int)) if __name__ == '__main__': unittest.main() </code>