本文共 5147 字,大约阅读时间需要 17 分钟。
目录
编写测试可能很无聊,并且利益相关者可以知道您的软件应如何运行。
SpecFlow可以为您提供帮助。这是用于BDD的框架,它使用Visual Studio的扩展程序将用户要求的功能转换为要测试的代码。
允许您用母语编写行为,并以Excel表格等形式显示数据。
您必须在测试项目中安装Visual Studio扩展。安装此程序包,nuget SpecFlow, SpecFlow.Tools.MsBuild.Generation这将从我们的IDE中生成代码,并且SpecFlow.xUnit(如果您使用xUnit)将允许Visual Studio查找specFlow将自动生成的测试。
假设您想通过在线购买为水果和蔬菜仓库创建测试。
但是,请小心正确地分割这些部分,这就是我想继续讨论的内容。
背景是您收集每个测试场景共有的所有信息的部分。例如,用户列表。
Feature: Order and warehouse testingBackground Given registered users | UserId | Name | Surname | Mail | Delivery address | City || AJ | John | Red | j.red@red.com | Down street | London || MWitch | Marck | Witch | Mark.Witch@gl.it | High street | New york |
如果过于详细,则背景部分可能太大,您只需要针对多种情况编写所需内容即可。
我们可以添加一个@tag ,它允许我们收集特性,就好像它们是在一个名称空间中一样。
@OrdersScenario: An order is submitted
Given:我们描述要测试的动作的先例的位置:
Given The warehouse| Code | Products | Quantity | Unit of measure | Alert threshold || P1 | Tomato | 150 | Box | 25 || V1 | Wine | 350 | Bottle | 40 |
When:我们将触发我们要测试的代码的动作放在何处:
When An order arrives| User | Product | Quantity || AJ | P1 | 2 || AJ | V1 | 1 |
Then:我们将代码运行时需要发生的所有事情放在哪里。
Then The warehouse contains these products| Code | Product | Quantity || P1 | Tomato | 148 || V1 | Wine | 349 |Then the Purchasing Office is notified| Product under threshold | Quantity | Threshold |
或其他情况:
@OrderScenario: An order is placed that lowers the quantity of the products under the thresholdGiven The warehouse| Code | Products | Quantity | Unit of measure | Alert threshold || P1 | Tomato | 26 | Box | 25 || V1 | Wine | 350 | Bottle | 40 |When An order arrives| Users| Products | Quantity || AJ | P1 | 2 || AJ | V1 | 1 |Then The warehouse contains these products| Code | Products | Quantity || P1 | Tomato | 24 || V1 | Wine | 349 |Then the Purchasing Office is notified| Products under threshold | Quantity | Threshold || P1 | 24 | 25 |
现在,您必须将表绑定到一段代码。
在这里,我们来帮助扩展Visual Studio的规范流程,它将转换为我们输入的所有Given,When,Then方法。从右键单击specflow文件,选择“生成步骤定义”。
您将需要使用它来保存显然是虚构的数据,并使用一个内存数据库,该数据库将为您填充Given。
[Given(@"registered users")]public void GivenregisteredUsers(Table table) { foreach (var row in table.Rows) { sessionManager.AddRecord( new User { UserId = row["UserId"], Name = row["Name"], Surname = row["Surname"], DeliveryCity = row["City"], DeliveryAddress = row["Delivery address"], Mail = row["Mail"] }); } }}
When 将响应一个代码,该代码将调用我们用于下订单的方法:
[When(@"An order arrives")]public void WhenAnOrderArrives(Table table){ OrderCore core = new OrderCore(sessionManager); List<order> = new
(); foreach (var row in table.Rows) { order.Add( new Order { User = row["User"], Product = row["Products"], Quantity = Convert.ToDecimal(row["Quantity"]), }); } result = core.AcceptOrder(order);}
使用代码:
public OrderResult AcceptOrder(IEnumerableorders){ var orderResult = new OrderResult(); foreach (var order in orders) { var product = sessionManager.Query () .Single(x => x.Code == order.Product); product.Quantity = product.Quantity - order.Quantity; sessionManager.SaveOrUpdate(product); if (product.Quantity < product.Threshold) orderResult.AlertThresholds.Add( new OrderResult.AlertThreshold { product = product.Name, Quantity = product.Quantity, Threshold = product.Threshold }); } return orderResult;}
在Then中,我们将放置一些代码来检查是否已产生所需的行为。
[Then(@"The warehouse contains these products")]public void ThenTheWarehouseContainsTheseProducts(Table table){ var products = sessionManager.Query(); foreach (var row in table.Rows) { var product = products.Where(x => x.Code == row["Code"]).Single(); Assert.That(product.Quantity == Convert.ToDecimal(row["Quantity"])); }}[Then(@"the Purchasing Office is notified")]public void ThenThePurchasingOfficeIsNotified(Table table){ if (table.Rows.Count == 0) Assert.That(result.AlertThresholds.Count() == 0); else { Assert.That(result.AlertThresholds.Count() == table.Rows.Count); foreach (var row in table.Rows) { var product = result.AlertThresholds .SingleOrDefault(x => x.product == row["Products under threshold"]); Assert.That(product != null); Assert.That(product.Quantity == Convert.ToDecimal(row["Quantity"])); Assert.That(product.Threshold == Convert.ToDecimal(row["Threshold"])); } }}
在构建解决方案时,Visual Studio将找到测试并将其显示在“测试资源管理器”中,您可以在其中运行它们。
示例代码在。
转载地址:http://qbwhj.baihongyu.com/