DEV Community

Cover image for Coverage Report for Elixir & Phoenix
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

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.

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
Enter fullscreen mode Exit fullscreen mode
  • Add this to deps at mix.exs
defp deps do
  [
    ..., # existing dependencies
    {:excoveralls, "~> 0.10", only: :test},
  ]
end
Enter fullscreen mode Exit fullscreen mode
  • 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"
  ]
}
Enter fullscreen mode Exit fullscreen mode
  • To test if it's already installed and works. You can run this command.
MIX_ENV=test mix coveralls
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

GitHub logo berviantoleo / elixir-exploration

Explore more about elixir

ElixirExploration

codecov

To start your Phoenix server:

  • Install dependencies with mix deps.get
  • Create and migrate your database with mix ecto.setup
  • Start Phoenix endpoint with mix phx.server

Now you can visit localhost:4000 from your browser.

Ready to run in production? Please check our deployment guides.

Blog Post

Part of this post

Learn more

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

Top comments (1)

Collapse
 
hminy572 profile image
hminy572

Thanks for sharing!