
- SpecFlow 教程
- SpecFlow - 首頁
- SpecFlow - 簡介
- 測試驅動開發
- 行為驅動開發
- SpecFlow - Visual Studio 安裝
- Visual Studio 擴充套件安裝
- SpecFlow - 專案設定
- 其他專案依賴項
- SpecFlow - 執行器啟用
- SpecFlow - HTML 報告
- SpecFlow - 繫結測試步驟
- SpecFlow - 建立第一個測試
- 配置 Selenium Webdriver
- SpecFlow - Gherkin
- SpecFlow - Gherkin 關鍵字
- SpecFlow - 特性檔案
- SpecFlow - 步驟定義檔案
- SpecFlow - Hook
- SpecFlow - 背景說明
- 使用示例進行資料驅動測試
- 不使用示例進行資料驅動測試
- 表格轉換為資料表
- 表格轉換為字典
- 使用 CreateInstance 的表格
- SpecFlow - 使用 CreateSet 的表格
- SpecFlow 有用資源
- SpecFlow - 快速指南
- SpecFlow - 有用資源
- SpecFlow - 討論
SpecFlow - 使用示例進行資料驅動測試
我們可以藉助關鍵字Examples執行資料驅動測試。我們還將藉助關鍵字Scenario Outline在多個值上執行相同的場景。
需要考慮的資料集將按順序傳遞到 Examples 部分下方,並以|符號分隔。因此,如果存在三行,我們將從單個場景執行三個測試用例。
Scenario Outline用於使用不同的資料集複製相同的場景。使用不同的值編寫相同的測試既麻煩又耗時。例如,

我們可以使用Scenario Outline將以上兩個場景組合起來。

因此,我們看到 Scenario Outline 應該與關鍵字Examples一起使用。對於 Examples 段下方出現的每一行,Scenario Outline 都將執行一次。
此外,我們還看到 Given 步驟具有<>分隔符。它指向 Examples 表的標題。SpecFlow 將在將步驟與步驟定義匹配的任務之前,將值放入此表中。
要驗證登入模組,我們需要執行以下步驟:
使用者輸入使用者名稱和密碼。
驗證使用者應該能夠登入。
我們將以上步驟合併到特性檔案中。
步驟 1:建立特性檔案
有關如何建立特性檔案的詳細資訊在“特性檔案”章節中進行了詳細討論。
Feature: User credential Scenario Outline: Login module Given user types <username> and <password> Then user should be able to login Examples: | username | password | | tutorialspoint1| pwd | | tutorialspoint2| pwd1 |
步驟 2:步驟定義檔案
有關如何建立步驟定義檔案的詳細資訊在“步驟定義檔案”章節中進行了詳細討論。
using System; using TechTalk.SpecFlow; namespace SpecFlowProject1.Features{ [Binding] public class UserCredentialSteps{ //regular expression used to point to data [Given(@"user types (.*) and (.*)")] public void GivenUserTypesUserAndPwds(string username, string password){ Console.WriteLine(username); Console.WriteLine(password); } [Then(@"user should be able to login")] public void ThenUserShouldBeAbleToLogin(){ Console.WriteLine("User should be able to login"); } } }
步驟 3:執行和結果
選擇使用者憑據(2),然後單擊“檢視”中的“執行所有測試”。

選擇登入模組、tutorialspoint1 場景,然後單擊“為此結果開啟其他輸出”連結。


場景已執行,使用者名稱為 tutorialspoint1,密碼為 pwd,如 Examples(第一行)中指定。
選擇登入模組、tutorialspoint2 場景,然後單擊“為此結果開啟其他輸出”連結。


測試已執行,使用者名稱為 tutorialspoint2,密碼為 pwd1,如 Examples(第二行)中指定。