使用Docker Compose运行单个测试功能

时间:2018-12-08 01:29:50

标签: docker flask docker-compose gunicorn

我在Docker中启动并运行了我的flask webapp,并尝试实施一些单元测试,并且在执行测试时遇到麻烦。在我的容器启动并运行时,我运行以下命令:

docker-compose run app python3 manage.py test

尝试在test中执行我的manage.py函数:

import unittest
from flask.cli import FlaskGroup
from myapp import app, db


cli = FlaskGroup(app)

@cli.command()
def recreate_db():
    db.drop_all()
    db.create_all()
    db.session.commit()

@cli.command()
def test():
    """ Runs the tests without code coverage"""
    tests = unittest.TestLoader().discover('myapp/tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    return 1

if __name__ == '__main__':
    cli()

但是由于我的start.sh中有一个Dockerfile,所以它只执行我的Gunicorn start.sh,但没有运行我的测试。我只在控制台中看到以下内容,但没有跟踪我的test()函数。

[2018-12-08 01:21:08 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2018-12-08 01:21:08 +0000] [1] [DEBUG] Arbiter booted
[2018-12-08 01:21:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2018-12-08 01:21:08 +0000] [1] [INFO] Using worker: sync
[2018-12-08 01:21:08 +0000] [9] [INFO] Booting worker with pid: 9
[2018-12-08 01:21:08 +0000] [11] [INFO] Booting worker with pid: 11
[2018-12-08 01:21:08 +0000] [13] [INFO] Booting worker with pid: 13
[2018-12-08 01:21:08 +0000] [1] [DEBUG] 3 workers

start.sh:

#!/bin/sh

# Wait until MySQL is ready
while ! mysqladmin ping -h"db" -P"3306" --silent; do
    echo "Waiting for MySQL to be up..."
    sleep 1
done

source venv/bin/activate
# Start Gunicorn processes
echo Starting Gunicorn.

exec gunicorn -b 0.0.0.0:5000 wsgi --reload --chdir usb_app --timeout 9999 --workers 3 --access-logfile - --error-logfile - --capture-output --log-level debug

有人知道为什么或如何在现有容器中执行测试功能而不必再次启动Gunicorn工人吗?

1 个答案:

答案 0 :(得分:2)

我假设start.sh是您的入口点。如果不是,您可以将其作为入口点,而不是将其放在CMD中。

我们可以使用--entrypoint参数覆盖入口点脚本,如下所示-

docker-compose run --rm --entrypoint "python3 manage.py test" app