I’ve been turning something over in my mind recently, so I’m going to try and write down some of these thoughts to get some clarity around my thinking here. Now that my talks are over for a while, I’m able to dive back into some open source work (YAY!), and specifically I wanted to contribute back to Benchee, the wonderful Elixir benchmarking tool. I tackled a new feature to add some cross-platform system information to the UI output, and in working on that feature some interesting questions came up.
First off, testing private functions. Coming from Ruby, I never tested private functions. However, when we have code like the below, it makes me wonder if maybe we should!
Having pattern matching means that I can very easily test each branch of the logic involved in this part of the application in isolation. This is awesome - or is it?! Are private functions implementation details that shouldn’t be tested like they are in OOP, or are they just a way to limit your “interface”? Because of the nature of what we’re testing (system information), we can’t test the public function unless we test on macOS, Linux and Windows separately.
I ended up settling on not testing these private methods. First off, I do think they’re implementation details, and they don’t describe the public behavior of the application, so they shouldn’t be tested. If they’re tested for development, then those tests should be deleted before deployment. But the real calculus that I made was that the benefit from having those tests didn’t outweight the cost of keeping those tests around.
The next issue that I had here was that we’re having to test code that deals with the unpredictable outside world - specifically, we need to make system calls on all three platforms and then handle those system calls. Tobias had the great recommendation of pulling all this into a single function so we can keep our error handling in one place. But, how do we test this?
System.cmd/2 call relies on the system on which the code is run, we can’t control the output of that function. That means we can’t test the error handling without forcing the machine to fail somehow in a predictable way - and that’s tricky.
So, now I was thinking of two possible solutions:
1) Make this function pbulic and extract the reference to
System.cmd/2 out as an argument to this function so we can pass in a test double for that function that consistently behaves in the way we expect, or:
2) Extract the
if/else statement in that function into pattern matching, and test each branch individually.
Here’s what option 1 would look like:
And here’s what option 2 would look like:
After considering both options, I ended up going with option number 1. My thinking here was two fold. First off, I liked the “readability” of having all the logic in one function. It wasn’t too much logic, and it (to me) makes it easier to see the flow of control when using
if/else instead of pattern matching here. Second, though, I felt that just testing the handling of errors alone, and not unit testing
system_cmd/2, was a little odd. Maybe that’s a feeling that I’m still applying inaccurately from my OO experience, but to be honest, that’s why I went with that decision.
And of course, in the end, both are perfectly valid and there isn’t really a “correct” answer here ;)