Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Planning unit tests with TDD

Tags:

tdd

When you approach a class you want to write, how do you plan its unit tests?
Are there formal templates which you follow or do you use pen and paper/notepad?
I am looking for some way to let other programmers/QAs know what tests should be implemented (and if something was overlooked it can be easier to spot it).

like image 940
the_drow Avatar asked Apr 18 '11 12:04

the_drow


People also ask

Which types of tests should be considered when using TDD?

In layman's terms, Test Driven Development (TDD) is a software development practice that focuses on creating unit test cases before developing the actual code. It is an iterative approach that combines programming, the creation of unit tests, and refactoring.

Is TDD same as unit testing?

TDD is a broader concept than unit tests. TDD is a software development approach focused on understanding the problem domain and fulfilling the requirements. Bare unit tests are about validating the written source code and avoiding bugs and regression. In fact, unit tests are part of the TDD cycle.


2 Answers

With TDD, tests drive the feature you are writing. If you're needing to write formal templates for it, then chances are you're not entering into the spirit of things!

TDD should be used to generate the test cases as you write the code. Simply put, before you write the next line of code, encode in a test what the code should do.

Check out Bob Martin's bowling game example which should give you more of a feel for TDD.

like image 78
Jeff Foster Avatar answered Sep 20 '22 17:09

Jeff Foster


I do not think that having a template goes well with using TDD. I assume that you have read Kent Beck's Book on Test Driven Development by Example. If no please do so.

But the general idea is simple. When we start a class, we will have a general idea on the responsibility of the class. This is the steps that we use:

  1. Have a general idea on class responsibility and use that information to name the class.

  2. Create a test case for this class.

  3. If you start with a concrete information on what the units inside the class are, just write those stubs inside the class and write test cases for those stubs. Initially all of it will fail and the signatures for most of those methods will change. That's the whole idea.

  4. In most cases, the developer may not have that degree of information. In that case, it's OK to start writing code in the First Test. Once the test passes, then migrate the logic to the class.

So what I am driving at is, the whole point of TDD is to make the development process more organic. The class grows, with the knowledge on what it should do. Having a formal template, or writing things down, will probably not help.

The only thing I could think that you could do, is to sit with your developers before each iteration and come up with a pretty detailed idea on each of the component classes and its responsibilities (we only use this discussion to finalize public APIs).

If you want to know the quality of test cases written by your developer, then you can conduct an ad hoc code review to see if the classes are correctly broken down to units and all the units are tested.

like image 26
uncaught_exceptions Avatar answered Sep 19 '22 17:09

uncaught_exceptions