[英]How do i combine the Multiple Assertion with MSTEST (Specflow)
下面是我的测试的断言我如何在一行代码中组合所有断言
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Show menu"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Summary"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Encounter"));
}
假设
ObjectRepository.phPage.GetMenuList()
返回IEnumerable<string>
首先,我们需要创建一个我们期望在“MenuList”中拥有的项目集合以及我们实际拥有的项目
var expectedItems = new List<string> { "Show menu", "Patient Summary", "Patient Encounter" };
var actualItems = ObjectRepository.phPage.GetMenuList();
现在,您可以根据需要选择两个选项:
1. 您想检查“MenuList”是否包含这 3 个项目(但不仅仅是那些)
CollectionAssert.IsSubsetOf(expectedItems, actualItems);
2. 你想检查“MenuList”是否只包含那 3 个项目(没有别的)
CollectionAssert.AreEquivalent(expectedItems, actualItems);
如果您希望您的断言在其中一个失败时继续运行,您可以使用 Specflow 多个断言:
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
Assert.Multiple(() =>
{
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Show menu"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Summary"));
Assert.IsTrue(ObjectRepository.phPage.GetMenuList().Contains("Patient Encounter"));
});
}
如果你真的只想要一个 Assert,从技术上讲,你可以这样做:
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
var menuItems = ObjectRepository.phPage.GetMenuList();
Assert.IsTrue(menuItems.Contains("Show menu") && menuItems.Contains("Patient Summary") && menuItems.Contains("Patient Encounter"));
}
但是我相信当测试失败时,您将无法准确了解缺少的菜单项。 你现在已经拥有的方式是有道理的。
如果您只想在测试方法中具有可读性,则可以引入一个私有方法,该方法包含所有断言,例如
public void ThenICanSeeTheFunctionlitiesForNONtransitionUserAsClnician()
{
AssertMenuListItems(ObjectRepository.phPage.GetMenuList());
}
private void AssertMenuListItems(TypeOfGetMenuList items)
{
Assert.IsTrue(items.Contains("Show menu"));
Assert.IsTrue(items.Contains("Patient Summary"));
Assert.IsTrue(items.Contains("Patient Encounter"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.