【发布时间】:2018-12-08 01:29:49
【问题描述】:
我在 Docker 中启动并运行了我的烧瓶 webapp,并尝试实施一些单元测试,但在执行测试时遇到了问题。当我的容器启动并运行时,我运行以下命令:
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 工作人员?
【问题讨论】:
标签: docker flask docker-compose gunicorn