Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Unit Test would you start with?

When you've come up with an overall design / idea for how a part of a system should work, how do you decide where to start when doing TDD, or rather, how do you decide your first test to start with?

like image 724
Sekhat Avatar asked Jan 15 '09 13:01

Sekhat


People also ask

When should I start unit testing?

Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object.

Should I write unit tests first?

It often makes sense to write the test first and then write as much code as needed to allow the test to pass. Doing this moves towards a practice known as Test-Driven Development (TDD). Bluefruit uses a lot of TDD because it helps us to build the right product without waste and redundancies.


1 Answers

Lets assume I'm coding a class called Oven to bake my delicious Pie objects. This is how I step through the unit-test order:

  1. What do I need to do to instantiate the object? In this case it would most likely be Oven oven = new Oven(); No test for this one, I suppose.
  2. How do I prepare the object for use? oven.turnOn(int degrees) sounds good, I'll do that. How do I check it? Better make oven.getTemperature(). There's an obvious test.
  3. Okay, oven is now hot enough and I want to bake my Pie. For that I need oven.bake(Pie p) so I'll make that. But now what? I want to check if the pie is ready but rather than having oven.isPieReady() I think that oven.pastryStatus() which returns things like "nothing in oven", "raw", "almost done", "cooked" and "charred" sounds good and in general should be more extendable than oven.isPieReady() so I'll do that.

And so on and so forth. So, I'll make my tests in order I expect to use the object refining the specification as I go. In the end I usually end up with rather simple yet powerful API which does what I want. After I've unit tested my API, I run coverage on my code to see what I missed and then add extra tests for those.

like image 76
Esko Avatar answered Sep 23 '22 22:09

Esko