我想知道是否有任何 BDD 风格的“描述它”Python 单元测试框架可以维护并准备好投入生产。我已经发现describe https://pypi.python.org/pypi/describe/0.1.2,但似乎没有维护,也没有文档。我也发现了sure http://falcao.it/sure达到了1.0,但似乎只是添加了语法糖,而不是编写断言。我真正想要的是类似于 RSpec 和 Jasmine 的东西,它使我能够设置测试套件。描述它语法允许测试函数的多种情况。与经典的断言结构相比,经典的断言结构测试每个函数一次,并具有多个断言来测试多种情况。这打破了单元测试的隔离性。如果有一种方法可以实现与断言式测试类似的效果,我将不胜感激有关如何做到这一点的任何建议。以下是两种风格的简单示例:
foo.py
class Foo():
def bar(self, x):
return x + 1
BDD 风格/描述它
测试_foo.py
describe Foo:
describe self.bar:
before_each:
f = Foo()
it 'returns 1 more than its arguments value':
expect f.bar(3) == 4
it 'raises an error if no argument is passed in':
expect f.bar() raiseError
单元测试/断言风格
测试_foo.py
class Foo():
def test_bar(x):
x = 3
self.assertEqual(4)
x = None
self.assertRaises(Error)
我自己一直在寻找这个并且遇到了mamba https://github.com/nestorsalceda/mamba。与 Fluent 断言库结合expects https://github.com/jaimegildesagredo/expects它允许您在 Python 中编写 BDD 风格的单元测试,如下所示:
from mamba import describe, context, it
from expects import *
with describe("FrequentFlyer"):
with context("when the frequent flyer account is first created"):
with it("should initially have Bronze status"):
frequentFlyer = FrequentFlyer()
expect(frequentFlyer.status()).to(equal("BRONZE"))
使用文档格式运行这些测试将为您提供类似 Jasmine 的测试报告:
> pipenv run mamba --format=documentation frequent_flyer_test.py
FrequentFlyer
when the frequent flyer account is first created
✓ it should initially have Bronze status
1 example ran in 0.0345 seconds
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)