未知
bdd的Python项目详细描述
简介
用于将BDD功能转换为python unittest.testcase类的库。运行 使用您最喜欢的测试运行器进行测试,如pytest、unittest或nose。 请参阅demo文件夹以获取示例。
示例
我们以计算器为例。 创建一个名为calculator.feature的纯文本文件,其中包含BDD样式 功能描述:
Feature: Basic math operations Test addition Scenario: add Given the value 10 When adding 7 Then the result is 17 Scenario: add a negative value Given the value 19 When adding -11 Then the result is 8
这是您可以与客户讨论并达成一致的文本。 接下来编写一个名为test_calculator.py的备份文件,其中包含 运行此功能:
import os from bdd import Environment from calculator import Calculator env = Environment() env.context.calculator = Calculator() @env.given('the value {:d}') def step(context, value): context.calculator.value = value @env.when('adding {:d}') def step(context, value): context.calculator.add(value) @env.then('the result is {:d}') def step(context, value): assert context.calculator.value == value def rel(p): this_dir = os.path.dirname(os.path.abspath(__file__)) return os.path.join(this_dir, p) CalculatorTestCase = env.load_feature_as_testcase('calculator.feature')
最后,编写实际的生产代码来实现计算器,在 名为calculator.py的文件:
class Calculator: def __init__(self): self.value = 0 def add(self, value): self.value += value
现在使用pytest:
$ python -m pytest test_calculator.py -v =========== test session starts =========================== platform linux -- Python 3.5.1, pytest-2.9.1 collected 2 items test_calculator.py::CalculatorTestCase::test_scenario_0 <- bdd.py PASSED test_calculator.py::CalculatorTestCase::test_scenario_1 <- bdd.py PASSED
参考文献
真正好的python bdd库是behave。