Live data from Hacker News

Why To Write Tests For Your Code

masternerd.lucianocheng.com

21–30 of 32 posts

Re: Why To Write Tests For Your Code

#21
post #2

There are obviously many reasons, but to add a couple; Fight regression You don't want to fix the same bug several times, do you? When a bug is found, I first write a test to repeat the bug. Then I fix the code. Now every time I want to release a new version, that particular bug is tested yet again. The 'quick fix' 5 minutes before launch You're approaching deadline, everything is looking fine. Then, just before laun…

The 'quick fix' 5 minutes before launch

The universe tends towards maximum irony, and you are seriously underestimating that maximum. Even with 100% test coverage, fixing something 5 minutes before launch is an awful idea. Either delay the launch, or accept that the bug is small enough to not matter.

Re: Why To Write Tests For Your Code

#22
post #19
post #3

I am relatively new to HN and have _read about_ but not _seen_ an anti-testing mindset around here. I imagine this article will stir that debate if it exists. Can anyone tell me why they would not want to write tests? I have no biases here.

If you're paid per hour and your client want you to take the less possible time. It's stupid; I agree. You can try argue with him/her about the importance of it, but in the end, it's their money so it's their choice.

I don't even ask anymore. If I'm writing an app for you, I'm starting with tests, before I even write a line of executable code. If you don't want tests for your app, you'll need to find a different developer. It's not an optional part of the process, it's how the app is developed.

Re: Why To Write Tests For Your Code

#23
post #4

I'd love to see an article on unit testing that gives reasons they add business value, so hackers like us can convince pointy headed bosses that we should be allowed the budget to write them. "Improving the quality of our software" is the one I tend to use, but it's so vague. How will unit testing convert into cold, hard cash is what upper management want to know.

Feigning incomprehension, I will ask, “Why do you need budget to write them?”

If I am estimating how long the work will take, I am estimating how long it will take to get the functionality working correctly with X% confidence it works correctly when the product ships. Unless “X” is less than 50%, automated testing is part of writing the code, not a nice-to-have.

“Improving the quality of our code” does sound vague, kind of like arguing how many knots are permissible in a plank of wood destined or a kitchen floor. Instead, I would simply explain that these tests I write are part of writing the code, and to explain that if management want the code without the tests, they need to tell me in writing that they are ok with a confidence level hovering around 50%.

I am not kidding about this number. I may deliver code that appears to do what it is supposed to do, but my experience is that either it is broken in ways that a simple QA test doesn’t show, or somebody (somebody else or even me next week) is going to regress it between now and when the product ships. Automated tests give me the confidence that it works, works correctly, and will continue to work correctly.

Re: Why To Write Tests For Your Code

#24
post #2

There are obviously many reasons, but to add a couple; Fight regression You don't want to fix the same bug several times, do you? When a bug is found, I first write a test to repeat the bug. Then I fix the code. Now every time I want to release a new version, that particular bug is tested yet again. The 'quick fix' 5 minutes before launch You're approaching deadline, everything is looking fine. Then, just before laun…

The 'quick fix' 5 minutes before launch The universe tends towards maximum irony, and you are seriously underestimating that maximum. Even with 100% test coverage, fixing something 5 minutes before launch is an awful idea. Either delay the launch, or accept that the bug is small enough to not matter.

Yes, I can/do not deny the power of the irony gods.

awful idea, yes, I agree, but real world is full of awful ideas. You can either accept it and adapt, or be drowned by it.

Re: Why To Write Tests For Your Code

#25
post #4

I'd love to see an article on unit testing that gives reasons they add business value, so hackers like us can convince pointy headed bosses that we should be allowed the budget to write them. "Improving the quality of our software" is the one I tend to use, but it's so vague. How will unit testing convert into cold, hard cash is what upper management want to know.

Feigning incomprehension, I will ask, “Why do you need budget to write them?” If I am estimating how long the work will take, I am estimating how long it will take to get the functionality working correctly with X% confidence it works correctly when the product ships . Unless “X” is less than 50%, automated testing is part of writing the code, not a nice-to-have. “Improving the quality of our code” does sound vague,…

"kind of like arguing how many knots are permissible in a plank of wood destined or a kitchen floor"

Having sat through an hour long lecture on lumber grades in a scenic construction course, let me tell you, it is anything but vague.

Re: Why To Write Tests For Your Code

#26
The way I look at it, there is only one reason to write tests: to assure shippability.

Everything else is a pleasant bonus. It amazes me that so much can be written without even touching on this.

Re: Why To Write Tests For Your Code

#27
post #3

I am relatively new to HN and have _read about_ but not _seen_ an anti-testing mindset around here. I imagine this article will stir that debate if it exists. Can anyone tell me why they would not want to write tests? I have no biases here.

One reason is simply that some tests can be very hard to write -- or outright impossible.

Re: Why To Write Tests For Your Code

#28
post #20
post #9

Earlier quoted context omitted.

I do not have a solid stance, but I am inconsistent on writing tests. I like to differentiate between the notions of hand-testing code and automated-testing code. I firmly believe code written should be verified somehow. I have not totally bought into writing tests. Right now, there is a lot of snake oil about what is better than what, and I would like to see good scientific studies that point in solid directions. So…

I am not so big on unit tests because I feel I mostly write glue code in rails were the problems are really not very hard. What I really like is Selenium test that work like manual testing and I thus feel they just automate something I would need to do in any case.

I agree; I like the idea of Selenium for web GUIs. I remember wanting to write my own similar tester for Win32 GUIs a while back, but I ultimately did not see enough value in it. For the same reason, I do not use Selenium: you have to completely "rewrite" the tests to adapt to small interface changes. So I wrote up test plans for humans to run, and they could adapt to the changes; it was faster than writing an AI. ;)

At an old job, we had what was called "random testing". This was a time to relax and take a few hours to pound at the application with whatever we could think of. With luck, someone would go ahead and attempt to automate those tests. One of our favorite "random" tests was to start some complicated process and then shake the window all around the screen to see if it would crash -- sometimes it did! It forces people to rethink their threading approaches. Another fun trick is closing an app mid-processing; does it exit gracefully?

Re: Why To Write Tests For Your Code

#29
> In a way, using tests to check code is like designing two trucks and having them pull against each other; if nothing breaks, you know they both work.

('bad metaphorical thinking' Dijkstra alert)

This is wrong. If the tests pass, it is entirely possible that both the original code and the tests are faulty.

Re: Why To Write Tests For Your Code

#30
post #27
post #3

I am relatively new to HN and have _read about_ but not _seen_ an anti-testing mindset around here. I imagine this article will stir that debate if it exists. Can anyone tell me why they would not want to write tests? I have no biases here.

One reason is simply that some tests can be very hard to write -- or outright impossible.

If your code is that hard to test, it's also a sign that it will be hard to maintain.
Post reply on HN