gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:45:51 25 4
gpt4 key购买 nike

我在实现一个在 .gitlab-ci.yml 中运行 pytest 的玩具示例时遇到问题

gitlab_ci 是一个包含单个文件 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


解决方案

将 python 文件放入新创建的 tests 文件夹中:

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 将与以前保持相同。

最佳答案

一些人认为操纵 PYTHONPATH 变量是一种不好的做法(例如,参见 this answer on stackoverflow 或这个 Level Up Coding post )。虽然这在 GitLab CI 作业范围内可能不是一个大问题,但这里有一个解决方案,无需摆弄 PYTHONPATH,而且也更干净。它基于Alberto Mardegan's comment on the blog post VonC's answer中提到:

pytest:
stage: Test
script:
- pwd
- ls -l
- python -m pytest

为什么这有效?来自 pytest docs :

You can invoke testing through the Python interpreter from the commandline:

python -m pytest [...] 

This is almost equivalent to invoking thecommand line script pytest [...] directly, except that calling viapython will also add the current directory to sys.path.

关于pytest - gitlab-ci.yml : 'script: -pytest cannot find any tests to check' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195130/

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