 
- SpecFlow 教程
- SpecFlow - 主页
- SpecFlow - 简介
- 测试驱动开发
- Behave驱动开发
- SpecFlow - Visual Studio 安装
- Visual Studio 扩展安装
- SpecFlow - 项目设置
- 其他项目依赖项
- SpecFlow - 运行器激活
- SpecFlow - HTML 报告
- SpecFlow - 结合测试步骤
- SpecFlow - 创建第一个测试
- 配置 Selenium Webdriver
- SpecFlow - 小Cucumber
- SpecFlow - 小Cucumber关键词
- SpecFlow - 特征文件
- SpecFlow - 步骤定义文件
- SpecFlow - 挂钩
- SpecFlow - 背景插图
- 数据驱动测试示例
- 没有示例的数据驱动测试
- 表转换为数据表
- 表转换为字典
- 带有 CreateInstance 的表
- SpecFlow - 带有 CreateSet 的表
- SpecFlow 有用资源
- SpecFlow - 快速指南
- SpecFlow - 有用的资源
- SpecFlow - 讨论
SpecFlow - 创建第一个测试
我们现在将在类库中创建一个文件,用于执行两个数字的减法。
using System;
namespace ClassLibrary2 {
   public class Class1 {
      public int Number1 { get; set; }
      public int Number2 { get; set; }
      public int Subtraction(){
         return Number1 - Number2;
      }
   }
}
特征文件实现
 
步骤定义文件实施
上述Feature文件对应的Step Definition文件,以及使用Class1来执行减法。
using ClassLibrary2;
using FluentAssertions;
using TechTalk.SpecFlow;
namespace SpecFlowCalculator.Specs.Steps { 
[Binding]
   public sealed class CalculatorStepDefinitions {
      private readonly ScenarioContext _scenarioContext;
      
      //instantiating Class1
      private readonly Class1 _calculator = new Class1();
      private int _result;
      public CalculatorStepDefinitions(ScenarioContext scenarioContext) {
         _scenarioContext = scenarioContext;
      }
      [Given("the first number is (.*)")]
      public void GivenTheFirstNumberIs(int number){
         _calculator.Number1 = number;
      }
      [Given("the second number is (.*)")]
      public void GivenTheSecondNumberIs(int number){
         _calculator.Number2 = number;
      }
      [When("the two numbers are subtracted")]
      public void WhenTheTwoNumbersAreSubtracted(){
         _result = _calculator.Subtraction();
      }
      [Then("the result should be (.*)")]
      public void ThenTheResultShouldBe(int result){
         _result.Should().Be(result);
      }
   }
}
执行测试
构建上述解决方案,然后从Test → Test Explorer获取构建成功消息后执行测试。
选择SpecFlowProject1功能并单击“在视图中运行所有测试”。
 
结果显示为1 Passed以及执行持续时间。单击选项打开此结果的附加输出以获取结果详细信息。
 
显示每个测试步骤的执行结果。
 
功能文件中的所有步骤均与完成状态一起执行。此外,步骤定义文件中的相应方法也会显示执行持续时间。