Live data from Hacker News

Most Unit Testing Is Waste (2014) [pdf]

rbcs-us.com

121–130 of 164 posts

Re: Most Unit Testing Is Waste (2014) [pdf]

#121
post #42

Absent from this discussion is the use of generative testing. At the unit level they can automatically enumerate a variety of scenarios the programmer would likely overlook.

I just discovered this, only it was called 'property based testing'. Do you recommend any resources for learning to use it effectively? I haven't dug in yet, but so far it hasn't clicked when thinking about it. I like the idea a lot, but I guess I don't see where it would be most useful for my own code.

Re: Most Unit Testing Is Waste (2014) [pdf]

#122
post #99
post #65

Earlier quoted context omitted.

To me this suggests that there is no clear understanding what your units are supposed to do. My second theory would be that the unit tests are written by the inexperienced developers while the good developers write the other code.

The tests will involve 5 mocked dependencies just to test a simple if statement. They test the implemention and are a waste of time. Isolated units that are well tested usually don't need to be updated in my experience so they rarely break.

Tests that have 5 mocked dependencies is usually a signed we done messed up somewhere.

Re: Most Unit Testing Is Waste (2014) [pdf]

#123
tl;dr I made it to "Testing does not increase quality; programming and design do" and quit.

It seems clear that the author has a strong opinion on this and perhaps that has been formed by exposure to unit tests done wrong. I suppose his article is worth reviewing and asking if any of my unit tests suffer from the problems he identified. I think most of his complaints relate to badly applied unit tests and I think we can all agree that any methodology can be badly applied. That does not provide grounds to condemn the methodology.

Having used Fortran (and Macro-11) as the first languages I employed professionally, I do not recall any thrust for unit testing at that time. Maybe it was just the shop I worked in. More recently I have used unit tests for Go, C/C++, Python, Perl, Java, shell scripts and probably some I'm forgetting. I wouldn't consider coding anything w/out some kind of unit test.

Back to the quote "Testing does not increase quality; programming and design do", I disagree vehemently with the claim that testing does not increase quality. While true that one cannot 'test in' quality, I find that designing code to be testable provides higher quality results. This is particularly true in languages such as shell scripts that often start small and grow until they are hundreds of lines of in-line code. Testable code is generally better partitioned and structured than it would otherwise be.

A second benefit to unit testing is immediate feedback and completion of parts of the system. I get a feeling of accomplishment when I complete something that passes its tests and prefer that to deferring satisfaction until the entire thing works.

Finally, if I test the bits in isolation, I can provide them data for all of the corner cases I think could cause trouble and make sure they work for a wider range of inputs than could easily be done during integration testing. When I do get to the point where I put the bits together, I have a much higher success rate with integration testing.

Re: Most Unit Testing Is Waste (2014) [pdf]

#124
post #9

I got progressively more and more frustrated with this article, largely because he keeps making statements about the impossibility of covering all states a class may take on (true!) but then followed up with espousing more use of integration and system tests, which are clearly combinatorial harder to test “completely”. He also implied at the very beginning that somehow this was driven by the switch from FORTRAN to OO…

For the same number of lines of test code, an integration test case traverses through much more source code than a unit test case. Not only that, but it also checks that the units are interacting properly and passing each other the right data and returning the right data. Good integration tests can also fee us from static typing because the integration tests implicitly check that the interface contracts between all t…

True, but they're also slower, so you'll have to either test fewer cases or run the tests less often.

Re: Most Unit Testing Is Waste (2014) [pdf]

#125
post #19

> If you find your testers splitting up functions to support the testing process, you’re destroying your system architecture and code comprehension along with it. Test at a coarser level of granularity. The emphasis here should be on the reason for splitting up functions. Long, complex functions can be difficult to understand, and removing a few lines in exchange for a (well named) function call is very beneficial fo…

It's quite the opposite: The whole point of the architecture is often to facilitate testing. If we didn't have to write tests we could do without a lot of that boilerplate.

Re: Most Unit Testing Is Waste (2014) [pdf]

#126
post #96
post #92

Earlier quoted context omitted.

The added "difficulty" is just the cost of doing a proper, correct refactor. Dynamic languages are not helpful just because they let you write bugs faster.

Developers are expensive though. If dynamic languages let you write buggy code in half the time that it would take to write perfect code in a static language, that would be an acceptable trade-off in many contexts.

> If dynamic languages let you write buggy code in half the time that it would take to write perfect code in a static language, that would be an acceptable trade-off in many contexts.

Shit at 50% off is still shit. You're not being paid to produce shit, even if you might be really, really efficient at doing so.

Re: Most Unit Testing Is Waste (2014) [pdf]

#127

One of the things I dislike the most about unit tests is how it's used for "quality theater". Some examples: - Trying to use coverage percentage metric as a sign of quality. As if a simple percentage means anything about the quality of the tests. It's as useless as using lines of code as a way to measure progress. - Not recognizing that useless unit tests are harmful for the code maintenance. It makes refactoring cod…

> I really hate useless unit tests. I've seen tests that setup a mock, configure it to return a value when called, call the code using the mock, then check the returned value is the mocked value. This tested absolutely nothing! I've had a similar disagreement about the validity of such a test. The other mocking issue I've seen, is people mocking blackbox third-party APIs over which they have absolutely no control, wh…

I'm with you people on this mocking issue. It is about time that mocking be recognised as an anti-pattern in automated testing, or at least as an undesirable tool of last resort.

Mocks often contain the same bad assumptions and misunderstandings about the mocked API which the developer used during the implementation of the unit they are trying to test.

If you feel the need to mock something then you should first ask yourself whether an integration test can do the job for you. Actually, I would generalise this advice to: Don't write a unit test when you can write an integration test.

Re: Most Unit Testing Is Waste (2014) [pdf]

#128
post #38

Earlier quoted context omitted.

This is no more or less likely in an integration test vs a unit test. One of the first things you learn in writing unit tests is that you should be selective in picking your inputs. This is even taught in those “trade schools” he subtly derides. You regularly see both public test cases and hidden test cases run against problem sets, at least that’s what my interns tell me.

Interesting so they don't teach testing by deliberately trying to break things, pasting Arabic text into an address field for example. When testing NAPI like to use edge cases like the company with the longest name eg "Donaudampfschiffahrtsgesellschaft" or the Famous Welsh location "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" I am pretty sure I crashed an A17 clearpath mainframe by doing aggressive te…

This is the sort of input that you'd selectively pick in a unit test, too. What you don't do is also picking inputs like "llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" too (all lowercase) :)

Re: Most Unit Testing Is Waste (2014) [pdf]

#129

Earlier quoted context omitted.

Unit tests don't guarantee that your code units are well designed. You can still write unit tests for spaghetti code. In fact, sometimes it encourages it because it encourages antipatterns like dependency injection; because developers get the urge to inject mock objects into the units from the unit tests. Just think about it at a high level. If your module depends on another module, is it really always necessary that…

I actually don't get where you are going with this breakdown. Can you give me an example.

I mean when you have a class which, instead of importing/including/requiring its dependencies directly, expects instances of various classes to be passed into its constructor for example. So the class cannot do its job unless you pass its dependencies into its constructor... This is an antipattern which unit tests tend to encourage because it allows you to decouple the unit logic from its dependencies and it makes it easy to inject mocks in the place of dependencies.

A unit test should only test a single class so that means you need to mock out all other classes which your class depends on. Mocking out dependencies in the test code is difficult or not feasible and that's why developers often resort to Dependency Injection in their source code but as mentioned before, it is an antipattern.

Also, I noticed that a lot of developers confuse unit tests with integration tests but the definition is quite clear: If your test covers more than one class without mocks, then it's an integration test. Integration testing does not necessarily mean end-to-end testing. It could just be a single class with its internal dependencies.

Re: Most Unit Testing Is Waste (2014) [pdf]

#130
post #59

Earlier quoted context omitted.

If you don't use mocks then won't you just end up with integration tests?

Who cares what they’re called as long as they’re fast and don’t crash when running at scale/fill the disk or memory

Because if you're not using mocks the tests get unweildy. Suddenly changing components three layers down the dependency tree breaks "unit" tests at the top. Those are the tests you just end up @ignore'ing.
Post reply on HN