# Coverage Report for Elixir & Phoenix

## 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](https://github.com/parroty/excoveralls).

## Setup ExCoveralls to Existing Phoenix Project

You can check the settings section of ExCoveralls, [here](https://github.com/parroty/excoveralls#settings). Please refer to their newest settings, this settings might be outdated for later.

* Add this to **project** at **mix.exs**.

```elixir
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**

```elixir
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.

```json
{
  "skip_files": [
    "test",
    "deps"
  ]
}
```

* To test if it's already installed and works. You can run this command.

```bash
MIX_ENV=test mix coveralls
```

## Setup Github Action

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

```yml
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](https://codecov.io). The result will be like this.

![Codecov Result](https://cdn.hashnode.com/res/hashnode/image/upload/v1631603548908/6uEx8QA8Z.png)
 

**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.

%[https://github.com/berviantoleo/elixir-exploration]

## Thank you

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

![Thank you](https://cdn.hashnode.com/res/hashnode/image/upload/v1631603551600/IDpSHfKgw.jpeg)

<figcaption>Image from <a href="https://unsplash.com/photos/0YbeoQOX89k">Unsplash</a></figcaption>
