gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:46:07 24 4
gpt4 key购买 nike

我在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 worker ?

最佳答案

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

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

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

关于docker - 使用Docker Compose运行单个测试功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com