Specflow - 建立第一個測試



我們現在將在類庫中建立一個執行兩數相減的檔案。

using System;
namespace ClassLibrary2 {
   public class Class1 {
      public int Number1 { get; set; }
      public int Number2 { get; set; }
      public int Subtraction(){
         return Number1 - Number2;
      }
   }
}

功能檔案實現

File Implementation

步驟定義檔案實現

以上功能檔案的相應步驟定義檔案,以及使用 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);
      }
   }
}

執行測試

構建以上解決方案,在從 測試 → 測試資源管理器 中獲取構建成功訊息後執行測試。

選擇 SpecFlowProject1 功能並單擊 在檢視中執行所有測試

SpecFlowProject

結果顯示為 1 個透過,並顯示執行時間。單擊選項 為此結果開啟其他輸出 以獲取結果詳細資訊。

Execution Duration

顯示每個測試步驟的執行結果。

Execution Result

功能檔案中的所有步驟均已執行,並顯示“已完成”狀態。此外,步驟定義檔案中的相應方法會顯示執行時間。

廣告