Click here to Skip to main content
15,888,297 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is actually purpose of Assert in unit test and what if I write more than one Assert statement or no Assert statement.


What I have tried:

[TestMethod]
public void checkAddition()
{
Program objPrgm = new Program();
int res = objPrgm.add(4, 7);
Assert.AreEqual(11,res);
Assert.AreNotEqual(12, res);
}
With this code my unit test passed but even if I dont write Assert statement it passed too
Posted
Updated 23-May-17 22:20pm
v2
Comments
Richard MacCutchan 24-May-17 3:54am    
The Assert statement verifies the results. Without the Assert you have no idea whether the test succeeds or fails.

1 solution

Asserts are what is used to cause a test to fail, if none of your asserts cause the test to fail then the test is a success. So if you don't have any asserts at all the test is bound to pass (assuming no exceptions are raised as most test frameworks consider an exception to be a fail too unless you configure the test in a way that defines an exception as a pass). Obviously a test with no asserts has no real value though.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900