LoginSignup
7
6

More than 5 years have passed since last update.

Docker Composeを使用してDjangoのテストを実行する

Posted at

DjangoでCIを回すときにDocker起動してテスト実行したいなと思ってちょっと試してみました。

簡単なサンプル
source

redisとかの接続してテストもしたかったのでDocker Composeを使用してコマンド一発で行えるようにしました。

docker-composepip install docker-composeでインストールしています。

  • docker-composeのconfig
docker-compose.yml
web:
  build: .
  ports:
    - "8000:8000"
  links:
    - redis
  volumes:
    - app:/app
redis:
  image: redis
  ports:
    - "6379:6379"
  • webのDockerfile
FROM python:2.7
ADD app /app
WORKDIR /app
RUN pip install django redis
CMD cd /app && python manage.py test

Djangoはstart projectしたものに下記のテストファイルを追加しただけです。

tests.py
from django.test import TestCase
import redis


class Test(TestCase):

  def setUp(self):
      self.r = redis.StrictRedis(host="redis")
      self.r.flushdb()

  def test_ok(self):
      self.assertEqual(1, 1)

  def test_error(self):
      self.assertEqual(2, 1)

  def test_redis(self):
      self.assertEqual(self.r.get("name"), None)

      self.r.set("name", "name")
      self.assertEqual(self.r.get("name"), "name")

サンプルのリポジトリのところで、docker-compose run webを実行すると下記のようになりテストが実行されます。本当はmysqlとも連携したかったけどコネクトできなくて諦めました。databaseはなんでもいいというならsqlite3のほうが作成が早いのでテストはsqlite3のほうがいいですしね。

$ docker-compose run web

Creating dockerdjangotest_redis_1...
Building web...
Step 0 : FROM python:2.7
 ---> 38960da60ac6
Step 1 : ADD app /app
 ---> 8176a8eb5810
Removing intermediate container f0dcd557a477
Step 2 : WORKDIR /app
 ---> Running in 13b39ca62c46
 ---> 237ded13f022
Removing intermediate container 13b39ca62c46
Step 3 : RUN pip install django redis
 ---> Running in 286f081c5147
You are using pip version 6.0.8, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting django
  Downloading Django-1.8-py2.py3-none-any.whl (6.2MB)
Collecting redis
  Downloading redis-2.10.3.tar.gz (86kB)
Installing collected packages: redis, django
  Running setup.py install for redis

Successfully installed django-1.8 redis-2.10.3
 ---> 2e59a3a2bc76
Removing intermediate container 286f081c5147
Step 4 : CMD cd /app && python manage.py test
 ---> Running in f164c5af9ec0
 ---> 49ae601130fb
Removing intermediate container f164c5af9ec0
Successfully built 49ae601130fb
Creating test database for alias 'default'...
F..
======================================================================
FAIL: test_error (app.tests.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/app/tests.py", line 15, in test_error
    self.assertEqual(2, 1)
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 3 tests in 0.006s

FAILED (failures=1)
Destroying test database for alias 'default'...
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6