Home > database >  Why test stage fails in flask app in the Gitlab CI/CD?
Why test stage fails in flask app in the Gitlab CI/CD?

Time:03-27

I am learning GitLab CI/CD and I want to make a stage to run tests on my flask app. Without the test stage, everything works with the Gitlab CI/CD. Here is my .gitlab-ci.yml file:

 image: python:3.8-slim-buster
    
    cache:
      paths:
        - .cache/pip
    
    stages:
      - build
      - test
      - deploy_heroku
    
    build message-requests:
      stage: build
      script:
        - apt-get update
        - apt-get install -y --no-install-recommends gcc
        - apt install -y default-libmysqlclient-dev
        - pip3 install -r requirements.txt
    
    test message-requests:
      stage: test
      script:
        - python3 test.py
    
    deploy_heroku:
      stage: deploy_heroku
      image: ruby:latest
      before_script:
        - apt-get update -qy
        - apt-get install -y ruby-dev
        - gem install dpl
      script:
        - dpl --provider=heroku --app=message-requests --api-key=$HEROKU_SECRET_KEY

My pipeline results in this error on the test stage:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from app import app
  File "/builds/dave/message-requests/app.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

Locally tests are working fine. How can I use app data which was built in the build stage? I understand that is possible to run test.py in the build stage, but I heard that it is better to have a separate stage for testing. I think the solution would be with using artifacts, but I am not sure how they work and where the python application installs everything. As I have mentioned I am new to GitLab CI/CD, therefore, if I am doing something wrong I am happy to hear best practices.

CodePudding user response:

In GitLab, every job you create starts in a fresh environment. So, you have to install your requirements in each job.

Your test message-requests: job does not take any steps to install Flask or other dependencies. You should add those steps.

  • Related