Skip to main content
 首页 » 编程设计

python之travis ci 在使用 pytest 时没有收集我的测试

2024年10月01日14java哥

我已经用谷歌搜索这个问题一个多小时了,完全不知道该怎么做——我正在尝试在我的公共(public)仓库上设置 travis ci,出于某种原因,每次我提交和构建时,我总是得到这个:

============================= test session starts ============================== 
platform linux2 -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 
rootdir: /home/travis/build/epitone/digitron, inifile: 
plugins: cov-2.5.1 
collected 0 items                                                                
========================= no tests ran in 0.01 seconds ========================= 
The command "pytest" exited with 5. 
Done. Your build exited with 1. 

出于某种原因,即使在我的本地机器上,pytest 运行得很好,甚至通过了我设置的(单个)测试,我的测试也没有被收集。有谁知道这里到底发生了什么?

我的目录设置是这样的:

digitron/ 
├── bot.py 
├── lib 
│   ├── _config.py 
│   ├── config.py 
│   ├── __init__.py 
│   └── utils.py 
├── README.md 
├── requirements.txt 
└── tests 
    └── auth_test.py 

我的 .travis.yml 文件:

language: python 
python: 
  - "2.7" 
  - "3.2" 
  - "3.3" 
  - "3.4" 
  - "3.5" 
  - "3.6" 
  - "nightly" # currently points to 3.7-dev 
before_install: 
    - pip install pytest pytest-cov 
# command to install dependencies 
install: "pip install -r requirements.txt" 
# command to run tests 
script: pytest 

我的 requirements.txt 文件包含:

pytest>=3.2.1 
py>=1.4.31 
pluggy>=0.4.0 

我的 auth_test.py 文件很简单:

""" 
Testing for Digitron 
""" 
#  Necessary to find parent directory 
import os, sys 
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 
import bot 
import pytest 
 
    def test_connect(): 
        """Test connect() method in bot.py 
 
        Returns True or false 
        """ 
        assert(bot.connect("irc.chat.twitch.tv", 6667, "oauth:key", "username", "#channel")) == True 

我基本上是在绞尽脑汁想弄清楚我做错了什么 - 谷歌搜索错误代码 5 什么也得不到,我什至跟随别人的设置但仍然一无所获 - 有吗我缺少什么?

编辑: 尝试在 pytest 命令中显式调用 tests/目录 - 在本地工作,在 travis.ci 上再次失败

请您参考如下方法:

  1. 虽然我不知道这是否是您问题的原因,但至少强制sys.path 的方法不是很正统。 pytest 自动执行此操作:https://docs.pytest.org/en/latest/goodpractices.html#tests-as-part-of-application-code所以可能存在导入问题。

  2. 错误编号 5 是由 pytest 未找到任何测试引起的,请参阅 documentation

  3. 可能的分辨率测试:

    1. 将您的测试文件重命名为 test_auth.py
    2. 将诊断信息添加到您的脚本:pwd、ls 等。尝试简单地执行文件:python tests/auth_test.py。

在评论中设置格式有点长而且烦人,因此是答案。