简体   繁体   中英

MS Test assertion checking

I have wrote a test method using MS Test. I have written two assert for testing purpose. I have made the test case failed intentionally but I don't know which part of its failed.

public void CalculatorConstructorTest()
        {
            Calculator concreteObject = new Calculator();

            Assert.IsTrue(concreteObject == null, "Construction failed");
            Assert.IsTrue(concreteObject.Total == 0, "Value should be initially 0");
        }

As in the first assertion it will fail. Also I have maded Total greater than 0. So the second one will be also failed. But say one of them is valid. So how do I identify for which assert, my test case failed?

Thanks in advance.

There are many ways to do so:

Debugging

Try debugging the Test instead of just running it. This will cause an exception beeing raised at the failing assertion.

Inspecting Assertion Messages

You already povided some messages to your assertions, those will be visible in the test-explorers details-section after selecting the failed test. This is making it easier to determine which test-case failed (see screenshot below).

在此处输入图片说明

One Assertion per Testcase

As pointed out in the comments, try to have just one assertion per test-case where ever it is possible. This way its obvious which assertion failed simply because there is only one within a testcase.

Use fitting Assertions

Try to have more fitting assertions. In your case, you might try

    Assert.IsNull(concreteObject); 
    Assert.AreEqual(0, concreteObject.Total); 

which will also tell you which of the two assertions failed by providing more precise automated messages as seen in the following screenshot:

在此处输入图片说明

However, the screenshot shows points out very well that even though we have 2 Assertions in the testcase, only one is marked as failed. The second one would also fail, but since the test is stopped at the first failing assertion, we'll not see that until the first assertion succeeds. This is why I would always go for one assertion per test-case .

尝试调试测试以查看哪个部分失败。

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