Explain like I'm five: Is Test Driven Development really that used in most of the programming world?

When I'm navigating on the big wide web that is the internet I often see job postings that require experience in TDD. I've been programming for about two years and I've never really used PHPunit(shame) because each time I finish a functionality I test it with various inputs to correct bugs. Can someone explain (like I'm five) what's the importance of programming using TDD?

P.S.: I hope I've posted this in the right place cause this is my first time :p

I do TDD sometimes but it's never been my only way of coding, for me it just doesn't always fit the problem practically. But if I have a really good idea heading in of what I want the solution to look like and where the interfaces are going to be, it's a great way to step through your code and proactively sturdy it up.

I'll add that TDD is a bit like going to the gym, lots of folks have a membership, but most don't go as much as they say they do. TDD is a very disciplined way of coding and in truth it's not actually that popular, even among those who claim they do it.

I tend to take the opposite approach. The more unsure I am, the more I leverage TDD. Writing tests first really helps me think about structure, interfaces, etc.

In my experience, I've found that many people and companies conflate and/or confuse TDD with unit testing in general. If someone asks me if I do TDD, I say no and then quickly explain that I'm less interested in when the tests are written - as long as they are written.

I've worked with a few people who always write the tests first. But the vast majority (90%+) tend to write tests during or after their code (or not at all!)

Totally agree. I also think "TDD" is often used to mean "Unit tests". Saying that you never unit tested your code during a job interview sounds a bit like suicide to me. On the other hand, I think no one will think it's a real problem if you code first and then unit test it.

Yeah, I also think that when most companies mention TDD they mean unit testing. In my current job I have practically zero at all to write tests as the main focus of the superiors is to get the most amount of results in the least amount of time possible...

I wrote a blog about this quite a few years ago: coveros.com/why-i-write-tests-first/

I like test-driven development, but I’m not a purist. I almost always write my code with testing in mind, but I don’t always write tests first. Most of the time, but not always.

When I do write the tests first, I find that the process works just like the books say it should. That is, I write the tests and in doing so define the interface that I want to use. By the time I have what I believe to be a complete, failing test, I have identified the behavior I want along with a number of edge cases. Once I start the implementation, coding is fairly straightforward and is usually done more quickly than I would have expected.

There has been a heated debate on whether TDD is good practice or a bad pratice, with valid arguments on both sides (in my opinion).

There is a lot less disagreement whether "Self testing code", as Martin Fowler calls it, is a good thing: martinfowler.com/bliki/SelfTesting...

Spoiler alert: For any non-trivial project, it's a good idea, in my experience. Without it, you can't be sure that you didn't accidentally introduce errors in your software when making changes to the code.

Ever heard of "never change a running system"? That's a sympton of code that is not self testing.

Especially if you are developing in an iterative fashion, regression testing ALL of the code (not just the new features) every iteration is not feasible manually. At least not with reliable results.

But regression testing is exactly what you need to do to prevent unexpected side effects (apart from a design that reduces coupling). A new feature may break an old feature unexpectedly. Or what about that accidental typo you introduced without noticing?

TDD is one practice to lead you to self-testing code.
Some people claim it helps you with the software design, but I would claim you need to know about software design in order for TDD to help you with it.

I've attempted TDD a few times now in my hobby node.js development projects. I think the biggest thing you find is that when you start a new project you just want to get on and start coding. Pulling in the libraries you need and hacking away through the code.

However, whilst I know I'm a horrible person for doing the above, I do it all too often where time and fun is paramount working on these hobby projects you tend to want to forget the boring test world. With that being said, my current project has hit a point where there's some logic I know I simply need to test. For this specific logic I've produced my test file, added in some cases and I enjoy seeing the code re-run every time I save my work assuring me everything is ok.

There is enjoyment in testing. However, I don't feel like I have the enjoyment in writing a test for every single part of my code. In fact, I know that I struggle with gauging how in depth my scenarios need to be and also how detailed I need to go into the depth of my code to assure myself that everything is working OK. I believe I work in a world where I make a judgement as to how long I'm spending killing myself refactoring, re-running snippets of code and then where an automated test would pay off.

Testing is tough for my mind. As mentioned the level of detail required boggles me and also what about all those scenarios I may invariably miss? I understand the need, I do minimal testing but the world needs to stop making me feel guilty for my lack of 100% coverage.

I'll be posting a new thread soon in the hope to get some answers around some of these questions.

Explained to a 15-yrs old: In most companies, and most groups, there's at least one or two people who use this practice on a daily basis. There's about as many people that haven't mastered the practice, ie they're not able to use it, most commonly due to lack of training and exposure.

Just went to a great talk last night on TDD by John Reese. His example helped me understand that, like others have said, writing the tests first helps write cleaner code that's easier to maintain, understand, and is obviously testable.

I think it's the backwards approach to what most teams are used to, but usually is a goal that they want to try, but just haven't made the switch.

Initial investment is a little deeper, which project management also isn't on board with generally, so that could be a factor as well.

By chance the talk isn't available online? A quick google search only revealed the meetup page and the next result was the actor of Person of Interest xp

The idea of writing the tests first does sound like it will help to building more robust code, have to try it soon if I get some free time

It’s not, he’s active in the open source community and the code is on github if you want to probe through it.

github.com/jpreese/MessageSender

I'd like to separate out two things.

First, many job postings ask for TDD. Most companies want people who know it, but they will NOT do it. So knowing and speaking intelligently is going to cover a huge amount of job postings. It is often sought because some of the most well-known people in our field use it and have written about it extensively. It is also one of those things that are labeled a, "Best practice."

Second, TDD is a skill. It takes dedication and practice just like any skill. I've been doing TDD near exclusively for about 5 years now.

The benefits that I have seen come with it are:

  • High-confidence in production
  • Fewer defects and bugs related to my code
  • Very few "Your code is shit" conversations
  • My code becomes very obvious
  • My tests tell me what the code should and shouldn't do months from now
  • My code begins to take on more traits of well-designed code
  • I become very familiar with the tech through failing tests

I wrote an article here about trying TDD out

Short answer: It's not, because people are lazy.

But it does not mean you should stop doing it, or that you should never try it.

TDD for me is like healthy-eating habits: It's good for you in the long run, and makes you think before every step. And hey, because "not so many people eat healthy", Would you eat fast food on a daily basis? :P

Personally, I do some TDD, specially on projects that have lots of different inputs and complex rules.

PROTIP: If you use Visual Studio, you can enable Live Unit Testing, which is new for VS2017 and amazing for this kind of approach.

I have been using TDD methodology , I don't always follow it the way it is. But certainly my coding has been improved since I have adopted it. Here are my reasons

1) Now code are structured
2) Everything is tested now, so no more surprise bugs. Especially null bugs.
3) If production code fails , I know exactly what could fail as all code has it's unit test and integration test
4) My colleague and I can understand any codebase now. this is utmost important.

I use TDD at work for bug fix tickets specifically. This seems to mesh nicely with the way my brain approaches code. When I'm adding a feature, I often mess around a lot before I can really articulate to myself exactly what I am trying to do. Whereas with bug fixes, writing tests often helps me identify exactly what the problem is.

It's been a while since I did a refactor-only ticket as opposed to rolling refactors into some other work, but it seems like the TDD approach would be good for refactors as well.

A few years ago, I interviewed for a job, and one of the developers said "we try to do TDD here". So my first project on that job I tested a lot. But it turned out that there was no team culture of unit testing, and it was just this one developer who was into unit testing. In fact, I had put so much effort into testing that I became the "unit testing guy" for a while.

So TDD in a job posting might just be something they threw on there, and it's common for not everything listed to be a hard requirement.

I've never done TDD in its full form, but there are cases where writing the tests first helps. A self-contained class where you know the interface up front, and it seems to help when fixing a bug to write the tests to reproduce the bug before fixing it.

There are arguments either way for TDD. David Heinemeier Hansson had a good RailsConf keynote that I've adopted as my philosophy of testing: The level of testing should be a function of the level of confidence needed for a particular piece of software. Test the things that you think might break, test the application as a whole, and test the things that have broken before, but don't go overboard, because testing a too granular a level can make it hard to refactor. And prefer integration tests over unit tests (this one is iffy, because integration tests can be slow).

I worked for a company for two years that prided themselves on creating BDD and TDD tools (one of them quite well known). They were known in the dev community for making these tools and being a company that "writes tests".

HOWEVER, TDD or BDD were used in no project I worked on for that company. Internally the developers joked about this.

I think generally developers at work don't do TDD. In my experience no company using agile or waterfall allows time for TDD. I have read many articles about TDD and have used it on personal projects. But in regards to a work environment I have never come across it.

My advice is to take what you read in jobs ads with a pinch of salt. And remember they are trying to entice you to apply for the role, after all it is an "advertisement".

Yes it is.

Especially for junior devs. Several times occurred that when I was asking about unit test (also in review) or even TDD approach and one of colleague refuse, it quickly turned out that she/he didn't cover all of story paths.

Fortunately we could prevent this waste of time (and money), only if we use TDD at the right time.

Cover all code paths, check edge cases, validate input and output are good entry point on TDD. You cannot defend yourself against all bugs, but you can dramatically reduce the amount of it.

Of course you can just sit and code, but in my opinion it is good only for small projects and PoC. In any larger solution, when a way to manually test a feature consist of setup whole environment, we should use TDD. First, you are testing in isolation only feature you want to test. Second, you can mock any dependencies you want. Even if you want to test your feature with real dependencies, you are able to write integration test (still using TDD).

I imagine TDD like training with weighted armor. I try to TDD in all I do because I don't feel like, after a yearish, that I have mastered it. Follow the few rules and the rest is art.
blog.cleancoder.com/uncle-bob/2014...

Relax, it's fine. Most people don't use git and you are asking about tests?

Making tests is very important as it makes you confident to change and refactor stuff around.

p.s. but making right tests is not easy, that's why i don't do it ;P

Nope, in the programming world you have legacy code. Is not that used.
Automatic testing yes, TDD practice no. Also it doesn't make sense in startups, prototyping, MVP's, gaming and other fast changing envs.

Not in my experience.

It's not a fad (cough*XP*cough), and it definitely works.