3 ms·
> All new lines must be executed at leaat once. What if you write a function in Go that's littered with the usual if err != nil { return err } and th
by majewsky 9y ago
> All new lines must be executed at leaat once.
What if you write a function in Go that's littered with the usual
if err != nil {
return err
}
and the error that's being propagated is hard to reproduce, e.g. a filesystem error? I never cover those "return err" in actual programs.
- henrik_w 9y agoI sometimes temporarily modify the program by for example misspelling a file name of a data base column name just to provoke an error that will execute the error handling code. Or changing a condition from == to != to cause an exception. Not ideal, but at least the new code is executed at least once, so I see that it executes as expected. Creating tests for some of these cases would involve mocking things out, and then you have to make assumptions that may not hold anyway. That's why I like to see it execute in the real environment at least once.
- vram22 9y ago>I sometimes temporarily modify the program by for example misspelling a file name of a data base column name just to provoke an error that will execute the error handling code. Yes, a lot of things like that are possible. I remember in one client-server project I worked on, we unplugged the network cable from one PC (while the program was communicating between client and server), to see if we got the correct error message that we had written for that case. And did other stuff like that.
- vram22 9y agoTo continue the anecdote: This was done in a project I handled, for the World Bank. I had a team of 9 motivated though young developers (first project for most of them). Near the end, before we sent the software to the client, a colleague was assigned to test the software - testing by a member external to the team, a slightly senior guy, ex-CITIL (a Citi IT subsidiary which had more mature software processes than we did then), though of course we did test the software ourselves a lot, first. I remember what happened: We prepared the machines and software for the test. Called him over. He said: I am going to sit down and destroy your software. We said: Go ahead, please try. And he tried to. But could not. He only found a few minor cosmetic defects. Getting up after a few hours of rigorous testing, he said something like - I am happy with your work.
- mustardo 9y agoConsider using a language that has exceptions instead of 1980's error codes
- SwellJoe 9y agoYou still have to mock something to test the error path. I don't see how exceptions make that go away, they just make it look slightly different.
- dullgiulio 9y agoSetting aside the fact that your question is loaded ("littered" with incorrect error handling--OK): 1. Return something meaningful, not the bare error you received above. 2. If you do nothing but decorate the error, ignore coverage. Having decorated the error helps debug the full system when the error occurs. 3. If your error handling is more complex (remove temporary files, close handler, etc.), you might want to factor the cleanup into its own function and test it there. Finally, your code is not really "littered." If you had exceptions, there would be no trace of the possible errors that might occur on the production system. Your coverage will be higher, but when the software will blow up in production, it will not matter then least.