In a software development process, certain steps must be taken to ensure that your software module is delivered as bug free as possible. The aim is for it to be working without any bugs. However, the question is what bug free is exactly. One possible definition is the software module is consider bug free if it fulfills all its requirements, functional and non-functional. Beyond that definition, it is hard to control the injection of bugs from outer sources. For example, if the compiler is not generating the code and causes a mis-behaviour of the software module. Technically, this is a bug for the product and will still to be fixed, but it is hard to control if the behaviour of the compiler is not well known.
So with that definition, the steps in the process are clear. First,
the requirements are finalised, and then the implementation can start.
Since the requirements are finalised, it would make sense for the test
code to be written first. From the requirements, the functional
behaviour of the software module is well defined. This means that the
input data into the software module is known as well as the results from
it. If these are not clear or have some doubts, it is a sign that the
requirements are not clear enough. The requirements need more work.
Once the test code are written and executed. Obviously, all the test
cases will fail as the code for the software module is not written yet.
Now is the write time to start writing your code for the software
module. As you continue to write the software module, the test cases are
used as a validation for the software module. The implementation
continues until all your test cases passes.
Suppose the code for software module is written before the test code.
The downside is that the chance of getting your software module correct
is low and you won’t know it until you have some test code to do the
testing. You have just invested into some technical debt. If your test
code is written first, it is very likely to be correct the first time as
all the inputs, all the outputs and the behaviours are well defined.
Save time and effort by writing the test code first, and then write the code for your software module.
I am writing a Guide to Successful Unit Tests.
You can get it here at Leanpub. or here at Gumroad. and read about these topics and more.