Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In TDD, should tests be written by the person who implemented the feature under test? [closed]

We run a project in which we want to solve with test driven development. I thought about some questions that came up when initiating the project. One question was: Who should write the unit-test for a feature? Should the unit-test be written by the feature-implementing programmer? Or should the unit test be written by another programmer, who defines what a method should do and the feature-implementing programmer implements the method until the tests runs?
If I understand the concept of TDD in the right way, the feature-implementing programmer has to write the test by himself, because TDD is procedure with mini-iterations. So it would be too complex to have the tests written by another programmer?
What would you say? Should the tests in TDD be written by the programmer himself or should another programmer write the tests that describes what a method can do?

like image 718
martin Avatar asked Apr 07 '10 16:04

martin


People also ask

Why does TDD want you to write the tests before the code?

The simple concept of TDD is to write and correct the failed tests before writing new code (before development). This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests.

What is the rule of a test driven development?

Uncle Bob describes TDD with three rules: - You are not allowed to write any production code unless it is to make a failing unit test pass. - You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.

When using TDD Why does a newly written unit test always fail?

This is the one and only correct answer because the idea of TDD is that you only implement code for which you have a failing test. It prevents you from implementing things you do not yet need. This comes from one of the ExtremeProgramming practises YAGNI.


7 Answers

It could be done both ways, you could write the unit test yourself, or go for the ping pong approach where you take turns with another developer writing unit tests and writing the implementation if you are pairing. The right solution is the one that works for you and your team. I prefer to write the test myself, but I know others that have had luck with the ping pong approach as well.

like image 74
NerdFury Avatar answered Oct 27 '22 09:10

NerdFury


In TDD the developer first writes the unit tests that fail and then fixes the production code to make the test pass. The idea is that the changes are made in really small steps - so you write a test that calls a method that doesn't exist, then you fix the test by adding an empty method, then you add some assertion to the test about the method so it fails again, then you implement the first cut of the method, etc. Because these steps are so small it is not practical to have a separate person write the tests. On the other hand I would recommend pairing, so that you gain some additional eyeballs making sure the code makes sense.

I think it would be possible to have another person/team/or even client (when you use tools like Fitness) to write acceptance tests, that test the whole functionality on a higher level.

like image 43
Grzenio Avatar answered Oct 27 '22 09:10

Grzenio


The Unit Test should be written prior to coding and test that a Unit meets the requirements, therefore it should be fine for the developer implementing the code to also write the Unit Test.

like image 29
Justin Niessner Avatar answered Oct 27 '22 11:10

Justin Niessner


I think you need to separate Automated Unit Testing from Test Driven Development. (IMHO it's not just you who should make a vital distinction here).

AUT strongly recommends, TDD requires test to be written first.

TDD furthermore makes the test an essential part of the writing code process. TDD is not so much a method of quality assurance, but a way to think about code - so separate responsibilities would be against the philosophy of TDD. They'd also be impractical - the new test / new code cycles are very small, usually a matter of minutes. In my understanding, Test Driven Design would be a better description.

AUT can be fitted on an existing code base (although often badly, depending on size and structure of the code base). Separate responsibilities might have some advantages here. Still, AUT puts some pressure on design - so the separation would be at the who types the code level.


Distinction: I freely admit that I don't like the idea of TDD. It might work well for a certain type of coder, for certain applications, in certain markets - but all examples, demos and walkthroughs I've seen up to now make me shudder. OTOH, I consider AUT a valuable tool for quality assurance. One valuable tool.

like image 29
peterchen Avatar answered Oct 27 '22 10:10

peterchen


One of the benefits of TDD is the fast feedback cycle. Having another developer write the tests would slow the process down too much. The same developer should write both.

like image 29
Daniel Avatar answered Oct 27 '22 10:10

Daniel


Unit Tests and Acceptance Tests are two different things, both of which can (and should) be done in TDD. Unit Tests are written from the standpoint of the developer, to make sure that the code is doing what she expects it to. Acceptance Tests are written from the standpoint of the customer, to make sure the code fulfills the appropriate need. It can make a lot of sense for the Acceptance Tests to be written by someone else (usually because it requires a slightly different mindset and domain knowledge, and because they can be done in parallel) but Unit Tests should be written by the developer.

TDD also says that you shouldn't write any code except in response to a failing test, so having to wait for someone else to write the Unit Tests seems pretty inefficient.

like image 38
Paul Tevis Avatar answered Oct 27 '22 10:10

Paul Tevis


I'm a little confused here.

You say that you want to use TDD and you do seem to understand it correctly that a programmer writes a test, then the same programmer writes the implementation and does it in the next few seconds/minutes after writing the test. That is part of the definition of TDD. (btw 'the same programmer' also means 'the other programmer in the pair' when practising pair programming).

If you want to do something different, then go for it and write up your experiences in a blog or article.

What you shouldn't do is to say that what you do different is TDD.

The reason for 'the same programmer' writing the implementation, and writing it very soon after the test is for the purposes of rapid feedback, to discover how to write good tests, how to design software well and how to write good implementations.

Please see The Three Rules Of Tdd.

like image 44
quamrana Avatar answered Oct 27 '22 09:10

quamrana