bjn*_*ier 13 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
}
}
Run Code Online (Sandbox Code Playgroud)
Python有类似的东西吗?
noa*_*amt 11
就在这里!
我是Nimoy的作者——一个旨在成为 Python 的 Spock 的框架。
您可以创建数据驱动的测试:
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
Run Code Online (Sandbox Code Playgroud)
您可以进行模拟:
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
Run Code Online (Sandbox Code Playgroud)
我们也有非常模拟的断言:
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)
Run Code Online (Sandbox Code Playgroud)
pytest可以参数化测试功能:
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
Run Code Online (Sandbox Code Playgroud)
我也非常喜欢 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
Run Code Online (Sandbox Code Playgroud)
还有作者博客文章为什么它诞生了。
| 归档时间: |
|
| 查看次数: |
1726 次 |
| 最近记录: |