AstonJ

AstonJ

What is your testing philosophy?

In the Udemy Elixir Bootcamp course we’re told something along the lines of:

Write doctests as if you were showing someone how your functions work. Write unit tests for behaviours that really matter to you inside that module.

How does everyone feel about this and what is your own philosophy when it comes to testing?

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

In my humble opinion, ‘test-driven’-development only works when you know beforehand exactly what you are going to solve, and how you are going to solve it. In practice, this is of course not the case; a project specification is far from constant.

I have worked with teams in the past that had far too rigid tests, which meant that every feature-change needed to be changed both in the code, and in, say, 20+ tests. As the ‘Why most unit testing is a waste’ paper (which the thread @StefanHoutzager linked to discusses in more detail) said: A test that never fails tells you just as little as a test that always fails.

Also, ‘code coverage’ is about as good an indication of how well-tested your application is, as ‘lines of code per day’ is for developer productivity.

What I do think is important is the mindset behind TDD, which is: Think before you code. In practice, I write most tests after I’ve written my code, but I do believe that tests are important. It is a delicate balance: Too little tests, and your application might perform weird at times you do not expect. Too much tests, and your application becomes hard to maintain. Sola dosis facit venenum.

I myself am a great fan of Hammock-Driven Development, because it results in better code even without requiring you to write all tests beforehand (and because hammocks are awesome, of course!):


As for testing in Elixir: I wholeheartedly agree with the statement you quoted above from the Udemy Elixir Bootcamp course. Doctests are great as examples and simple unit/regression tests. When you need to test some edge cases that arise in a more complex context, then these tests can better be written in their own test file.


Another thing: Many people that are new to Elixir think that the “Let it Crash” mentality also means to “Let it burn”. But I think that what we actually usually mean is: We write tests to avoid the simple bugs, but we won’t actively look for the Heisenbugs, because these will find you. It is ridiculously hard to spot a Heisenbug, and instead of wasting too much time on this (and still possibly missing one), it is better to just make you application fault-tolerant: prevent it from burning down even if it crashes.

10
Post #5
ibgib

ibgib

For me:

  • Unit Tests
    • Good for small, self-contained algorithmic functions, especially when enumerating multiple input permutations.
    • Fit very nicely with doctests, e.g. in my IbGib.Helper module.
    • TDD is very well suited for this, but not a hard and fast rule.
  • Integration Tests
    • What I use most of the time.
    • Can test primary workflows (i.e. non-edge cases)
    • Can act as examples of how to use a library.
    • Often use TDD because it’s a great guide, but again, not a hard and fast rule (especially for more experimental approaches).
  • Regression Tests
    • If I find any decent, non-trivial bug, always do TDD to clearly define expected behavior.
    • Don’t want to ever have a bug happen twice.
    • Any decent number of bugs in a given area indicates that more tests are needed, and possibly an architecture/design rethink.
NobbZ

NobbZ

Well it took me really a while to grok this, but I only can sign what you have learned at udemy.

A doctest is not to test you function via documentation, but it is to find flaws in your documentation after changing the API a bit.

Besides of this, my philosophy of testing is varying :wink:

I really do like to test before I implement, but thats not always possible.

At work we do only test a very small subset of critical functions and we do introduce regression tests as soon as there have been known bugs fixed.

In general I realized that I do test much less when I do have a very strong type system, Often I only have one or to tests that test rough behavior (valid input gives valid output and invalid input will throw an exception, stuff like this) while in languages with limited or no compile time types, I do much more testing and even try to do some randomized stress-tests to uncover hidden edge-cases, very similar to property based testing.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement