Flask应用GitLab CI/CD测试失败(本地正常):多类问题求助
GitLab CI/CD 测试失败问题解决方案
1. 压力测试连接拒绝问题
原因:CI 任务中未启动 Flask 应用,Locust 请求 127.0.0.1:5000 时无服务监听。本地测试时手动启动了应用,但 CI 管道遗漏了该步骤。
解决步骤:
- 在压力测试脚本中先启动 Flask 应用并后台运行,确保服务就绪后再执行 Locust
- 用轮询方式替代固定等待时间,保证服务真正启动
修改后的stress_tests配置:
stress_tests: script: - nohup python src/app.py & # 替换为你的Flask实际启动命令 - while ! curl -s http://127.0.0.1:5000; do sleep 2; done # 轮询等待服务就绪 - locust -f src/tests/stress/company_stress_test.py --headless --host http://127.0.0.1:5000
2. Cypress 参数解析错误问题
原因:
cypress/included镜像已预装 Cypress,无需用npx调用- 功能测试任务未启动 Flask 应用,Cypress 无目标服务可访问
解决步骤:
- 直接使用
cypress run命令,去掉npx前缀 - 在功能测试任务中添加 Flask 应用启动步骤
修改后的functional_tests配置:
functional_tests: image: cypress/included:10.0.0 before_script: - apt-get update && apt-get install -y python3 python3-pip python3-venv - python3 -m venv venv - source venv/bin/activate - pip install -r requirements.txt - export PYTHONPATH=./src - nohup python3 src/app.py & - while ! curl -s http://127.0.0.1:5000; do sleep 2; done script: - cypress run --config-file src/tests/functional/cypress.config.js
3. Maven 目标版本错误问题
原因:Python 3.9 镜像默认安装的 Java 版本低于 21,与 Maven 编译目标版本不兼容。
解决步骤(二选一):
方案一:在 E2E 任务中安装 Java 21
e2e_tests: before_script: - apt-get update && apt-get install -y maven openjdk-21-jdk - export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 - export PATH=$JAVA_HOME/bin:$PATH - python -m venv venv - source venv/bin/activate - pip install -r requirements.txt - export PYTHONPATH=./src script: - cd src/tests/e2e - mvn test
方案二:修改pom.xml中的编译目标版本
将maven-compiler-plugin配置改为镜像支持的 Java 版本(如 11):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin>
4. 多 Docker 镜像的网络隔离问题
GitLab CI 中每个任务是独立运行的 Docker 容器,容器间默认网络隔离,不存在跨任务互通。因此每个测试任务都需在自身容器内启动 Flask 应用,不能依赖其他任务的服务。
正确配置原则:
- 每个测试任务(压力、功能、E2E)都要包含 Flask 应用启动步骤,后台运行后再执行测试
- 无需额外配置容器间网络,每个任务保持独立闭环即可
完整修改后的 .gitlab-ci.yml 示例
image: python:3.9 stages: - test - deploy # 复用Flask启动脚本的锚点 .flask_setup: &flask_setup - python -m venv venv - source venv/bin/activate - pip install -r requirements.txt - export PYTHONPATH=./src - nohup python src/app.py & - while ! curl -s http://127.0.0.1:5000; do sleep 2; done stress_tests: stage: test before_script: - *flask_setup script: - locust -f src/tests/stress/company_stress_test.py --headless --host http://127.0.0.1:5000 functional_tests: stage: test image: cypress/included:10.0.0 before_script: - apt-get update && apt-get install -y python3 python3-pip python3-venv - python3 -m venv venv - source venv/bin/activate - pip install -r requirements.txt - export PYTHONPATH=./src - nohup python3 src/app.py & - while ! curl -s http://127.0.0.1:5000; do sleep 2; done script: - cypress run --config-file src/tests/functional/cypress.config.js e2e_tests: stage: test before_script: - apt-get update && apt-get install -y maven openjdk-21-jdk - export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 - export PATH=$JAVA_HOME/bin:$PATH - *flask_setup script: - cd src/tests/e2e - mvn test
内容的提问来源于stack exchange,提问作者Cian Sullivan
相关产品推荐
相关产品推荐

