gitlab-ci.yml: 'script: -pytest cannot find any tests to check'

自作多情 提交于 2021-02-08 08:55:21

问题


I'm having trouble implementing a toy example that runs pytest within .gitlab-ci.yml

gitlab_ci is a repo containing a single file test_hello.py

gitlab_ci/
    test_hello.py

test_hello.py

# test_hello.py

import pytest

def hello():
    print("hello")

def hello_test():
    assert hello() == 'hello'


.gitlab-ci.yml

# .gitlab-ci.yml

pytest:
  image: python:3.6
  script:
    - apt-get update -q -y
    - pip install pytest
    - pytest # if this is removed, the job outputs 'Success'

CI/CD terminal output

$ pytest
=== test session starts ===
platform linux -- Python 3.6.9, pytest-5.2.0, py-1.8.0, pluggy-0.13.0
rootdir: /builds/kunov/gitlab_ci
collected 0 items

=== no tests ran in 0.02s ===
ERROR: Job failed: exit code 1

I'm not sure why the test did not run... pytest does not seem to recognize test_hello.py


Solution

Put the python file inside the newly creared tests folder:

gitlab_ci/
    .gitlab-ci.yml
    tests/
        test_hello.py

Modify gitlab-ci.yml in the following manner:

# .gitlab-ci.yml

pytest:
  image: python:3.6
  script:
  - apt-get update -q -y
  - pip install pytest
  - pwd
  - ls -l
  - export PYTHONPATH="$PYTHONPATH:."
  - python -c "import sys;print(sys.path)"
  - pytest


And test_hello.py would stay the same as before.


回答1:


This blog post mentions a similar pipeline, but:

However, this did not work as pytest was unable to find the ‘bild’ module (ie. the source code) to test.
The problem encountered here is that the ‘bild’ module is not able to be found by the test_*.py files, as the top-level directory of the project was not being specified in the system path:

pytest:
  stage: Test
  script:
  - pwd
  - ls -l
  - export PYTHONPATH="$PYTHONPATH:."
  - python -c "import sys;print(sys.path)"
  - pytest

The OP kunov confirms in the comments:

It works now! I put the single file inside a newly created folder called 'test'



来源:https://stackoverflow.com/questions/58195130/gitlab-ci-yml-script-pytest-cannot-find-any-tests-to-check

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!