hkrutzer
Integration testing is hard
For several years, I’ve found integration testing in Elixir to be very hard to do right and very painful. Now I don’t care much for integration testing but people always seem to want to test this way. They want to start a process and do some things to it, and then check for the side effects it causes somewhere else, probably several processes away.
For example, there is a process that handles user chat messages. The process also triggers telemetry based on the messages, and telemetry is captured by another process and queued up to be saved in an analytics database. So the test scenario is: start a chat process, send some messages, and ensure the database contains the correct statistics. I don’t think it is an especially good idea to test this way, but it doesn’t seem entirely unreasonable either.
But how do we do it? Mocking the repo or something near the database insert? Most of the mocking libraries are kind of iffy and can cause problems or don’t work async etc. Then there is Mox, but it only works with behaviors. So I’ve seen people add a @callback to a module, just so it can be mocked, there isn’t even a behaviour. I don’t like that because now we’re creating half of a behaviour just for tests. Additionally you need to put stuff in the Application env, causing clutter. That is two aspects of mocking with Mox that require adding test-only code in the regular (non-test) codebase. So I don’t want to do this because in most cases, other than Mox requiring it, there is no reason for adding a behaviour.
Then there’s the other obvious option which is add a lot of sleeps which is bad for obvious reasons.
Now we reach slightly more esoteric techniques like using :erlang.trace as described in e.g. this post. This is actually quite decent if you can use it. You find a process that is supposed to be called and ensure that it is in fact called, using assert_receive. If your process gets a lot of messages it can take a lot of time to get the right pattern for the assert because you have to fish it out of a very long message inbox printed in the terminal. I guess it’s actually only half-decent. And now the process is supposed to do a database insert. And database processes can’t be traced as easily because there is a pool of them. Back to square one.
Of course people are going to reply with stuff like “well in Javascript and Ruby you can just overwrite anything and that is bad because of reasons” and “in Java you have to have an IoC container and that is bad”. And the obvious “you are doing it wrong” / “just don’t test this way”. All I can say is, I respect and appreciate all the work various people have done to make testing in Elixir possible, and I like ExUnit, but in over 5 different programming languages I’ve used, these kinds of tests are the most painful in Elixir.
I guess it boils down to testing things that happen across processes is inherently hard. That’s why I try to avoid it, only test a single process / module as much as possible. But how do you convince other people to avoid these kinds of tests? In some cases they are not that hard to write, but you pay the price later anyway when you rewrite them and they are no longer easy.
Trending in Discussions
Other Trending Topics
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 10 of 17 Posts
iarekk
Please take my post with a pinch of salt, as I’ve read 2 books on Elixir, but have no experience of it in production/OSS otherwise.
After painfully going through similar questions you’re raising, I’ve arrived at the following so far:
Therefore, I’ve found it’s most practical to:
mix testdoes by default), and send test messages/observe side effects as the application is running. Most likely it means having a DB running as part of your build job etc.#2 assumes that the processes/services are very slim and delegate all decisions to the pure code. That’s not always easy as messages that get passed around are usually intertwined with the business logic.
I’m not sure I’ve made my peace with the above approach yet, but this is what the toolset has been pushing me forward. Larger projects on Github that I’ve looked at (Phoenix, Nostrum) all seem to be following similar philosophy.
hkrutzer
I think we’re in agreement. It’s best if you can cover every module separately and then all the separate test combined test the entire system.
With respect to running the database, that is not ideal but not that bad either. However, the challenge with that is when you have an interaction that goes across multiple processes; how do you know the DB driver has performed the insert you want to test, without a sleep?
benwilson512
In my experience, particularly if you’re doing an “integration test” you would not mock the database at all. Ecto sandboxes are designed to work with multiple processes. Start your processes you need for your test, put Ecto in either the shared mode or use allowances, and do a real “end to end” test.
LostKobrakai
I have a few principles I try to follow when testing processes / sets of processes:
Imo this is a fallacy. The usecase for behaviours is to provide some level of interface where multiple implementations are to be used. If you have one implementation running, that’s hard to test, and a different one meant to aid in testing that’s multiple implementations. Even if an implementation happens to only be used in testing it’s just as bad if the implementation starts to drift apart with what is expected from the implementation as it would be for one used in production.
I think the general arguments about unit tests vs integration tests apply here as well.
hkrutzer
Yes, and add sleeps to wait for the insert, which is undesirable. I don’t want to mock the database, I want to know when it has performed its tasks.
This looks like an actual fallacy namely circular reasoning:
Example
Of course I can test separately whether
That also means
While your other points are well taken I don’t see a solution for an end-to-end test of the above that doesn’t involve sleeps.
dimitarvp
Since when?
Repo.insertis synchronous, when the function call is completed the record is in the DB.No it doesn’t, Elixir behaviours are basically a form of programming by contract. They are not something that will ruin your life, they are used to (a) increase clarity on what does a certain agent in your program do and (b) help with mocking if you are so inclined.
D4no0
That mention baffles me too, the only case where I would see uncontrolled concurrency happening is when somebody would use something like GenServer.cast/2 and this points to a bad design, as that function doesn’t guarantee that the message was received by the process.
hkrutzer
Let’s try to maintain the level of the discussion. genserver cast is a normal OTP function which serves many purposes and is used very often.
hkrutzer
Obviously if you are calling a function which calls insert directly or performs a cast which leads to calling it. But once there is an asynchronous step somewhere, whether
insertitself is synchronous doesn’t matterdimitarvp
I think we should take a step back here because your comments strike me as a bit academic. Maybe we should discuss your particular hurdles when trying to test something concrete?
Discussions like these rarely are fruitful because everything carries tradeoffs – there’s no one single perfect solution. Though it’s also true there are a number of solutions that are objectively worse than others. If that’s your goal here – to uncover a subset of better solutions then OK but f.ex. arguing that “
Repo.insertcan actually be asynchronous” is not productive and won’t lead us to anywhere enlightening.