Wednesday, June 22, 2016

How to fake tests (Part 3)

In this 3rd part of the series I want to show how assertions can be faked, so that not only lines and branches get covered but the test themselves also contain some assertions.
Faking Assertions only makes sense if a metric such as "assertions/test" is computed at all. Otherwise you may skip that part, because every proper code review would reveal your test as fake.
Test libraries such as Junit or TestNG contain various means for expressing assertions. In addition to this, some frameworks exist for that sole purpose, i.e. Hamcrest, Truth, to name a few. Basic approach for all is, to invoke the system under test (generating coverage information) and to verify outcomes against assertions.
But the outcomes doesn’t have to be related to what is declared as expected for the test to succeed. So all of the following assertions might do the trick
  • assertTrue(true);
  • assertNotNull(new Object()); (a real life example I’ve encountered during a code review)
  • assertEquals("2","2");

After having applied fake assertions and fake coverage, our testsuite satisfies the following criteria
  • Big, lots of tests for all the methods
  • 100% Line Coverage
  • 100% Condition Coverage
  • Tests contain assertions (maybe 1 assertion/method as a metric)
This would make every project manager happy, because the quality of the product is so good and there is proof for that....


 You’ve probably produced the most sophisticated test suite with best quality ratings with minimum effort to create that has no value at all (Achievement unlocked).

In the next post I'll show how all these fakes described in this and the earlier posts can be revealed as such -  and more important, how the effectiveness of a test suite can be determined and gaps in a sensible test suite be found. So stay tuned.
Post a Comment