Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing and Running Tests in Docker

Alexandre Figura
July 24, 2018
85

Writing and Running Tests in Docker

“How to Launch your Tests with Docker Compose: Best Practices and Traps to Avoid": that would make a great title for a book! Unfortunately, there is no such book available currently. In the meantime, we are all struggling on how to make our applications running in Docker… Because there is so many ways to do it, it’s often hard to find out what is the best way to do it.

But with some practice, and many trials/errors, some patterns take shape. That’s what we will see during this training session. We will start by writing some tests for a web application with Pytest. Then, we will automate them with Tox. And to finish, we will run them in Docker Compose. Our development workflow will be managed with Invoke, and our goal will be to have tests independent of the running environment, so they can be run both locally or on a continuous integration server (e.g., GitLab with Kubernetes runners).

Requirements:

- Python 3.6
- Tox (+ Pip + Virtualenv)
- Docker & Docker Compose
- Google Chrome
- Clone demo repository: https://github.com/arugifa/ep2018-workshop

[Workshop given at EuroPython 2018]

Alexandre Figura

July 24, 2018
Tweet

Transcript

  1. Writing and Running Tests in Docker Or How to Test

    your Web Application Seamlessly... Alexandre Figura & Steffen Neubauer @ SysEleven GmbH
  2. Agenda 2 1. Setting-up our development environment. 2. Writing tests

    with Pytest. 3. Automating tests with Tox. 4. Running our application in Docker Compose. 5. Managing our workflow with Invoke.
  3. Useful Links 3 1. Pytest documentation: https://docs.pytest.org/ 2. Tox documentation:

    https://tox.readthedocs.io/ 3. Docker documentation: https://docs.docker.com/engine/reference/builder/ 4. Docker Compose documentation: https://docs.docker.com/compose/ 5. Invoke documentation: http://www.pyinvoke.org/
  4. 4 Setting-Up Environment 1. Clone the demo application (blog +

    web API): git clone https://github.com/arugifa/ep2018-workshop git reset --hard setup 2. Install requirements: • Python 3.6 • Tox (+ Pip + Virtualenv) • Docker & Docker Compose • Google Chrome 3. Create a temporary virtualenv: virtualenv -p python3.6 venv && source venv/bin/activate
  5. 5 Writing Tests 1. Install Pytest + extensions: pip install

    requirements-test.txt 2. Have a look to existing fixtures: vim tests/conftest.py 3. Write tests: • Acceptance tests with Pytest-BDD for the blog, • End-to-end tests with Webtest for the web API.
  6. 6 Automating Tests • Write a Tox file (Tox.ini) with:

    • one Testing environment, • two Development environments: • One to be used locally (to get auto-completion in your IDE), • Another one to be used in Docker later on. • one Linting environment, • one environment to check Security Issues in dependencies.
  7. 7 Running Tests • Write two Dockerfiles: 1. One for

    Production: • Based on Python 3.6 Alpine, • With manage.py as entrypoint, • We should be able to configure the database connection with an environment variable, 2. Another one for Testing: • Based on the PROD image, • With test requirements
 and Tox installed inside the container. • Write a Docker Compose file
 (docker-compose.yml): • With two services: • One for the web application, • Another one for PostgreSQL. • Share your local source code with the container.
  8. 8 Managing Workflow • Write Invoke tasks (tasks.py): • Two

    tasks: • One to run the demo server, • Another one to run tests with Tox. • Both tasks should run in Docker Compose, • Provide a debug mode so we can: • manually execute manage.py or Pytest inside Docker, • and use PDB for troubleshooting bugs.