BDD - SpecFlow ExternalData Plugin 导入外部测试数据
创始人
2024-04-17 06:10:32
0

BDD - SpecFlow ExternalData Plugin 导入外部测试数据

  • 引言
  • SpecFlow ExternalData 插件
    • 支持的数据源
    • Tags
  • 实践
    • 创建一个 Class Libary Project
    • 添加 NuGet Packages
    • 添加测试数据源文件
      • CSV 文件
      • Excel 文件
    • 添加 Feature 文件
    • 实现 Step Definition
    • 执行
      • Scenario 导入测试数据源
      • Scenario Outline 导入测试数据源
      • 重命名数据源中的列名
      • Excel 指定 worksheet

引言

在设计 BDD Scenarios 时,有时会用到大量的测试数据,或是多个 Scenarios 共享这些大量数据,如果将这些数据都列在 Sceanrios 中,会使得 Scenario 非常庞大,大量重复的数据快也使得 Feature 文件非常庞大,数据行非常长等,导致可读性差,不够简洁。

这时我们就会思考能不能将数据放在某个文件中,Scenarios 中的参数可以跟这些外部数据关联起来。非常棒的是 SpecFlow 可以做到,支持加载外部数据源,并且非常方便地将这加载的数据导入到 Scenarios 中。

SpecFlow ExternalData 插件

利用 SpecFlow ExternalData 插件,可以实现将测试数据和测试 Scenarios 分开,并且 Scenarios 之间可以重用这些测试数据。这个插件支持最低版本 SpecFlow 3.5.5 起。

支持的数据源

  1. CSV 文件(格式 ‘CSV’, 扩展名 .csv)

Note: 标准的 RFC 4180 CSV 格式是带一行 header line,也就是表头 (ExternalData 插件利用 CsvHelper 来解析这类文件).

  1. Excel 文件(格式 Excel, 扩展名 .xlsx, .xls, .xlsb)

Note: XLSX 和 XLS 都是支持的 (ExternalData 插件利用 ExcelDataReader 来解析这类文件).

注意:SpecFlow ExternalData 某些版本可能支持 JSON 格式的文件,可参考官网实例 ExternalDataSample,但是这个插件后面被重写了,最新版本不再支持 JSON 文件加载导入到 Scenarios 中,具体参考 Issue #2559

Tags

@DataSource:path-to-file
这个 tag 主要是指明数据来源,可以加到 Scenario 或 Scenario Outline 上。

注意: 这个路径是基于 Feature 文件所在 folder 的相对路径。也就是说数据源文件应该放在 Feature 文件夹范围内,不能 Feature 文件夹范围外其它地方,不然编译会出错。

@DisableDataSource
@DataSource tag 可以加在 Feature 节点上,将所有 scenarios 转化成 scenario outlines。当整个 Feature 文件都用到相同的外部数据源时,这种方式非常有用。@DisableDataSource 用于少数 Scenarios 不使用这个 tag 在 Feature 节点的外部数据源。

@DataFormat:format
这个 tag 仅用在文件的扩展名不能被识别。

@DataSet:data-set-name
这个 tag 只应用于 Excel 文件。 默认是指定第一个 worksheet,当有多个 worksheet,就可以用这个 tag 指明特定的 worksheet。

@DataField:name-in-feature-file=name-in-source-file
这个 tag 用于 “重命名” 外部数据源的列名

tags 汇总:

  • Tags 可加在 Feature,scenario, scenario outline 或 scenario outline examples 上
  • Tags 可从 Feature 节点上继承,但是可覆盖父类 tag 或者用 @DisableDataSource 弃用数据源。
  • Tags 不能包含空格,通常用下划线 (_) 代表一个空格。目前不支持访问访问一个文件名或路径含有空格的文件

实践

创建一个 Class Libary Project

在这里插入图片描述

添加 NuGet Packages

都是最新版本:
SpecFlow 3.9.74
SpecRun.SpecFlow 3.9.31

SpecFlow.ExternalData 3.9.74

FluentAssertions 6.8.0

在这里插入图片描述

添加测试数据源文件

为了区分数据源 path,我们将准备两个数据源,一个直接放在 Feature 文件夹下,一个放在 Feature 文件夹下的子文件夹下。

CSV 文件

Feature/TestData 目录下添加一个 CSV 文件 products.csv

在这里插入图片描述

在这里插入图片描述

Excel 文件

Feature 目录下添加一个 Excel 文件 products.xlsx

在这里插入图片描述

在这里插入图片描述

添加 Feature 文件

Products.feature

Feature: Products@DataSource:TestData/products.csv
Scenario: The basket price is calculated correctly (csv)Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €@DataSource:TestData/products.csv
Scenario Outline: Valid product prices are calculated (csv Outline)	Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €Examples: | product    | price || Cheesecake | 2.0   |@DataSource:TestData/products.csv @DataField:product-name=product @DataField:price-in-EUR=price
Scenario: The basket price is calculated correctly (csv renamed fields)	Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €@DataSource:products.xlsx @DataSet:other_products
Scenario: The basket price is calculated correctly for other productsGiven the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €

在这里插入图片描述

实现 Step Definition

ProductBindings.cs
这里我们只是想知道 Scenarios 中的参数和测试文件的联系,所以只实现了部分 step,保证 Scenarios 能跑起来就 OK。

using System;
using TechTalk.SpecFlow;namespace ExternalData.Steps
{[Binding]public class ProductBindings{[Given(@"the price of (.*) is €(.*)")]public void GivenThePriceOfProductIsPrice(string product, float price){Console.WriteLine($"product:{product}");Console.WriteLine($"price:{price}");}[Given(@"the customer has put (.*) piece of (.*) in the basket")]public void GivenTheCustomerHasPutPieceOfProductInTheBasket(int number, string product){}[When(@"the basket price is calculated")]public void WhenTheBasketPriceIsCalculated(){}[Then(@"the basket price should be €(.*)")]public void ThenTheBasketPriceShouldBePrice(string price){}}
}

在这里插入图片描述

执行

Scenario 导入测试数据源

这个 Scenario 导入了 TestData/products.csv 文件的数据,注意文件路径
@DataSource:TestData/products.csv

@DataSource:TestData/products.csv
Scenario: The basket price is calculated correctly (csv)Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €

products.csv 中有三行数据,所以 Scenario 会自动转换成 Scenario outline

在这里插入图片描述

在这里插入图片描述
执行其中一个 Scenario,Step 中的 参数和 CSV 列名对应上了,自动关联起来了。

Given the price of  is €
 [Given(@"the price of (.*) is €(.*)")]public void GivenThePriceOfProductIsPrice(string product, float price){Console.WriteLine($"product:{product}");Console.WriteLine($"price:{price}");}

在这里插入图片描述

Scenario Outline 导入测试数据源

@DataSource:TestData/products.csv
Scenario Outline: Valid product prices are calculated (csv Outline)	Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €Examples: | product    | price || Cheesecake | 2.0   |

生成了 4 个 Scenarios,其中绿色部分是 Scenario 中 Example 中的数据,黄色高亮是 CSV 中三行数据生成的。

在这里插入图片描述

重命名数据源中的列名

@DataField:product-name=product @DataField:price-in-EUR=price 通过这种方式将 Scenario 中的参数名和数据源中的列名映射起来。

@DataSource:TestData/products.csv @DataField:product-name=product @DataField:price-in-EUR=price
Scenario: The basket price is calculated correctly (csv renamed fields)	Given the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €

在这里插入图片描述

Excel 指定 worksheet

Excel 有三个 worksheet,其中 other_products 有两行测试数据。

在这里插入图片描述

@DataSet:other_products 通过 @DataSet 来指定特定的 worksheet

@DataSource:products.xlsx @DataSet:other_products
Scenario: The basket price is calculated correctly for other productsGiven the price of  is €And the customer has put 1 piece of  in the basketWhen the basket price is calculatedThen the basket price should be €

生成了 2 条 Scenarios,并且参数能够自动关联起来。

在这里插入图片描述

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...