Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

51–60 of 329 posts

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

#51
post #33

No, in fact, we don’t use coded tests at all, deliverables are tested by the product owner(s). On the code level, we hardly see bugs, not even when refactoring. I often wonder if it is luck or expertise, and whether we would benefit from writing tests.

If you tested, you wouldn’t have to wonder.

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

#52
Never did TDD as prescribed. But I try to write one test together with the implementation, to have basic coverage to cover the most silly things, and to ensure testability. This way it is easy to add more tests later, if and when I find that they are needed.

Also I try to test at subsystem / API boundaries whenever possible. Small units like a function rarely get their own tests, they are covered implicitly by being used. This avoids tests of arbitary internals (that should be free to change) becoming a maintenance burden. External APIs should be stable.

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

#53
Others have said similar, but for better or worse the three cases where I usually write tests are

    - when it’s really easy
    - when it’s really important
    - before a refactor
The last one is arguably the most important and has saved me a lot of headaches over the years.

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

#54
Only for functions that have a clear input and output. You can nicely think of all the edge cases, and then get going with the actual code.

But most of the time, fixing some bug or implementing a feature is more of experimenting and prototyping at first. Writing tests for every futile attempt would be a waste of time.

At best we design some small architecture first with interfaces, and then create the tests off that.

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

#55

I follow the Functional Core, Imperative Shell pattern. I do TDD on Core, especially on mission critical code. The Shell however have almost zero automated tests.

Thanks for that term, it sounds related to the style I prefer. Do you have any recommended references?

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

#56
post #30
post #6

I mean how many of you stick with this test driven development practice consistently? I have been doing this for a while now. Practically, saves me a tonne of time and am able to ship software confidently. Can you describe the practical benefit? Say, a change is executed on one section of the (enterprise level)application. You missed addressing an associated section. This is easily identified as your test will FAIL.…

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.

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

#58
I do, TDD gives me such a sense of confidence that now that I'm used to, it's hard not to use.

> Can you describe the practical benefit?

Confidence that the code I'm writing does what it's supposed to. With the added benefit that I can easily add more tests if I'm not confident about some behaviors of the feature or easily add a test when a bug shows up.

> Do you happen to rewrite the tests completely while doing the implementation?

Not completely, depends on how you write your tests, I'm not testing each function individually, I'm testing behaviour, so unless there's a big architectural change or we need to change something drastic, the tests have minimal changes

> When does this approach work for you and when did it fail you?

It works better on layered architectures, when you can easily just test the business logic independently of the framework/glue code. It has failed me for exploratory work, that's the one scenario where I just prefer to write code and manually test it, since I don't know what I want it to do...yet

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

#59
I've been writing software professional for 20 years and for much of that time I was very skeptical of testing. Even after I started writing tests it was several more years before I saw the value of writing tests first. I've moved to doing this more and more, especially when doing maintenance or bug fixes on the back-end. I still struggle with writing valuable tests on the front-end, apart from unit tests of easily extracted logic functions, or very basic render tests that ensure a component doesn't blow up when mounted with valid data.

If you write your test after making the code changes, its easier to have a bug in your test that makes it pass for the wrong reasons. By writing the test first, and progressively, you can be sure that each thing it asserts fails properly if the new code you write doesn't do what is expected.

Sometimes I do write the code first, and then I just stash it and run the new tests to be sure the test fails correctly. Writing the test first is simply a quicker way to accomplish this.

Like others have said when there is a lot of new code - new architectural concerns etc, its not really worth it to write tests until you've sketched things out well-enough to know you aren't so likely to have major API changes. Still, there is another benefit to writing the tests - or at least defining the specs early on - which is that you are not as likely to forget testing a particular invariant. If you've at least got a test file open and can write a description of what the test will be, that can save you from missing an invariant.

Think of tests as insurance that someone working on the code later (including yourself, in the future) doesn't break an invariant because they do not know what they all are. Your tests both state that the invariant is intentional and necessary, and ensure it is not broken.

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

#60
No. I have always felt that TDD gives a false feeling of safety and satisfaction, and that it is mostly a waste of time that could be better spend optimizing and refactoring.

Testing simple code is simple and therefore pretty much useless. Testing complicated code is complicated and therefore more likely to fail by making to few or to many assumptions in the test, or completely screwing up the test code itself.

Post reply on HN