TDD comes out of the same circles as Extreme Programming, aka XP. In XP, there's something known as a spike, which is basically a short-lived technical prototype. If you really don't know how to tackle something, you build a quick one to throw away.
In that case, yes, definitely don't do TDD, because a goal of TDD is to build up a good test suite for production code. It's not a good match for throwaway code (assuming the team really has the discipline to throw it away.)
But if I'm exploring in a way that feels less disposable to me, which happens often, then I'm happy to use TDD. E.g., if I want to try out a new way of solving a problem, or I have an idea for an architectural improvement. Then I'll use TDD to force myself to think about it from the outside in, to keep design considerations to the forefront.
But you should know this from your functional requirements, right?
(Slightly sarcastic comment, I know in a lot of places people just start coding without a plan, but I think TDD and functional requirements go together well).
Well, I don't do that. But i usually don't know all the names of my (internal) interfaces and models and properties right at the start. I might move things around and rename stuff along the way. And I think that's quite normal if you don't live in a world where the spec for a small task is multiple books thick.
Doesn't TDD get in the way with? Wouldn't I keep refactoring the tests all the time then?
Functional requirements describe observable behaviour. You can't write any meaningful tests for that behaviour until a lot of your code is written.
Yet somehow in all the discussions there's this unexplaned leap from "you have the requirements" -> "well you just write a unit test for your function first, and then code it".
My functional requirements come in the form of "Given publisher A in Country B and contract requirements C the service must return D for a subset of products E", for multiple combinations of A, B, C, D, and E. And where each of those may and will require lookups in external services and databases.
Essentially if you don't have an existing code structure in place, TDD will be very confusing and you'll end up tangling yourself into a coding mess.
On the contrary, if you have a structure in place and need to ensure the correctness of the code you're about to write, it is incredibly useful.
Building my own stuff, I hardly ever write any tests tho as in the early stages of a project, testing will slow you down as the tests are essentially locking your code in. For me personally, I prefer to have my code be more fluid so I can rapidly change it without having the overhead of having to write and rewrite the corresponding tests.
When I come across some tricky logic tho I will definitely crank out some tests as it actually helps to create the solution.
As usual context is key to knowing whether TDD (or even testing) is appropriate or not.
If you prototype a new features for an existing system, a test let you execute only the code you actually need. This will shorten your feedback loop and allow you to iterate faster. Refactoring a test is fine. Writing a BS test just to explore a solution is fine. In my experience following a test lead practice will help you build a simpler system which will be easier to maintain.
It sounds like you're making the mistake of thinking that TDD means writing the tests first. That's not quite right but it's understandable that lots of people believe it given the way TDD is usually discussed.
In TDD you should be writing one test first. Then you write enough code to make that test pass. Then add a second test. Get that to pass. And so on. If you realise you missed something you can delete tests that don't make sense any morejuat like you delete code that doesn't make sense. In essence TDD is about writing the tests in parallel to writing the code instead of writing them afterwards.
You can easily prototype and experiment with TDD because you're never much further ahead with your tests.
TDD is faster than writing tests later because your code has to be testable throughout the process. Writing tests later often means you need to refactor or rebuild to make it testable, which is a waste of effort, even in a prototype.
The way I have always gone about TDD is just that I am testing the code I am writing by running the test, not from the main entrypoint of the application. The things you would log and look for when you run the application you instead validate with an `assert()`. Then once you have finished developing, you do a single pass verification from main and you have both a test and a function written
It's just as easy to be inefficient writing useless/bad tests than it is writing bad code. I think every developer should do TDD in their career, because it will make you think about the testability of the code. That said once you understand how to write testable code than I think it makes sense to be flexible on whether you write the test first or take a crack at an implementation.
The argument that you should have a good understanding of requirements I think leads to a lot of analysis paralysis, and isn't a practical method for building novel things in a timely manner.
You have your test which gets the response, and you can still log the output to see what it looks like. It's a little slower at first since you have to write the test, but I'm not sure I see a big distinction vs "have a small script that sends the request".
And do you see that in many examples just coding ahead without conaidering how to test the code later will in many cases leave you with code that is such a pain to test that you start avoiding the tests because "they take too much time".
If you start with the test the question of "how can this be made testable" is unavoidable.
I am not dogmatic here. If you are operating on the level of "I am happy if anything works at all" then starting with writing a test is probably a bad idea, better try things out first, scrap the whole thing once you understood what you wanna do in which way and then start writing the tests.
For a lot of adhoc code test driven development doesn't make too much sense, but if the stuff you write could ruin someones day (or life) maybe it is the way to go.
For the rest of us it's better to write too many tests than too few. You might waste some time, but that a smaller problem than accidentally skipping a test that was actually useful and pushed your code in a better direction than you'd have gone without it.
That's a very bad attitude that is all but enforced by TDD proponents.
It's very easy to see which tests are useless and which are not. So you end up with people writing thousands of unit tests and little-to-no integration or functional tests because:
- "TDD told me so", and
- Testing frameworks are written by people who adhere to the same philosophy
Whereas there's very little utility in those "too many tests" while they give you a very false sense of security.
For trivial things, maybe, but then people start applying the 'rule' to non-trivial things "because it's easy" and they get it wrong. All I'm saying is that until you are a Kent Beck level expert it's safer to err on the side of caution. When you have a ton of experience and knowledge you can do what you like.
Also note that I said its better to write too many tests than too few - that still doesn't mean you should test everything, or that you have to test trivial things. It just means when you're not 100% sure it's safer to write a test.
Most of the stuff we do is trivial [1]. Unfortunately no one really teaches testing or shows what needs ot be tested. Hence the prevalent "write hundreds of tests perhaps some of them will actually be useful". And most available examples, and most available advice is that: write many many useless tests.
I have a perfect example from the Java + Spring world. It's common to have a Controller + Service + Facade + external service client definition.
So I've seen countless times when tests are written as follows:
- external service client is mocked, multiple unit tests for the Facade to make sure it returns data
- Facade is mocked, multiple unit tests for Service, to make sure data is returned
- Service is mocked, mutiple unit tests for Controller, to make sure ata is returned
They are all the same tests, and can be easily deleted and replaced by a single suite of:
- external service is mocked, including invalid responses and timeouts. The actual rest service provided by the app is tested for all the scenarious that were quadruplicated in the unit tests above
You don't need to be a "kent Beck level expert" to do that. However, almost literally nothing teaches you to do that or helps you write those tests. Almost literally everything is hardwired to write small useless unit tests.
[1] Except UIs. I have no idea how to test UIs, and I don't think anyone does :D
Actually, Kent Beck, who coined the term TDD, has the following to say in his book "Test Driven Development":
"In Test-Driven Development, we Write new code only if an automated test has failed"
This implies that in TDD the test is written first.
Regardless of any definition, test-first and test-last are just two ends of a scale we can use.
The code I write initially can often be very dumb, repetitive and procedural. I often have to discover and see what the data structures really look like, what the public API looks like, what I leave in code and what I want to be data driven etc.
At this stage it makes little to no sense to write tests first. All I need is to see the output, which might change dramatically during development. It’s a very fast, direct way of programming, with a lot of copy pasting and long parameter lists.
When the code is laid out like that, then I can see where it’s going. I see the repetitions, the edge cases etc. Now it starts to make sense to design the data structures, give declarative names, find the right abstractions. Now I can move forward with tests.
Testing (aka evaluation) should happen when one knows the routine + outcome you want to evaluate that routine one.
Whether it’s code or improving your singing or learning in school. This generalizes to all of that, IMO
I do this plenty after writing a new (high level integration) test.
Usually I have a fairly clear idea of what the public API I want should look like before I code. A test that calls that makes a good test. I might tweak it when coding the implementation, but probably not by much. The integration test doesn't care what kind of data structures you use.
>At this stage it makes little to no sense to write tests first. All I need is to see the output, which might change dramatically during development.
I frequently write the test first for this type of code. Where I'm expecting the test to change the output I program it to temporarily overwrite the output with the results of the program, eyeball the result and commit it all together if it's correct.
The self rewriting pattern is especially good for API tests or command line tests - where the output is a blob of JSON or a wall of text.
In fact, if you know exactly what you are building, how could tests drive your decisions? You have already made up your mind. If you know what you are building you can simply implement it straight away without the data gathering phase.
This is the main issue that I have with TDD. I use a methodology that I call "Evolutionary Design,"[0] and TDD won't work for that.
But I think they have the right idea, in emphasizing the need for extreme Quality. I like to achieve this, by using test harnesses, as opposed to unit tests[1].
[0] https://littlegreenviper.com/various/evolutionary-design-spe...
[1] https://littlegreenviper.com/various/testing-harness-vs-unit...
However well written tests are independent of how you organize your code or any other implementation details. So tests are great even when you are just prototyping or exploring. You can rapidly change your code and have the tests ensure that it still works the same way. And the end result is a fully tested, easy to maintain code base. What’s not to love about that?
Just don't be dogmatic about practices like TDD. It's always a tradeoff. Being a good engineer means understanding when the tradeoff is worth it and when it isn't. Be wary of development teams, or people, that force you to practice certain methodologies regardless of context.