目錄
介紹
編寫測試可能很無聊,並且利益相關者可以知道您的軟件應如何運行。
SpecFlow可以爲您提供幫助。這是用於BDD的框架,它使用Visual Studio的擴展程序將用戶要求的功能轉換爲要測試的代碼。
SpecFlow允許您用母語編寫行爲,並以Excel表格等形式顯示數據。
安裝SpecFlow
您必須在測試項目中安裝Visual Studio擴展。安裝此程序包,nuget SpecFlow, SpecFlow.Tools.MsBuild.Generation這將從我們的IDE中生成代碼,並且SpecFlow.xUnit(如果您使用xUnit)將允許Visual Studio查找specFlow將自動生成的測試。
背景
假設您想通過在線購買爲水果和蔬菜倉庫創建測試。
但是,請小心正確地分割這些部分,這就是我想繼續討論的內容。
背景是您收集每個測試場景共有的所有信息的部分。例如,用戶列表。
Feature: Order and warehouse testing
Background
Given registered users
| UserId | Name | Surname | Mail | Delivery address | City |
| AJ | John | Red | [email protected] | Down street | London |
| MWitch | Marck | Witch | [email protected] | High street | New york |
如果過於詳細,則背景部分可能太大,您只需要針對多種情況編寫所需內容即可。
我們可以添加一個@tag ,它允許我們收集特性,就好像它們是在一個名稱空間中一樣。
@Orders
Scenario: An order is submitted
Given, When, Then
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 |
或其他情況:
@Order
Scenario: An order is placed that lowers the quantity of the products under the threshold
Given 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>order> = new <List><order>();
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(IEnumerable<order> orders)
{
var orderResult = new OrderResult();
foreach (var order in orders)
{
var product = sessionManager.Query<product>()
.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<Product>();
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將找到測試並將其顯示在“測試資源管理器”中,您可以在其中運行它們。
示例代碼在GitHub上。