Elixir Testing-Running Only Specific Tests.

Stop coding if you are not writing the test cases.

Google image search Testing…

This article comprises three major topics.

  1. How to run the specific test cases?
  2. How to use tags in test cases? and
  3. How to configure the tests?

When I first started writing the code, I spend most of the time in writing the code only. I did not even think of writing the test cases. One fine day, I read a sentence “ If you are not writing the tests , stop coding”. This quote really impressed me in writing the tests. So, I started hunting the geeks’ libraries.

I have gone through some libraries written by the great coding geeks. They really care about writing the test cases. However, don’t waste your time in writing the test cases. Keep a parallel progress in coding and testing as well.

Elixir comes with nice Testing Framework called ExUnit. If you do not know how to write the test cases, this article really helps you.

You can run the specific tests in different ways. Let’s get into it…

A File with Specific Test Cases

You can write all the test cases inside a file and test them with command

$ mix test path_to_your_file.exs

This will run all the tests defined inside the file. 
We do some coding stuff here…

We will create a file my_tests.exs and add some unit test cases. If you are a smart lazy guy, you can copy and paste it.

# Filename: my_tests.exs
ExUnit.start              # Start the ExUnit
defmodule MyTests do
use ExUnit.Case # Using the test frame work here
test "test1" do
IO.puts "Test 1"
assert true
  test "test2" do
IO.puts "Test 2"
assert true
  test "test3" do
IO.puts "Test 3"
assert true

Here you cannot use the mix test my_tests.exs because we are not inside the mix project. However, you can execute all the tests with a command

$ elixir my_tests.exs

Make sure that you are in the same directory of the file my_tests.exs. You will see the output like in the following screen shot.

Code + OutPut

This is how you run only specific tests by isolating them from other tests. You can make as many test files you want.

Next, we will go with the mix project for tasting the advance style of testing. Create one Mix project as mix new my_project and navigate to the directory test.

$ mix new my_project  # this creates the project
$ cd my_project/test # change the directory to the test
$ ls
my_project_test.exs test_helper.exs

The test directory comprises two files. The file test_helper.exs will set the default configuration of all tests. This contains only one line of code ExUnit.start and the file my_project_test.exs is where our real test cases sit.

So basically, when you run the mix test task, it starts the current application, loads up test/test_helper.exs and then requires all files matching the test/**/_test.exs pattern in parallel. It loads all the files the have the suffix as _test.exs

Testing the test case at specific Line Number

Now coding begins. You open the file my_project_test.exs and replace all the lines of code with the following lines

defmodule MyProjectTest do
use ExUnit.Case
doctest MyProject
test "test at line 5" do
IO.puts "testing test at line 5"
assert 1 + 1 == 2
test "test at line 10" do
IO.puts "testing test at line 9"
assert 1 + 1 == 2
test "test at line 15" do
IO.puts "testing test at line 14"
assert 1 + 1 == 2

Here you cannot see the line numbers. So, I am sharing the screenshot of the file which displays the line numbers.


Our file contains test cases at line numbers 5, 10 & 15 . Now we will run only the test at line number 10

Change your directory to the root of the project. If you are already inside the root directory, skip this command

$ cd path_to_my_project

After changing directory, run the command as

$ mix test test/my_project_test.exs:10

You will see the output like in the screenshot…

mix test tes/my_project.exs:10 // OutPut

Did you observe that, we have 4 tests and only one is tested and the remaining 3 are skipped. We have added 3 tests and by default we have one test case. Don’t get confused here…

$ mix test test/my_project_test.exs:10
$ mix --only line:10 test tes/my_project_test.exs
#both resembles the same

Running the Specific tests using the tags

Now we will take our testing to the next level. ExUnit provides tags and filtering functionality that allows developers to select which tests to run.

In the file my_project_test.exs we will make some changes. Changes include adding the tags to identify the test cases.

We will add different tags for clear understanding the tags concept.

Note: By default, all the test cases are included.

You can add the tags inside the module before the test macro like

@tag tagname: true/false
#name and value can be anything just for my conveince I am using true/false

File after adding the tags will look like following…

defmodule MyProjectTest do use ExUnit.Case
doctest MyProject
  @tag runnable: false
test "test at line 5" do
IO.puts "testing test at line 5"
assert 1 + 1 == 2
  @tag runnable: true
test "test at line 10" do
IO.puts "testing test at line 10"
assert 1 + 1 == 2
  @tag runnable: true
test "test at line 15" do
IO.puts "testing test at line 15"
assert 1 + 1 == 2

Here we added the @tag runnable: false/true to the test cases. Now we filter the test cases using the filters like include and exclude. As I already mentioned by default all the tags are included.

We will run the tests whose tag runnable: true. According to our code
the last two tests must run.
Use the following command

$ mix test --include runnable:true  #no space after :

Even though we added include filter, it is still running the all tests. This is because as I already mentioned by default all tests are included irrespective of their tag and value.

So, to run specific tests first we will exclude them and run include tags.

$ mix test --exclude runnable --include runnable:true

In the above screen shot we first excluded the tests with tags runnable and next we included only runnable: true tests.

ExUnit also provides --only option to run only specific tests.

$ mix test --only runnable:true
$ mix test --only runnable:false

This command will run the tests with tag runnable: false means only one test must execute here…


Setting Default Configuration

You can also add the default behavior in the test_helper.exs. Suppose if you want to exclude some particular tests, add this code in your test_helper.exs . As by default all the tests are included, even if you specify the include option that does not make sense.

# Exclude all runnable: false  tests from running 
ExUnit.configure exclude: [runnable: false]

This will apply for every test in every file in the test directory.

Hope it helps you apart…

Happy Coding !!

love to share