Testing side effect free internal functions

61 views
Skip to first unread message

Eric Merritt

unread,
May 19, 2016, 11:20:46 AM5/19/16
to elixir-lang-talk
Guys,

How do we test internal functions? To give you background, we go through a lot of effort to make sure we split the functions in our modules into side effect free and side effect full functions. In Erlang, at least, we test the side effect free functions using things like eunit and quick check. For the side effect full functions we stand up the system and run integration tests. We can do that cheaply as we spent a lot of effort to make sure our systems can be stood up as needed.

The problem is that the side effect full functions tend to be the external api of the module and declared with 'def'. While the side effect free functions tend to be internal and declared with `defp`. The common response is to use mocking and just test the external api, the internal api will get tested as a side effect. We don't use mocking unless its mocking a service. I wrote up why at one point and that write up is easy to find, so I don't want to argue the validity of the approach here.

So that brings up how we go about it solving this testing problem. I can think of three possible ways to do that.

1) Just use `def` instead of `defp` and document what the external functions are -- Problematic for a number of reasons
2) Write a macro that looks at an environment variable and, if we are in test exposes everything -- Our test and prod systems would look significantly different though
3) Split all of our modules into API modules and internal modules -- This is what we do in Haskell and probably the least invasive approach.

I wonder if there are elixir specific pros and cons here I should be aware of, alternatives? etc.
Eric

Sent from ProtonMail, encrypted email based in Switzerland.

José Valim

unread,
May 19, 2016, 11:27:35 AM5/19/16
to elixir-l...@googlegroups.com
I personally go with 3.

If you have just one function private function you would like to test, you can also make it public but add "@doc false" so it doesn't show when generating documentation and so on.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/z8nxswLVL4YJHzzmtqkycd8CvrzNNAZGi2F5QCig1X_VpOLCB69OgFm5suhoiVvYpPOkYr83rn8RgRwFqculxg%3D%3D%40merritt.tech.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages