# C# 使用SpecFlow创建BDD测试用例

``````namespace CalBmi
{
public class BmiCalculator
{
public Decimal Height { get; set; }

public Decimal Weight { get; set; }

public Decimal Bmi()
{
throw new NotImplementedException();
}
}
}
``````

``````Feature: Calculator
Simple calculator for adding **two** numbers

@mytag
Given the first number is 50
And the second number is 70
When the two numbers are added
Then the result should be 120
``````

``````Feature: 计算BMI

@mytag
Scenario: 根据身高体重计算BMI
Given 身高1.75米
And 体重70.00公斤
When 计算BMI
Then 结果应该是22.86
``````

``````using System;
using TechTalk.SpecFlow;

namespace TestBmi.StepDefinitions
{
[Binding]
public class 计算BMIStepDefinitions
{
[Given(@"身高(.*)米")]
public void Given身高米(Decimal p0)
{
throw new PendingStepException();
}

[Given(@"体重(.*)公斤")]
public void Given体重公斤(Decimal p0)
{
throw new PendingStepException();
}

[When(@"计算BMI")]
public void When计算BMI()
{
throw new PendingStepException();
}

[Then(@"结果应该是(.*)")]
public void Then结果应该是(Decimal p0)
{
throw new PendingStepException();
}
}
}
``````

``````private readonly BmiCalculator _bmical = new BmiCalculator();
``````

``````using CalBmi;
using System;
using TechTalk.SpecFlow;

namespace TestBmi.StepDefinitions
{
[Binding]
public class 计算BMIStepDefinitions
{
private readonly BmiCalculator _bmical = new BmiCalculator();
private decimal _result;

[Given(@"身高(.*)米")]
public void Given身高米(Decimal p0)
{
_bmical.Height = p0;
}

[Given(@"体重(.*)公斤")]
public void Given体重公斤(Decimal p0)
{
_bmical.Weight=p0;
}

[When(@"计算BMI")]
public void When计算BMI()
{
_result=_bmical.Bmi();
}

[Then(@"结果应该是(.*)")]
public void Then结果应该是(Decimal result)
{
_result.Should().Be(result);
}
}
}
``````

``````namespace CalBmi
{
public class BmiCalculator
{
public Decimal Height { get; set; }

public Decimal Weight { get; set; }

public Decimal Bmi()
{
return Weight/Height/Height;
}
}
}
``````

``````        public Decimal Bmi()
{
return System.Decimal.Round(Weight /Height/Height,2);
}
``````

原文作者：寻找无名的特质
原文地址: https://www.cnblogs.com/zhenl/p/16380928.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。