- SpecFlow 教程
- SpecFlow - 首页
- SpecFlow - 简介
- 测试驱动开发 (Test Driven Development)
- 行为驱动开发 (Behavior Driven Development)
- SpecFlow - Visual Studio 安装
- Visual Studio 扩展安装
- SpecFlow - 项目设置
- 其他项目依赖
- SpecFlow - 运行器激活
- SpecFlow - HTML 报告
- SpecFlow - 绑定测试步骤
- SpecFlow - 创建第一个测试
- 配置 Selenium Webdriver
- SpecFlow - Gherkin
- SpecFlow - Gherkin 关键字
- SpecFlow - 功能文件 (Feature File)
- SpecFlow - 步骤定义文件 (Step Definition File)
- SpecFlow - Hooks
- SpecFlow - 背景图示
- 使用 Examples 进行数据驱动测试
- 不使用 Examples 进行数据驱动测试
- 表格转换为数据表
- 表格转换为字典
- 使用 CreateInstance 的表格
- SpecFlow - 使用 CreateSet 的表格
- SpecFlow 有用资源
- SpecFlow - 快速指南
- SpecFlow - 有用资源
- SpecFlow - 讨论
SpecFlow - 背景图示
Background 关键字用于在功能文件中的所有场景之前复制相同的步骤。我们可以通过将这些步骤组合在 Background 部分下,将其移至背景。
它有助于为场景添加上下文。它可以包含多个 Given 步骤。因此,它将在执行每个场景之前执行,但在任何 Before hooks之后执行。
Background 位于第一个 Example 或 Scenario 之前,缩进级别相同。简而言之,它用于声明所有测试的公共步骤。
在上面的示例中,有两个场景,Background 步骤将在每个场景执行之前运行一次。
Background 规则
让我们描述一些应用 Background 时的规则:
除非我们被迫将应用程序带到需要执行复杂步骤的状态(根据项目利益相关者的要求),否则应将其用于定义简单的步骤。
它应该简洁而现实。
所有场景也应该简明扼要。
Background 示例
让我们来看一个示例,其中我们使用了 Background 步骤,这些步骤将在功能文件中的所有测试之前执行。例如,要为应用程序添加普通用户和管理员用户,我们需要在执行“普通用户添加”场景之前运行以下步骤:
启动应用程序 URL。
提交用户名和密码
步骤 1:创建功能文件
关于如何创建功能文件的详细信息在“功能文件”章节中有详细讨论。
Feature: Member addition
Background:
Given launch URL
Then enter name and password
Scenario: Normal user addition
Given user is on normal user addition screen
When enters normal user details
Then user should be added as normal user
Scenario: Admin user addition
Given user is on admin user addition screen
When enters admin user details
Then user should be added as admin user
步骤 2:创建步骤定义文件
关于如何创建步骤定义文件的详细信息在“步骤定义文件”章节中有详细讨论。
using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.Features{
[Binding]
public class MemberAdditionSteps{
[Given(@"launch URL")]
public void GivenLaunchURL(){
Console.WriteLine("Url launched");
}
[Given(@"user is on normal user additon screeen")]
public void GivenUserIsOnNormalUserAdditonScreeen(){
Console.WriteLine("User is on normal user addition screen");
}
[Given(@"user is on admin user addition screen")]
public void GivenUserIsOnAdminUserAdditionScreen(){
Console.WriteLine("User is on admin user addition screen");
}
[When(@"enters normal user details")]
public void WhenEntersNormalUserDetails(){
Console.WriteLine("User enters normal user details");
}
[When(@"enters admin user details")]
public void WhenEntersAdminUserDetails(){
Console.WriteLine("User enters admin user details");
}
[Then(@"enter name and password")]
public void ThenEnterNameAndPassword(){
Console.WriteLine("User enters name and password");
}
[Then(@"user should be added as normal user")]
public void ThenUserShouldBeAddedAsNormalUser(){
Console.WriteLine("User should be added as normal user");
}
[Then(@"user should be added as admin user")]
public void ThenUserShouldBeAddedAsAdminUser(){
Console.WriteLine("User should be added as admin user");
}
}
}
步骤 3:执行和结果
选择 SpecFlowProject(2),然后单击 Run All Tests in View。
选择 普通用户添加场景,然后单击 Open additional output for this result link。
在上面的输出中,Background 步骤 - Given Url launched 和 Then enter name and password 在实际的普通用户场景之前执行。
选择 管理员用户添加 功能,然后单击 Open additional output for this result 链接。
在上面的输出中,Background 步骤 - Given Url launched 和 Then enter name and password 也在实际的管理员用户场景之前执行。