gitlab-ci.yml:'脚本:-pytest无法找到要检查的任何测试' [英] gitlab-ci.yml: 'script: -pytest cannot find any tests to check'

查看:113
本文介绍了gitlab-ci.yml:'脚本:-pytest无法找到要检查的任何测试'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法实现在.gitlab-ci.yml中运行pytest的玩具示例

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

gitlab_ci 是一个包含单个文件 test_hello.py

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终端输出

$ 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

我不确定为什么测试没有运行... pytest似乎无法识别 test_hello.py

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

解决方案

将python文件放入新添加的 tests 文件夹中:

Put the python file inside the newly creared tests folder:

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

以以下方式修改 gitlab-ci.yml :

# .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


test_hello.py 与以前一样.

推荐答案

博客文章提到了类似的管道,但是:

This blog post mentions a similar pipeline, but:

但是,由于pytest无法找到要测试的"bild"模块(即源代码),因此无法正常工作.
此处遇到的问题是 test _ *.py 文件找不到'bild'模块,因为未在系统路径中指定项目的顶级目录:

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

OP库诺夫确认

现在可以使用!我将单个文件放在新创建的名为" test "

这篇关于gitlab-ci.yml:'脚本:-pytest无法找到要检查的任何测试'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆