📅  最后修改于: 2023-12-03 15:13:38.520000             🧑  作者: Mango
BDD (Behavior Driven Development),行为驱动开发。
BDD 是一种敏捷软件开发过程,旨在改善软件开发的沟通,协作和软件质量。它强调软件开发人员,QA 和非技术人员之间的协作,以确保软件的行为与业务期望一致。
BDD 的好处如下:
以下是一些有用的 BDD 资源。
Behave 是一个 Python 框架,它提供了一些特性,使得编写 BDD 测试变得更加容易。
安装:
pip install behave
示例:
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
from behave import given, when, then
@given('I have entered {number:d} into the calculator')
def step_impl(context, number):
context.calculator = Calculator()
context.calculator.push(number)
@when('I press add')
def step_impl(context):
context.calculator.add()
@then('the result should be {result:d} on the screen')
def step_impl(context, result):
assert context.calculator.value() == result
Cucumber 是一个基于 BDD 的工具,支持多种编程语言和平台。
安装:
gem install cucumber
示例:
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
Given("I have entered {int} into the calculator") do |number|
@calculator = Calculator.new
@calculator.push(number)
end
When("I press add") do
@calculator.add
end
Then("the result should be {int} on the screen") do |result|
expect(@calculator.value).to eq result
end
SpecFlow 是一个基于 Cucumber 的 .NET 框架,支持多种 .NET 开发语言。
安装:
nuget install SpecFlow
示例:
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
[Binding]
public class AdditionSteps
{
private readonly Calculator calculator = new Calculator();
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int number)
{
calculator.Push(number);
}
[When(@"I press add")]
public void WhenIPressAdd()
{
calculator.Add();
}
[Then(@"the result should be (.*) on the screen")]
public void ThenTheResultShouldBeOnTheScreen(int result)
{
Assert.AreEqual(calculator.Value(), result);
}
}
Gherkin Linter 是一个用于验证 Gherkin 文件语法的命令行工具。
安装:
npm install -g gherkin-lint
使用:
gherkin-lint my_feature.feature
BDD 能够大大改善软件开发的沟通和协作,从而提高软件的质量和稳定性。使用 BDD 工具和框架能够更好地编写测试,并帮助发现需求和系统设计方面的问题。以上是一些常用的 BDD 工具和框架,希望对你有所帮助。