Coverage Report for Elixir & Phoenix

Coverage Report for Elixir & Phoenix

Play this article

Introduction

Hello, I am new at Elixir and Phoenix. I curious about DevOps aspects and Backend aspects. So, that is why I explore more about coverage report and automatic testing. I found great alternative about coverage report, that is ExCoveralls.

Setup ExCoveralls to Existing Phoenix Project

You can check the settings section of ExCoveralls, here. Please refer to their newest settings, this settings might be outdated for later.

  • Add this to project at mix.exs.
def project do
  [
    ..., # existing settings
    deps: deps(),
    test_coverage: [tool: ExCoveralls],
    preferred_cli_env: [
      coveralls: :test,
      "coveralls.detail": :test,
      "coveralls.post": :test,
      "coveralls.html": :test
    ]
    # if you want to use espec,
    # test_coverage: [tool: ExCoveralls, test_task: "espec"]
  ]
end
  • Add this to deps at mix.exs
defp deps do
  [
    ..., # existing dependencies
    {:excoveralls, "~> 0.10", only: :test},
  ]
end
  • Create new file with name coveralls.json in the root project. This settings to ignore test and deps folder become coverage area.
{
  "skip_files": [
    "test",
    "deps"
  ]
}
  • To test if it's already installed and works. You can run this command.
MIX_ENV=test mix coveralls

Setup Github Action

I use Github Action as my main CI/CD platform. Currently I use this configuration.

name: Elixir CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  build:
    name: Build and test
    runs-on: ubuntu-20.04
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres:13-alpine
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: postgres
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432
    steps:
    - uses: actions/checkout@v2
    - uses: erlef/setup-beam@v1
      with:
        otp-version: '22.2'
        elixir-version: '1.10'
    - name: Restore dependencies cache
      uses: actions/cache@v2
      with:
        path: deps
        key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
        restore-keys: ${{ runner.os }}-mix-
    - name: Install dependencies
      run: mix deps.get
    - name: Check Format
      run: mix format --check-formatted
    - name: Run tests
      run: mix coveralls.json
      env:
        MIX_ENV: test
    - name: Upload to Codecov
      run: |
        curl -Os https://uploader.codecov.io/latest/linux/codecov
        chmod +x codecov
        ./codecov

This configuration will automatically to add coverage results to Codecov. The result will be like this.

Codecov Result

Note: The configuration will work for public repository. If you use private repository, please consider modify the configuration. You need to change the line of ./codecov become ./codecov -t ${CODECOV_TOKEN}.

Here is my example app.

Thank you

Thank you for read this article. If have any comments to improve this article, feel free to comment here.

Thank you

Image from Unsplash