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.
defprojectdo[...,# existing settingsdeps: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
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 CIon:push:branches:[main]pull_request:branches:[main]jobs:build:name:Build and testruns-on:ubuntu-20.04services:# Label used to access the service containerpostgres:# Docker Hub imageimage:postgres:13-alpine# Provide the password for postgresenv:POSTGRES_PASSWORD:postgres# Set health checks to wait until postgres has startedoptions:>---health-cmd pg_isready--health-interval 10s--health-timeout 5s--health-retries 5ports:# Maps tcp port 5432 on service container to the host-5432:5432steps:-uses:actions/checkout@v2-uses:erlef/setup-beam@v1with:otp-version:'22.2'elixir-version:'1.10'-name:Restore dependencies cacheuses:actions/cache@v2with:path:depskey:${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}restore-keys:${{ runner.os }}-mix--name:Install dependenciesrun:mix deps.get-name:Check Formatrun:mix format --check-formatted-name:Run testsrun:mix coveralls.jsonenv:MIX_ENV:test-name:Upload to Codecovrun:|curl -Os https://uploader.codecov.io/latest/linux/codecovchmod +x codecov./codecov
This configuration will automatically to add coverage results to Codecov. The result will be like this.
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}.