News for hackers. Stuff that matters.
Re:Why To Write Tests For Your Code by gatlin on Monday August 29, 2011 @12:57PM (#2937849)
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.

Re:Why To Write Tests For Your Code by mechanical_fish on Monday August 29, 2011 @01:14PM (#2937926)
There was a good article that turned up for me just the other day (although it turns out to be from 2009) that captures the source of the problem:

http://research.microsoft.com/en-us/news/features/nagappan-1...

What the research team found was that the TDD teams produced code that was 60 to 90 percent better in terms of defect density than non-TDD teams. They also discovered that TDD teams took longer to complete their projects—15 to 35 percent longer.

So, as with all classic engineering debates -- emacs vs vi, MySQL vs MongoDB, et cetera -- there is no definitive answer to the question should we write these tests or not? because it's an engineering tradeoff. Writing tests and then throwing them away is foolish: You wasted time. Not writing tests and then fixing 60-90% more bugs is foolish: Bug fixing is a waste of time, often wasting more time than it would have cost to avoid the bugs up-front, and risking breakages in the process.

Which waste of time is better for you? It depends on the business situation. There are situations in startups where it's not worth while to write tests. Indeed, there are situations where it is a mistake to write code at all: Use a big pile of post-it notes and a cheap outsourced worker to mock-up the system while you prove that you actually need it. Whereas once you've found your business plan the more typical mistake is to write too few tests, which is why there's a big popular movement that promotes writing lots of tests: There are more people with too little testing than people with too much testing.

Re:Why To Write Tests For Your Code by Xurinos on Monday August 29, 2011 @01:12PM (#2937913)
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.

Some of the blur is caused by different kinds of programmer experience. We have to work with the "art" and "creativity" variable here, along with "has seen a similar pattern before". The other problem is asking the right question: What kinds of testing can create a consistent and solid improvement in acquiring money (or some other important pointy-haired boss measurement) over a 10-year period of time?

There are things that can make us feel good, nice placebos. For example, 100% test coverage is known to not equate to good, working, maintainable code: you may be missing features or doing things completely wrong, but your tests validate that your wrong thing or lack of thing is working.

Is there a magic bullet? Probably not... but it is widely believed (and probably justifiably so) that automated testing helps somehow. People will expound on the benefits. Personally, I want numbers. I want to know the pain before the tests and the less pain afterward. I want to know confidently that the time spent writing tests as part of development is less than the time spent discovering and fixing problems. Note the careful wording there.

Re:Why To Write Tests For Your Code by nasmorn on Monday August 29, 2011 @02:18PM (#2938190)
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.

Re:Why To Write Tests For Your Code by Xurinos on Monday August 29, 2011 @04:40PM (#2938852)
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 by sorbits on Monday August 29, 2011 @01:34PM (#2938015)
I assume that “not wanting to write tests” is when the tests are more complex than the code tested, for example writing tests for a GUI application using a closed-source platform framework is complex (if at all possible).

You can write tests for the core algorithms and data structures but that is often only around 10%.

Re:Why To Write Tests For Your Code by damoncali on Monday August 29, 2011 @01:54PM (#2938088)
If the project is new and the code is changing frequently tests slow me down and get thrown away frequently. So I don't write them until the app is on a stable path.
Re:Why To Write Tests For Your Code by jackpirate on Monday August 29, 2011 @01:06PM (#2937885)
Can anyone tell me why they would not want to write tests?

Laziness, ignorance, rapid prototyping, no budget, more important problems... all the obvious reasons. I can't think of any "good" reasons from an engineering perspective, only business.

Re:Why To Write Tests For Your Code by wccrawford on Monday August 29, 2011 @01:08PM (#2937895)
Rapid prototyping is the best anti-testing reason I've heard yet. I even halfway agree with it.

But in my experience, even prototypes can benefit from it.

For me, testing actually makes the whole thing go faster. I spend less time worrying and more time developing, because I have the confidence that the rest of my code works exactly like I want it to.

Re:Why To Write Tests For Your Code by coecoventures on Monday August 29, 2011 @01:19PM (#2937946)
Totally agree. We're following a lean startup approach and have identified our MVP and roadmap. Unit testing allows us to ensure our MVP works and that we have a reliable way to add features per our roadmap and still have a viable product. This sets the stage for us to rapidly and reliably enhance the UI, as well, while still shipping on time and getting customer feedback.
Re:Why To Write Tests For Your Code by fatalerrorx3 on Monday August 29, 2011 @01:40PM (#2938036)
Rapid Prototyping gets my vote..I'm in the process of working on a prototype for a new web service that's attempting to make easier a very complicated industry (where things are evolving at a rapid pace). I've noticed that we never have any time to create test user cases, however I am working with 2 others who hire outsiders to perform real user tests, which works well enough for the time being...but testing only a few users, with a minimal amount of requests doesn't really give you a good indication of whether or not your application can handle the load of multiple users. From a functionality standpoint user testing is good, but from a stress-testing go-live standpoint it's not really that good an indication as to where you stand
Re:Why To Write Tests For Your Code by d0m on Monday August 29, 2011 @02:14PM (#2938170)
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.
Re:Why To Write Tests For Your Code by gatlin on Monday August 29, 2011 @07:16PM (#2939437)
I am not a Randroid or very libertarian but I like Howard Roark's way of dealing with clients: you ask me to build X, and I will do it the best way I know how. If you want so damn much control over the how then you build it.

It irks me when clients tell me how I should do the thing they're paying me to do. It seems counterproductive and is insulting.

Re:Why To Write Tests For Your Code by d0m on Wednesday August 31, 2011 @09:47PM (#2948323)
Hmm, it's more.. I can do it in x hours the best way I know of; or I can do it in y hours by writing crappy code. So, at the end, if you tell your client that 10 hours is not enough to build a quality software but he still insist, well, it's his money.

It's a little bit like if you eat at a restaurant and you've got 20$. The cook could tell you there's something really good and way better at 50$.. but if you're happy with something less good at 15$, well, it's your body and your money. (I know that development is way different and in the long term tests will save money, but depending of the project, it's not always the case.)

Re:Why To Write Tests For Your Code by philcrissman on Monday August 29, 2011 @02:31PM (#2938240)
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 by colomon on Monday August 29, 2011 @03:52PM (#2938596)
One reason is simply that some tests can be very hard to write -- or outright impossible.
Re:Why To Write Tests For Your Code by LargeWu on Monday August 29, 2011 @05:16PM (#2939051)
If your code is that hard to test, it's also a sign that it will be hard to maintain.