简体   繁体   中英

Is there an alternative to accomplish what Assert does and have the test fully execute when it fails without using a Try Catch?

I am writing tests for UI and am using assert statements to check if an element is present with string for the message when the assert fails. I would like the test to continue to check for other elements even if it fails to find an element. I would also like to have the useful message that shows when the asserts fails to be there at the end of the test.

Assert.IsTrue(IsElementPresent(By.XPath("//a/img[@src='/Img1.png']")), "Img1 not present");
Assert.IsTrue(IsElementPresent(By.XPath("//a/img[@src='/Img2.png']")), "Img2 not present");
Assert.IsTrue(IsElementPresent(By.XPath("//a/img[@src='/Img3.png']")), "Img3 not present");

Is there an alternative to using assert statements or having the assert statement in a try catch for the test to continue?

You could hold the error message and then check at the end. The below is horrible code and you would want to tidy it up, but it gives an idea of what you could do.

[Test]
public void All_images_are_present()
{
    string message = string.Empty;

    message = AssertImage("Img1");
    message += AssertImage("Img2");
    message += AssertImage("Img3");
    ... etc ...

    Assert.That(message, Is.Empty, message);
}

private static string AssertImage(string imageName)
{
    string imagePath = string.Format(@"//a/img[@src='/{0}.png']", imageName);

    if (IsElementPresent(By.XPath("//a/img[@src='/Img1.png']")))
        return string.Empty;

    return string.Format("{0} not present;");    
}

However, it might be better to have three different tests and check each image in each one. The tests should ideally check one piece of functionality each.

In programming, we call that an " if statement ". Assert.IfTrue(condition, message) is merely a shorthand form for:

if (condition) then
    print message
    abort_execution
end-if

The best way for your solution would be using parametrized tests. In NUnit, you can make such by using [TestCase] attribute.

Following example is rewritten to parametrized tests:

[TestCase("//a/img[@src='/Img1.png']")]
[TestCase("//a/img[@src='/Img2.png']")]
[TestCase("//a/img[@src='/Img3.png']")]
public void ElementIsPresent(string xpath)
{
    Assert.IsTrue(IsElementPresent(By.XPath(xpath)));
}

Now, you are able to distinguish two types of red tests: ones in which expectations differs (here: if false is returned instead of true) and those which throws an exception in the way.

There is also another problem with your approach. As Andy Nichols mentioned, you are testing more than one logical assert in one test. Unit test should have no more than one logical assert per test. Having parametrized tests as per above guarantee you are testing one logical assert in one test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM