7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Testing in Elixir

Testing is a first-class concept in Elixir. In this lesson, we're going to learn about the ExUnit framework and how to write tests correctly in your documentation.

2.5 Testing in Elixir

Hi and welcome back to Get Started With Elixir. In this lesson we are going to look at testing which has a very high standing in the Elixir community. The main test tool in Elixir is called ExUnit. To start it you will have to call ExUnit.start in your test file. Next automatically sets this out for you and the test file. A test looks very simple. Let's look at the test that was generated automatically for a project in the last test. It defines the module that is usually named like the module you want to test. Rest of our test and then it uses the ExUnit.Case module, to make it a test module. The use macro is quite similar to the import directive. Within the module there are a number of calls to test with a description, and a block. Those are your tests. Each test needs to have one or more assertions, that verifies the code you're testing is doing what you expect it to do. In this example we just have one test and it verifies that 1 + 1 = 2. Let's jump back into the shell and learn how to run the tests. It is a simple mix task so you just run mix text and it runs two tests. Wait, why two? The reason for that is because I omitted one line that is also present in the example test file. Let's have a look. It is doctest Coder. This little line sets up inline doc testing for Elixir which is a really awesome feature. It essentially allows you to make sure your documentation provides correct and working examples. Let's look at the auto generated coder.ex file. Here is an example how a function that returns the world item. In its documentation there is an example that uses the IEX syntax. Whenever this is present in the documentation It will be run as a doc test, and compare the output of the function of the next line. Let's change the documentation to world2. The test will now fail, because we expected a different return. Might I add, it failed with a very useful error message. Let's practice this by adding a test file to our encoder class. I'm going to add a new test file in the coder name space, and just add the use macro and the doctest call. In the encoder module, I can now add some documentation, providing parameters and examples. In my examples I am using the hello Elixir string and expect the output to be the very best version. Let's run the tests. And they fail. We still have the configured prefix preprendent. I'm going to get rid of it. While I'm at it I'm also adding some documentation for a second function to JSON. Note that you have to use triple back slash to escape the quotes properly. Because you also have to escape the single black slash that would normally be there. When we run all the tests, everything passes. To recap, testing has a high standing among the Elixir community. ExUnit is the provided test framework. You can write classic tests in the test file or use doctests. Doctests are part of the documentation and verify that the function works as described. You should doctest all your public functions. In the next lesson we are going to start looking at concurrency. With the first lesson about processes. See you then.

Back to the top