运行 个使用 docker compose 的单独测试函数

Running individual test functions using docker compose

我启动了我的 Flask Web 应用程序并在 Docker 中 运行 并尝试实施一些单元测试,但在执行测试时遇到了问题。当我的容器启动并 运行ning 时,我 运行 以下内容:

docker-compose run app python3 manage.py test

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

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()

但是因为我的 Dockerfile 中有一个 start.sh 它只执行我的 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 worker?

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

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

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