DEV Community

Vincent Nguyen
Vincent Nguyen

Posted on

Integration testing Phoenix application with Circle CI 2.0

Introduction

Writing test cases is a thing that a software developer should do. Writing tests to make sure the quality of project’s source code.

When developing application by Ruby on Rails framework, we often use Capybara.

So which library that we should use if we develop application by Elixir/Phoenix. Hound is an Elixir library for writing integration tests and browser automation. Hound is not a new library, I believe you guys can find a lot of tutorials on Internet that show how to use Hound in Elixir/Phoenix application.

In this post, I will show how to run the integration tests that written by Hound on Circle CI 2.0 for Phoenix application.

Circle CI 2.0

All we know when run integration tests on any CI environment, we need to config for the test can run on headless browser mode that will be setup before CI execute the test.

There are various tools out there to help with headless testing such as PhantomJS, Headless Chrome, Selenium. And there are also many ways to setup these tools on CI environment. For example: In Travis CI, we can use before_script block to run the shell script to install headless testing tool. And in Circle CI 2.0, they supports browser testing out of the box. Sound cool, right?

Getting Started

Specify correct image

To enable headless testing on Circle CI 2.0, the primary image has the current stable version of Chrome pre-installed (this is designated by the -browsers suffix). This means, your .circleci/config.yml file needs to utilize an image with the -browsers suffix.

version: 2
jobs:
  build:
    working_directory: ~/my_application
    docker:
      - image: circleci/elixir:1.5-browsers

Install Selenium driver

Hound use Selenium driver by default, so this step, I’m going to install Selenium on Circle CI 2.0:

steps:
  - checkout
  - run:
      name: Download Selenium
      command: |
        curl -O http://selenium-release.storage.googleapis.com/3.5/selenium-server-standalone-3.5.3.jar
  - run:
      name: Start Selenium
      command: |
        java -jar selenium-server-standalone-3.5.3.jar -log test-reports/selenium.log
      background: true

Run the test

The final step is put all configurations together:

version: 2
jobs:
  build:
    working_directory: ~/my_application
    docker:
      - image: circleci/elixir:1.5-browsers
        environment:
          TEST_DATABASE_URL: postgresql://developer@localhost/my_application_test?sslmode=disable
      - image: circleci/postgres:9.5-alpine
        environment:
          POSTGRES_USER: developer
          POSTGRES_DB: my_application_test
          POSTGRES_PASSWORD: ""

    steps:
      - checkout
      - run:
          name: Download Selenium
          command: |
            curl -O http://selenium-release.storage.googleapis.com/3.5/selenium-server-standalone-3.5.3.jar
      - run:
          name: Start Selenium
          command: |
            java -jar selenium-server-standalone-3.5.3.jar -log test-reports/selenium.log
          background: true
      - run:
          name: Install System Dependencies
          command: sudo apt-get update -qq && sudo apt-get install -y build-essential postgresql postgresql-client libpq-dev rake nodejs unzip
      - run: mix local.hex --force
      - run: mix local.rebar
      - run: mix deps.get
      - run: MIX_ENV=test mix ecto.create
      - run: MIX_ENV=test mix ecto.load -d apps/my_application/priv/repo/structure.sql
      - run: mix test

Conclusion

Getting Phoenix integration test and Circle CI to work in harmony is not difficult. Hope you enjoyed and feel free to let me know your comment. Thanks!

Source: https://thefirstapril.com/2018/01/18/Integration-testing-Phoenix-application-with-Circle-CI-2-0/

Top comments (0)