Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Cases AND assertion statements

The code in this question made me think

assert(value>0); //Precondition
if (value>0)
{
  //Doit
}

I never write the if-statement. Asserting is enough/all you can do. "Crash early, crash often"

CodeComplete states:

  • The assert-statement makes the application Correct
  • The if-test makes the application Robust

I don't think you've made an application more robust by correcting invalid input values, or skipping code:

assert(value >= 0 );  //Precondition
assert(value <= 90);  //Precondition
if(value < 0)         //Just in case
  value = 0;
if (value > 90)       //Just in case
  value = 90;
//Doit

These corrections are based on assumptions you made about the outside world. Only the caller knows what "a valid input value" is for your function, and he must check its validity before he calls your function.

To paraphrase CodeComplete: "Real-world programs become too messy when we don't rely solely on assertions."

Question: Am I wrong, stuborn, stupid, too non-defensive...

like image 319
jan Avatar asked Sep 11 '08 09:09

jan


People also ask

What are assertions in test cases?

An assertion is a boolean expression at a specific point in a program which will be true unless there is a bug in the program. A test assertion is defined as an expression, which encapsulates some testable logic specified about a target under test.

What does assertion mean in testing?

At the basic level, an assertion is just a Boolean expression. It contains a true and false binary. The expression is placed into the testing program and pertains to a certain section of the software being tested. The assertion itself encompasses an expression that describes the logic of the code under test.

What are the examples of assertion?

A basic assertion is a straightforward statement that expresses a belief, feeling, opinion, or preference. For example: “I would like to finish this email before we have our conversation.” or “I would like you to wait until I have finished speaking.”


2 Answers

The problem with trusting just Asserts, is that they may be turned off in a production environment. To quote the wikipedia article:

Most languages allow assertions to be enabled or disabled globally, and sometimes independently. Assertions are often enabled during development and disabled during final testing and on release to the customer. Not checking assertions avoiding the cost of evaluating the assertions while, assuming the assertions are free of side effects, still producing the same result under normal conditions. Under abnormal conditions, disabling assertion checking can mean that a program that would have aborted will continue to run. This is sometimes preferable. Wikipedia

So if the correctness of your code relies on the Asserts to be there you may run into serious problems. Sure, if the code worked during testing it should work during production... Now enter the second guy that works on the code and is just going to fix a small problem...

like image 114
Tnilsson Avatar answered Sep 19 '22 17:09

Tnilsson


Use assertions for validating input you control: private methods and such.

Use if statements for validating input you don't control: public interfaces designed for consumption by the user, user input testing etc.

Test you application with assertions built in. Then deploy without the assertions.

like image 28
Daren Thomas Avatar answered Sep 18 '22 17:09

Daren Thomas