是否有类似Spock的Python测试库

时间:2012-06-12 09:27:52

标签: python unit-testing

我和Spock合作并喜欢'where'子句,它允许您轻松地使用多个输入和输出来运行测试用例。例如:

class HelloSpock extends spock.lang.Specification {
    def "length of Spock's and his friends' names"() {
        expect:
            name.size() == length

        where:
            name     | length
            "Spock"  | 5
            "Kirk"   | 4
            "Scotty" | 6
    }
} 

Python有类似内容吗?

5 个答案:

答案 0 :(得分:4)

pytest允许你parametrise a test function

import pytest
@pytest.mark.parametrize(("input", "expected"), [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])
def test_eval(input, expected):
    assert eval(input) == expected

答案 1 :(得分:3)

如果您有多个测试,我会推荐像behave这样的BDD框架。您指定Gherkin syntax,例如(链接教程中的代码):

Scenario: some scenario
  Given a set of specific users
     | name      | department  |
     | Barry     | Beer Cans   |
     | Pudey     | Silly Walks |
     | Two-Lumps | Silly Walks |

 When we count the number of people in each department
 Then we will find two people in "Silly Walks"
  But we will find one person in "Beer Cans"

用于解析它的Python代码,例如:

@given('a set of specific users')
def step_impl(context):
    for row in context.table:
        model.add_user(name=row['name'], department=row['department'])

编写Python代码非常简单,并且在线有许多样板代码示例。这种技术的优点在于您的测试套件具有高度可重用性,非常容易被非程序员扩展。

答案 2 :(得分:2)

不,没有。这很难过,因为Spock非常优秀。我一直在寻找一年,并且考虑过如何在python中创建这样的DSL所需要的,因为我非常想念它。

Behave和Lettuce将为您提供BDD样式语法和习语,但您必须维护与您的方案文件匹配的单独步骤文件。显然,当您想要进行TDD但是具有BDD的可读性时,这很糟糕,而这正是Spock所能实现的。

如果你也想要Spock风格的模拟,那么Mox是我找到的最接近的。但是,一旦你被Spock宠坏了,它又是一个糟糕的替代品。

答案 3 :(得分:2)

是的,有!

我是Nimoy的作者,这是一个旨在成为Spock for Python的框架。

您可以创建数据驱动的测试:

from nimoy.specification import Specification

class MySpec(Specification):

    def my_feature_method(self):
        with given:
            a = value_of_a
            b = value_of_b

        with expect:
            (a * b) == expected_value

        with where:
            value_of_a | value_of_b | expected_value
            1          | 10         | 10
            2          | 20         | 40

您可以进行模拟:

from unittest import mock
from nimoy.specification import Specification

class MySpec(Specification):

    def my_feature_method(self):
        with setup:
            the_mock = mock.Mock()

        with when:
            the_mock.some_method() << [5, 6, 7]

        with then:
            the_mock.some_method() == 5
            the_mock.some_method() == 6
            the_mock.some_method() == 7

我们也有相当不错的模拟断言:

from unittest import mock
from nimoy.specification import Specification

class MySpec(Specification):

    def my_feature_method(self):
        with setup:
            the_mock = mock.Mock()

        with when:
            the_mock.some_method('abcd', True)

        with then:
            1 * the_mock.some_method('abcd', True)

答案 4 :(得分:0)

我还是Java / Groowy世界中Spock框架的忠实拥护者。在搜索中类似Python。在搜索中,我发现nimoy看起来很有希望。

来自官方页面的示例:

from nimoy.specification import Specification

class MySpec(Specification):

    def my_feature_method(self):
        with given:
            a = value_of_a
            b = value_of_b

        with expect:
            (a * b) == expected_value

        with where:
            value_of_a | value_of_b | expected_value
            1          | 10         | 10
            2          | 20         | 40

还有作者blog post为何出生。