How to add test coverage report to Phoenix project.

Phoenix is a web framework for Elixir language.  In this post, we will see how to add test coverage report support with excoveralls library. Follow below steps.

Add excoveralls as a (Development) Dependency to mix.exs

Open mix.exs file and find the "deps" function

 def deps do

Add the following line to the end of list

{:excoveralls, "~> 0.11.2", only: [:test, :dev]} # tracking test coverage

Additionally, find the def project do section (towards the top of mix.exs) and add the following lines to the List:

  test_coverage: [tool: ExCoveralls],
  preferred_cli_env: [
    coveralls: :test,
    "coveralls.detail": :test,
    "coveralls.post": :test,
    "coveralls.html": :test
  ]

Then, install the dependency on excoveralls we just added to mix.exs:

mix deps.get

You should see following

 Resolving Hex dependencies...
 Dependency resolution completed:
 * Getting excoveralls (Hex package)
 ... etc.

Create a new file named coveralls.json

In the "root" (base directory) of the Phoenix project, create a new file called coveralls.json and copy-paste the following:

{
  "coverage_options": {
     "minimum_coverage": 100
 },
    "skip_files": [
    "test/"
  ]
}

This config file instructs coveralls app to require a 'minimum_coverage' of 100% and to ignore the files in the /test directory for coveragge checking.

Run the Tests with Coverage checking

To run the tests with coverage, copy-paste the following command into your terminal:

MIX_ENV=test mix do coveralls.json

To view coverage in a web browser, run following

MIX_ENV=test mix coveralls.html && open cover/excoveralls.html

You should see coverage report in browser as below.

Anil Wadghule

Anil Wadghule