Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

201–210 of 329 posts

Re: Ask HN: Do you write tests before the implementation?

#201
I write approximately as much test code as application code, but it never makes sense to write tests first.

I frequently redesign/rewrite an implementation a few times before committing it, often changing observable behaviors, all of which will change what the tests need to look like to ensure proper coverage. Some code is intrinsically and unavoidably non-modular. Tests are dependent code that need to be scoped to the implementation details. Unless you are writing simple CRUD apps, the design of the implementation is unlikely to be sufficiently well specified upfront to write tests before the code itself. Writing detailed tests first would be making assumptions that aren't actually true in many cases.

I also write thorough tests for private interfaces, not just public interfaces. This is often the only practical way to get proper test coverage of all observable behaviors, and requires far less test code for equivalent coverage. I don't grok the mindset that only public interfaces should be tested if the objective is code quality.

When practical, I also write fuzzers or exhaustive tests for code components as part of the test writing process. You don't run these all the time since they are very slow but it is useful for qualifying a release.

Re: Ask HN: Do you write tests before the implementation?

#202
It depends entirely on the project.

For most (but not all) software projects, writing tests before you write code is wrong.

For many (but not all) API-driven projects, I will write tests alongside my code. So if I would write a few dummy lines of code at the bottom of a file to confirm something is working, I'll write that up as a test instead.

In order for that process to work, writing tests needs to be extremely easy -- you need to be able to add a test anywhere without thinking much about it or wasting time pre-organizing everything.

On that note, shameless self-plug for Distilled (https://distilledjs.com), a testing library I wrote that I use in all of my projects, and that I like a lot.

The reason Distilled prioritizes flexibility is that I strongly believe there is no single, right way to do testing that can be applied to every project.

- For Distilled, I do TDD development where I write tests before code. This is because Distilled has a rigid API and behaviors, and because I use my tests as documentation. Distilled aims to have 100% coverage: https://gitlab.com/distilled/distilled/blob/stable/src/tests...

- For Serverboy, I only do integration tests based on test ROMs and comparing screenshots. Those screenshots get written out to my README and show the current emulator compatibility status. With Serverboy, I only care about the final accuracy of the emulator: https://gitlab.com/piglet-plays/serverboy.js/blob/master/tes...

- For projects like Loop Thesis (https://loop-thesis.com), I do a combination of unit tests and integration tests. I don't aim for 100% code coverage, and I think of my tests as a form of technical debt. For Loop Thesis I'm also adding performance tests though that let me know when the game is getting more or less efficient.

- And with exercises or experiments, I add tests haphazardly on the fly alongside my implementation code, putting very little thought into organization: https://gitlab.com/danShumway/javascript-exercises/blob/mast...

So every project has slightly different requirements and goals, and those goals drive my testing practices, not the other way around.

Re: Ask HN: Do you write tests before the implementation?

#203

No. Tests are like any other code. They incur technical debt and bugs at the same rate as other code. They also introduce friction to the development process. As your test suite grows, your dev process often begins to slow down unless you apply additional work to grease the wheels, which is yet another often unmeasured cost of testing in this fashion. So, in short, I view tests as a super useful, but over-applied too…

Thankfully we don't have to. Hillel Wayne links to a few of the studies that have been done on TDD [0]. It doesn't have a conclusive effect on error rates in software. While I do write tests before I write code, more so in a dynamic language without a strong, static type system; it appears that there isn't any correlation to a reduced number of bugs. But I still do it. And I think that's because that while I may prev…

I haven't written tests as part of my regular workflow in... years now, but one of my earlier mind blown / level up as a developer was when I wrote tests, which made it required that my code actually had to be clean and conform to a load of best practices that weren't in my mind's eye yet.

The other day I was doing a part of Go Blueprints which touched upon TDD; one thing it highlighted was that if you use TDD you should only write until your test is green, no more - this (supposedly) avoids you writing too much code. It has some merits. I'll need to do a lot more TDD and co to actually see it though.

Re: Ask HN: Do you write tests before the implementation?

#204
No. I suppose only really smart thinkers and high level architects do that. In a book or a blog post, not in a real thing. It's similar to Aikido or a kata based martial dance where the oponents are either imaginary or aren't allowed to hit back/must follow a script if they exist.

Re: Ask HN: Do you write tests before the implementation?

#206
post #30

Earlier quoted context omitted.

The benefits you describe seems to be achievable with tests written after code as well. We write extensive unit tests, but mostly after development work. The re-write work you mention is then avoided.

The benefit of TDD is that the code you end up with will actually be testable. Just keeping in mind that you have to write a test for your code, changes how you write it. As a bad example, imagine having a 1000 line function that just does everything you needed for the new feature... Good luck testing that afterwards.

> Just keeping in mind that you have to write a test for your code, changes how you write it.

Indeed it does! But that doesn't really change whether you write the test pre- or post-coding.

Re: Ask HN: Do you write tests before the implementation?

#207
No.

I've had Sr. Soft. Engs. ask me why I thought we needed unit tests at all. I've had managers not know what they were. I've worked on projects where I was the only developer who wasn't afraid of the technology, but management couldn't give proper requirements. I've also worked in code bases where no testing framework (of any kind) existed.

I don't mean to sound combative. Looking back those places would benefit immensely from structured testing. Life got in the way though.

Re: Ask HN: Do you write tests before the implementation?

#210

Yeah. I also floss every day, clean up the kitchen as I cook, keep off-site backups of my personal data, call my mother regularly to thank her for raising me, read the terms and conditions to online services, keep an up-to-date to-do list, and change all my passwords once a month.

Suddenly I feel very lonely for the fact I do actually clean the kitchen during and right after cooking...

Me too. In fact I dont usually eat without having cleaned up. I like to finish my day after dinner without having to worry about things I have to do. Post dinner is just for things I want to do.
Post reply on HN