C# 处理多个断言,如果未找到任何断言,则进行失败测试
我正在做一个否定的测试用例,测试是否弹出错误消息,然后检查模态对话框中的消息是否正确。问题是,该应用程序有两种语言:英语和德语,因此错误消息可以是这两种语言中的任何一种。到目前为止,我所拥有的:C# 处理多个断言,如果未找到任何断言,则进行失败测试,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我正在做一个否定的测试用例,测试是否弹出错误消息,然后检查模态对话框中的消息是否正确。问题是,该应用程序有两种语言:英语和德语,因此错误消息可以是这两种语言中的任何一种。到目前为止,我所拥有的: productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94 string modalMessage = productsPage.errorM
productsPage.CreateProduct("Test product-" + "2bd43cfd", "2bd43cfd", ".TEST Product-Buyer", "CC", "HRK", 40.94
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try
{
Assert.AreEqual("A product with identical service id and service provider already exists!", modalMessage);
test.Log(Status.Pass, "Error message '" + modalMessage + "' was found inside modal dialog.");
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
try
{
Assert.AreEqual("Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!", modalMessage);
}
catch (NUnit.Framework.AssertionException)
{
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
catch (Exception e)
{
test.Log(Status.Fail, "Test execution failed: " + e.ToString());
throw;
}
如您所见,我希望使用两个断言,因为消息可以是英语或德语,但问题是,如果这两个断言都不通过,我希望测试失败
我该怎么做呢?您可以使用方法来代替
您的代码如下所示:
Assert.True(modalMessage == "A product with identical service id and service provider already exists!" ||
modalMessage == "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!");
这样,您将同时检查它们。经验法则是始终知道在特定测试用例中检查什么。如果测试不清楚在特定时刻验证哪条消息(英语或德语),您需要记住,如果:
@Test
public void singleTestForBothLanguages() {
boolean isEnglishMode = isEnglishMode();
string expectedMessage;
if (isEnglishMode) {
expectedMessage = "A product with identical service id and service provider already exists!";
} else {
expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
}
checkAlert(expectedMessage);
}
//for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
public bool isEnglishMode() {
if ( ....){
return true;
}
return false;
}
public void CheckAlert(string expectedMessage) {
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try {
Assert.AreEqual(expectedMessage, modalMessage);
} catch (NUnit.Framework.AssertionException) {
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}
要测试单个功能单元,您应该能够控制(模拟)影响它的元素。因此,我希望找到一种方法,专门将语言设置为英语,进行测试,然后再设置为德语,并进行测试。
@Test
public void singleTestForBothLanguages() {
boolean isEnglishMode = isEnglishMode();
string expectedMessage;
if (isEnglishMode) {
expectedMessage = "A product with identical service id and service provider already exists!";
} else {
expectedMessage = "Produkt kann nicht erstellt werden! Ein Produkt mit identischer Dienst Id und Diensteanbieter ist bereits vorhanden!";
}
checkAlert(expectedMessage);
}
//for example check attribute of some element (maybe a flag? or whatever makes you sure to know which mode is on now)
public bool isEnglishMode() {
if ( ....){
return true;
}
return false;
}
public void CheckAlert(string expectedMessage) {
string modalMessage = productsPage.errorMsgServiceIdTaken.Text;
try {
Assert.AreEqual(expectedMessage, modalMessage);
} catch (NUnit.Framework.AssertionException) {
test.Log(Status.Warning, "The modal dialog did not contain message '" + modalMessage + "'.");
}
}