4 ms·
Yes poorly written or tautological tests are useless . But I think we already knew that. I primarily use unit tests as a way to clarify my own thinking about a
by Aqueous 13y ago
Yes poorly written or tautological tests are useless . But I think we already knew that.
I primarily use unit tests as a way to clarify my own thinking about a function. It helps to step through the process of what the function should return given what parameters it is given. Why is that psychological crutch useless? Is your argument that we all should work the same way? Can't we do whatever works?
- rubiquity 13y ago> Yes poorly written or tautological tests are useless > It helps to step through the process of what the function should return given what parameters it is given. Congratulations, you just described tautological unit testing. Unit testing should be about guiding design and describing the behavior of the subject under test. Focusing on input-output testing is exactly what leads to the "unit testing is worthless" blog posts.
- Aqueous 13y agoNot sure at all what you mean. Can you describe what "guiding design and describing the behavior of the subject" looks like from the unit test's perspective? How else are we to specify the behavior of a function than to specify, for a given combination of typed parameters, what is the return value? As the article points out it is impossible to exhaust the space of all possible combinations - but nobody is trying to do that, so it is a straw man. For me, I test the branches that are likely to occur in a production setting, and any obvious edge cases that might occur, but infrequently.
- jasonlotito 13y ago> Can you describe what "guiding design and describing the behavior of the subject" looks like from the unit test's perspective? That's the first part of unit testing. You write a test that fails. By doing that, you are describing what the API should look like. The unit test guides the design of the interface, and how that object works. People do this, even if they aren't unit testing. You've done this if you've ever written some code calling a method, knowing that the method isn't there yet. But you stub it in, and then go write the method.
- Aqueous 13y agoRight. And that's what I, and most other people who unit test, do. The unit test says nothing about how the object is implemented though - that's your job. As far as the unit test is concerned, the function is a black box. The unit test isn't concerned with the details of the implementation. For testing side effects (like the function's effect on a database) you sometimes work outside the function's parameters and return values. But the point remains - you are looking at what behavior the function exhibits from outside it.
- the_af 13y agoJust a nitpick: that's TDD, not unit testing in general. When unit testing guides the design, it's usually called TDD. It's perfectly acceptable to write unit tests _after_ the production code, and to not have them guide the design.
- jasonlotito 13y agoYou are right, of course. And that's what I meant. I just equate unit testing and TDD together so often that I tend to (incorrectly) equate the two.
- tunesmith 13y agoThat glosses over things too much. Tautological would be like testing the getters and setters of a DTO. Unnecessary. But testing the return value of a function that accepts parameters is not tautological if the function actually has a nontrivial implementation.
- RHSeeger 13y agoWhen you're implementing an algorithm (one of the places I'm most fond of making sure I have tests), focusing on input/output testing is precisely what you want to do. Partition your inputs into different ways the code needs to deal with them and test for each partition.
- metaphorm 13y agotautological tests are not necessarily useless. there is a lot of value in testing for unexpected behavior. the tautology SHOULD pass trivially, if everything behaves the way you expect it to. If the tautogical test fails that means your expecations are not correct and you need to learn more about the technology you're working with.