8 ms·
Unit tests are not a goal, they are a tool. Striving for 100% test coverage is nonsense, not testing your software at all levels is bad. Middle ground and moder
by jacquesm 6y ago
Unit tests are not a goal, they are a tool. Striving for 100% test coverage is nonsense, not testing your software at all levels is bad. Middle ground and moderation are where it's at, not a black vs white choice. Just like every other tool you should understand it's strengths and weaknesses and you should apply it properly, not dogmatically or it will bite you.
- alecco 6y ago> Striving for 100% test coverage is nonsense Tell that to SQLite guy.
- nottorp 6y agoSomething like sqlite can actually be unit tested to 95% or so. If you stretch the definition of 'unit test' a bit (don't mock i/o). Try that with a networked application that takes user input though...
- laumars 6y agoMocking networked services is something very much worth doing because you can then check you’ve set timeouts correctly, handling incomplete or junk responses gracefully etc. Those are the kind of hidden problems that can bite you on production deployments.
- nottorp 6y agoYou can mock networks all you want and you'll still cover only what you can think of. It will always break on the user's internet, because it's too diverse to predict. Doesn't mean you can't have some networking unit tests, just that you shouldn't believe in them too much. Edit: you said services. You thinking of server? I'm thinking of clients.
- tremon 6y ago"mocking networked services" is exactly what you would do when testing clients. And it doesn't have to be a static mock. It's not too hard to inject a fuzzer in your mock service response, although that's probably left to a separate testing routine, and not part of your unit test setup. But if you have no mock for your network service, you can't fuzz it either.
- ben-schaaf 6y agoStriving for very high test coverage seems like exactly the kind of thing you want for your database infrastructure.
- 1337shadow 6y agoAnd that's exactly why you need less tests in your project that uses a DB: there's no need to test the DB because it's covered by its own tests already.
- bpicolo 6y agoInteractions with your DB are often the most fragile piece of your code, because there's an impedance mismatch between the language you're writing and SQL. Some languages/frameworks abstract this more safely than others, though.
- 1337shadow 6y agoExactly, though I believe there's still a thin line between testing the interaction, and testing the db itself. Just like, the difference between testing some code, and testing the language itself.
- Jtsummers 6y agoInteraction, in general, is where many, if not most, errors lie. Unit testing verifies that things work in isolation. But if you code up your unit tests for two components that interact, but with different assumptions, then the unit tests alone won't do you any good: X generates a map like `%{name => string()}`, Y assumes X generates a map like `%{username => string()}`. Now, hopefully that won't happen if you're writing both parts at the same time, but things like this can happen. Now your unit tests pass because they're based on the assumptions of their respective unit of code, but put it together and boom!
- tanilama 6y agoTBH SQLite is suitable for 100% coverage. A lot of application code or workflow style code is hard to reach 100% coverage as they are rarely triggered.
- ben509 6y agoThe rarely triggered code is exactly where you want unit tests. That's an opportunity to describe, in code, what that path is supposed to do, and then make sure it does it.
- jfkebwjsbx 6y agoAnd yet they had bugs and could lose data. Which shows 100% unit test coverage is not better than spending that time in other kinds of tests.
- WrtCdEvrydy 6y agounit tests don't account for timing-based effects and you generally can't test for ACID properties.
- kelvin0 6y agoYou should definitely watch this presentation on SQLite exploits. SELECT code execution from using SQlite - DEF CON 27 Conference https://www.youtube.com/watch?v=HRbwkpnV1Rw https://www.youtube.com/watch?v=HRbwkpnV1Rw Don't get me wrong, it's a great product and I use it often, but 100% test coverage does NOT equal 100% safe.
- pjmlp 6y agoYeah, but those QA dashboards monitored by management....
- 1337shadow 6y agoExactly, the 80-20 rule also applies to unit tests. I don't have 100% coverage on big projects, but everything I write that's meant to go in production is in TDD anyway, so there's always enough tests to prevent a junior from breaking my stuff, and I save a lot of time because thanks to TDD I don't need to manually test much, and most of the times not at all, I just wait for user feedback: it's always much easier to have code working in real conditions, if it already works in test conditions, not the other way around.
- 2rsf 6y agothere is real world research that actually shows that the 20/80 works in unit tests, the last 20% hardly catches any issues or contribute very little to quality
- KingOfCoders 6y agoThe benefit of 100% test coverage is that there are no more discussions on what to test and what not. When in doubt, test it. In larger groups of developers there are otherwise ongoing discussions if A needs to be tested or not. I have seen culture wars around this, from people who don't want to test and are in the eyes of others always testing not enough and vice versa. Especially with a diverse development force with different ages, seniority and cultural background. It's often easier to just aim for 100% test coverage instead (with excluding some categories of files). EDIT: I would not and did not start with 100% unit testing. But if there are ongoing culture wars and discussions didn't lead to a workable compromise, 100% test coverage worked for me and after some days test coverage was a non issue.
- shawnz 6y agoEven with 100% coverage, that doesn't mean you've found every bug that can possibly be found with unit tests. Your tests could always cover more inputs, more situations, etc.
- KingOfCoders 6y agoNo, you can't find bugs that you can't think of. Property based testing and random values in unit tests find lots of bugs you didn't think of though.
- brlewis 6y ago> you can't find bugs that you can't think of Rather, you won't find bugs that you choose not to think of because you've let the "100%" number lull you into complacency, even though you know it's 100% of lines/branches, not 100% of inputs.
- KingOfCoders 6y agoYes. My problem in 40y of programming is still making bugs and those I make come from not thinking about edge cases or from wrong assumptions and not from being lulled into writing tests to meet a 100% number. But personalities differ and if being lulled into security by writing towards a 100% number is a problem for you I would be careful, I totally agree here.
- lloeki 6y agoI read this complementary one the other day, and one thing that is readily apparent to me is that a lot of people have a lot of different opinions about testing (move to system tests! more unit tests! regression tests!), but many are not asking the zillion dollar question: What are you testing for? This is critical because it basically gives you immediately what you should and should not test, and how. While mindless, dogmatic, metric oriented testing is a waste, testing with higher intent and purpose is extremely useful. An example: test that something working on current vX also works on vA to vW, and when vZ is out, have the answer readily. Or that a biz feature fulfills the requirements. Or that someone not as well versed on intricate details of your piece of ownership will be confident in that piece still working after a simple fix when you’re on vacation. It can be one, some, but probably not all. With that in mind, what to test, what doesn’t make sense to test, and what to test against becomes more clear: should I mock this? or should I run it against some staging environment? Should I perform (yikes? not!) manual testing? The answers are highly dependent on the piece of code being tested. Tests are here to help you answer a question, if you aren’t sure what the question is then your tests will miss the point. https://flak.tedunangst.com/post/against-testing https://flak.tedunangst.com/post/against-testing
- hnick 6y agoI feel like a lot of unit testing is just another form of bikeshedding. It's easily understood, everyone can talk about it, and you can spend a lot of time on it with no clear goal but feel like you're getting something done.
- majikandy 6y agoActually it is pretty common to have 100% coverage with some extra redundancy too (where some things get accidentally covered multiple times). Striving for 100% is indeed nonsense, but having 100% coverage is usually accidental in clean code that you want to work, and merely a by-product of TDD. I strive for working code. Sometimes I miss something in the TDD cycle and don’t have 100% and it is that which usually comes back to bite you. I have never found 100% test coverage has bitten me, dogmatic or otherwise.
- shawnz 6y agoIf it didn't catch any bugs, either during initial development or through later changes, then it bit you via wasting your time. I don't think it's fair to say that having tests necessarily makes the code under test any cleaner.
- mytherin 6y agoThat heavily depends on the size of your codebase and perhaps also the language you are writing in. Writing in C++, for example, I often have switch statements in the form of: switch(type) { case X: ... case Y: ... default: throw InternalException("Unsupported type!"); } Now if all goes well the default case will never be covered. At some point I thought "why have this code if it's not supposed to run; let's rewrite this so we can get 100% code coverage!", and I ended up with the following code: switch(type) { case X: ... default: assert(type == Y); ... } Now we can get 100% code coverage... except the code is much worse. Instead of an easy-to-track down exception we now trigger either an assertion (debug) or weird undefined behaviour (release) when the "not supposed to happen" inevitably does happen because of e.g. new types being added that were not handled before. Is worse code worth getting 100% code coverage? In my eyes, absolutely not. I think good code + testing should be able to reach at least 90% typically, likely 95%, but 100% is often not possible without artificially forcing it and messing up your code and/or making it much harder to change your code later on.
- tremon 6y agoWhy can't you have a test that checks if the correct exception type is thrown on invalid input? The exception is part of your API too.
- mcv 6y ago> "Striving for 100% test coverage is nonsense" 100% coverage of what exactly? Tests that go through all your lines of code without testing any of the logic, is useless. If you want to be thorough, you need to do mutation testing, which is a system that tests the quality your unit tests by mutating your logic (changing a > for >=, a + for -, etc) and then expects at least one test to fail. If no test fails, that piece of logic wasn't tested. Without that, it's entirely possible your high code coverage doesn't actually test anything meaningful. Also, this sort of logic is exactly the kind of stuff you want to unit test. All the standard plumbing boilerplate code is not something that needs to be unit tested. The logic does.
- 2rsf 6y agoand it's not just that, for example branch coverage only says that you branched but not how, a complex branch might need more than one test to be fully, or decently, tested.
- zzzcpan 6y agoRight, essentially you have to be aware that you are testing the state space the code might end up in, which is very different from just hitting every line of code or every branch.
- yaktubi 6y agoOn that note it is a great tool during development to get a piece working without connecting it to the broader application. Test driven development gives a nice debugging context that is easier to work with. The code coverage and regression part comes as a nice bonus feature. I’d also like to add that if you contribute code to an open source project it is extremely beneficial to have iron-clad unit tests. Since there is so many devs it would be easy for someone to accidentally break something you fixed already.
- DodgyEggplant 6y agoAnd like other tools their importance is part of the entire tool-set. In many shops tight schedules, management by Product managers or people who are too removed from code cause you compromise every other principle of responsible sane coding. When this happens, unit tests are your only shield from doom. If everybody knows and allowed to write sane, good code with reasonable time to build it, the unit tests are nice to have but not a must