I'm used to write unit tests with assertions which are based on the input, e.g. (hopefully self-explanatory and let's assume that using random test data is fine)
int a = random();
int b = random();
Adder instance = new Adder();
int expResult = a+b;
int result instance.add(a, b);
assertEquals(expResult, result);
Let's assume that Adder.add
has a lot of side effects which I cannot imagine, so that this test would make sense.
Now, I encountered a situation where it would make sense to create assertions based on the output, e.g.
int a = random();
int b = random();
Multiplier instance = new Multiplier();
int result = instance.multiply(a, b);
if(isPrimeNumber(result)) {
assertTrue(a == 1 || b == 1);
}else {
//some other assertions...
}
Yes, this is a non-sense test and it tests more the functioning of rational numbers than anything else, but it illustrates the difference between basing assertions on input exclusively and making the output/test result influence assertions.
I'm assuming that I cover all possible distinct output states of the test - just like I'd assume that I'm covering all possible input states.
if .. else
one of the reason, why we have tests. Continuously using conditions in the tests will lead in the tests which fail for wrong reason or even worse - will pass, when it should fail. – Fabio Mar 03 '18 at 09:46