5 ms·
In this case, I think Kent's right. Joel's doing it wrong. You should never have 10% of your unit tests depend upon the location of a menu. You should have o
by nostrademons 18y ago
In this case, I think Kent's right. Joel's doing it wrong.
You should never have 10% of your unit tests depend upon the location of a menu. You should have one unit test that depends upon the location of the menu, and everything else should be isolated by stubs, shunts, mocks, whatever. Otherwise, they aren't really unit tests, because they're testing more than a unit.
I didn't realize how wrong I was doing it until I got to Google. (Then again, a lot of Google's best programmers are doing it wrong too.) Perhaps this is because doing the right thing - mocking out your dependencies and testing only one feature per test - is often harder in the short term than just running all your code when you test the topmost layer. But I think the point needs to be hammered home: most programmers who're doing "unit" testing are not doing unit testing, and their code would be much less brittle if they took the time to stub out lower layers.
- zaius 18y agoAs someone who is relatively new to the world of testing, I think I finally understand the point of stubs/mocks/etc. Thanks!
- axod 18y agoI agree, in theory. However, when you're quickly iterating, redesigning, changing how things work, having to completely rework the unit tests as well is pretty much wasted effort. I think it depends on the project - is it a 'design up front, go away code once' project, or an iterative constantly changing thing.
- swombat 18y ago'Fraid not, axod. It's when you're quickly iterating and changing lots of stuff that quality unit testing helps you the most. Let's look at 2 possible scenarios: 1) You don't bother testing much. You make lots of changes, iterate quickly. Before the changes, you have code that, let's say, you know works. After the changes, you know there's probably bugs, but you don't know where. 2) You test fairly thoroughly. You make lots of changes, iterate quickly. Before the changes you have code that you know works. After the changes, you know where most of the bugs are because your tests tell you where they are. It's the difference between walking with your eyes open or closed... it may be more work to process all that information about obstacles, but it sure is helpful if you want to avoid those obstacles.
- fizx 18y agoI'll chime in from recent experience. I had to pull a dependency in my project because it was causing heisenbugs that I couldn't root out. Without tests, I wouldn't have known. With tests, it took 2 hours to replace (diff -200 +250), and I'm confident the library still works. Without tests, it would have taken several more, at least.
- mattm 18y agoHow long did it take to write the tests?
- fizx 18y agoI write tests as I develop, and they take the place of going to the browser or a repl and poking around at my work. In some sense, testing takes zero or negative net time. It would be hard to measure gross time, because it occurs in such small increments.
- axod 18y agoI disagree. If I'm constantly changing my mind about how best to do something, I don't want the massive overhead of having to change unit tests all the time as well. I'll test as I go, in the code.
- gdee 18y ago"If I'm constantly changing my mind about how best to do something" then you're finding /more/ not /best/ ways.
- Retric 18y agoIf your testing in the code then your doing unit tests. But probably in a poor fashion. Unit tests should hook into the application in such a way that you can reorder the GUI and change nothing. If you are using Unit tests correctly then any change to the code base at worst requires 10% of that effort to change the test cases. If you can think of any meaningful change that is worse then that your something is wrong with how you're testing.
- shiro 18y ago> I think it depends on the project - is it a 'design up front, go away code once' project, or an iterative constantly changing thing. I think your line is too coarse. I do heavily rely on unit tests even in "iterative constant changing projects". But in the very early "sketching" stage of development I don't bother to write tests. I just play with code on REPL (I work with Common Lisp or Scheme most of the time), in a flat namespace. Then, once I start putting code snippets together into packages (CL) or modules (Scheme), that's about the time I start writing tests as well. After that, even I constantly change stuff, sometimes drastically, unit tests do help me a lot. It is true that you need effort on reworking on the tests. Actually I think writing tests needs more work than writing actual code. And I think that's the right thing. Writing something that works is an easy part. Making sure it doesn't break in every possible situation is much harder. In language like CL, you can fairly quickly write something that takes typical input and reasonable output without serious thinking. To make sure it works in every corner case you have to think much harder and precisely, and test code reflects that thinking. (NB: I know some programmers who seem to think out those corner cases in their brain and just spit out a beautiful code at the first attempt. They may not need support from test code, I guess.)
- jsdalton 18y agoDo you know of any good texts or articles that describe the "right" way to test? I put myself in the category of mediocre test writer, because I do bump into the problem fairly regularly of having a lot of tests fail when I do a rewrite. I'd love to learn how to write better tests...and your last paragraph seems to scratch the surface of a superior approach.
- mhb 18y agoDive Into Python has a couple of good chapters on unit testing. Maybe you're looking for something more advanced though. If so, I think he gives some references for further reading. http://diveintopython.org/unit_testing/index.html http://diveintopython.org/unit_testing/index.html
- anthonyrubin 18y agoThe Google Testing Blog can be helpful: http://googletesting.blogspot.com/ http://googletesting.blogspot.com/
- nostrademons 18y agoSeconding this. I think the internal Google training materials were some of the best I've run across, and the Google Testing Blog is basically the public face of those.