
- Specflow 教程
- Specflow - 主頁
- Specflow - 介紹
- 測試驅動開發
- 行為驅動開發
- Specflow - Visual Studio 安裝
- Visual Studio 擴充套件安裝
- Specflow - 專案設定
- 其他專案依賴項
- Specflow - 執行器啟用
- Specflow - HTML 報告
- Specflow - 繫結測試步驟
- Specflow - 建立第一個測試
- 配置 Selenium Webdriver
- Specflow - Gherkin
- Specflow - Gherkin 關鍵字
- 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; } } }
功能檔案實現

步驟定義檔案實現
以上功能檔案的相應步驟定義檔案,以及使用 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 功能並單擊 在檢視中執行所有測試。

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

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

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