jeudi 14 juin 2018

How to make use of docker to provide a clean environment for every test case?

Background:

  1. Service

    • I have a service with DB support and have the following properties:
    • It has some APIs and propagate changes to DB
    • I have a Dockerfile to setup such a service with a clean state
    • It will bind port 80 to the host to accept the API call
  2. JavaScript test

    • I have more than 1 test case
    • Each test case requires connecting to the service with a clean state and perform test and assertion
    • Although the test is written in JavaScript, I believe this is a general question and does not limit only to JavaScript.

Problem:

How should I setup the whole test suite so that for each test I can have a clean service environment?

Naive Solution come up so far:

  1. docker-compose Node.js and Service for each test

    • The idea is basically setup a docker-compose to link a Node.js container and the service container
    • Execute the single test case in the Node.js
    • Teardown afterwards
    • Cons: Now writing a test case in JavaScript involves a docker-compose.yml
  2. Use beforeEach and afterEach to setup and tear down the docker

    • Cons: Have to make sure mocha or other testing framework does not run in parallel, or otherwise it fails become the same port cannot be bind to the host
  3. Stub all request and resopnse

    • This is not an approach I will take become of some natures of the service itself and our team wants to use a real service environment to do so.

Any suggestion is welcomed. Thank you.

Aucun commentaire:

Enregistrer un commentaire