Page 1 of 1

inline testing versus unit testing

Posted: Thu Jan 17, 2008 3:41 pm
by alex.barylski
When I first started in C I was quickly introduced to the assert macro...when I made the transition into Windows development and started using MFC the ASSERT macro was a quick replacement.

These are handy little (debug mode only) macros which you use to assert the pre and post states of a methods under test.

Code: Select all

function doSomething($object, $value)
{
  ASSERT($object instanceOf MyClass);
  ASSERT($value > 0 && <= 10);
 
  // ...
 
  $result = $stuff_above;
  ASSERT($result != 15);
 
  return $result;
}
 
This was my style of testing until I switched to PHP because there is no native ASSERT macro which is removed during release. I started gaining interest in TDD and most recently BDD to basically accomplish the same effect with the following advantages:
  • Externalizing your tests makes for a form of 'hot to use' API documentation
  • Speeds up the production code by removing test-centric code and also clears the production code. Lots of testing code leads to obfuscated source
  • Externalizing tests allows arbitrarty number of test input and output. Making it easy to add or remove additional tests without touching source code
  • Portability & Reuse. The odd chance you have a similar API in the future, you possibly reuse your tests
  • Frameworks for automation, generating nice displays, etc
I am already sold on the idea of unit testing and keeping tests code separate...however what I would like to know is are there any other advantages of unit testing over to inline testing other than the four points I mentioned above. What does unit testing offer over inline testing???

Opinions, input, etc???

Cheers :)

Re: inline testing versus unit testing

Posted: Fri Jan 18, 2008 8:41 am
by Maugrim_The_Reaper
http://devzone.zend.com/article/2772-An ... ing-in-PHP

About two thirds down the page ;).

But outside the long version it's down to approach. In-line testing is added as the code is written, while TDD and BDD advocate writing tests/specs before writing any code, so that you establish a set of behaviours guiding in what you should code. Since you know before starting what you're out to accomplish, and have a reasonable API to pin any implementation on, in theory it should speed up the process, improve your approach, and force you to confront the underlying problem being solved without any distractions.

Re: inline testing versus unit testing

Posted: Sat Jan 19, 2008 9:52 am
by Ollie Saunders
There is a legitimate reason for using inline tests still. You might want to look up "design by contract" or "contract driven development" which I think is the official name for what you were talking about in C.
Portability & Reuse. The odd chance you have a similar API in the future, you possibly reuse your tests
I doubt that's ever going to happen. But here's on benefit that wasn't mentioned: External unit tests can be used to gain control over some untested legacy code allowing you to change and improve it and better understand how it works.