python - 关键 - 在 Travis CI 中运行时出错| pytest

标签 python pytest travis-ci pytest-cov

在 Travis CI 上运行 Pytest 时,我收到 Key -Error。请在下面找到我的程序:

import sys
import os
sys.path.append(os.path.dirname(__file__)+"/../")
from src.read_files import VEHICLE_DATA
from src.main import create_parser

def getvehicles(climate):
    '''
       :param climate: type of climate
       :return: Based on climate, return available vehicles
    '''

    bike = VEHICLE_DATA['bike']
    tuktuk = VEHICLE_DATA['tuktuk']
    car = VEHICLE_DATA['car']

    if climate == "Sunny":
        vehicle = [[bike, tuktuk, car], -0.1]
    elif climate == "Rainy":
        vehicle = [[car, tuktuk], 0.2]
    else:
        vehicle = [[car, bike], 0.0]
    return vehicle

对应的pytest如下:

import sys
import os
sys.path.append(os.path.dirname(__file__)+"/../")
from src import traffic_problem_1 as tp
import pytest

@pytest.mark.parametrize('climate, speed',          \
                        [                           \
                            ('Sunny', -0.1),        \
                            ('Windy', 0.0),         \
                            ('Rainy', 0.2)
                        ])
def test_when_climate_sunny_return_all_vechicles(climate, speed):
    crater_speed = tp.getvehicles(climate)
    assert crater_speed[1] == speed

上述测试在我的本地计算机上成功运行。但 Travis CI 不是,请找到 Travis CI 日志的链接:

https://travis-ci.org/pythonprogsnscripts/geekttrustproblems/builds/570241873

如果退伍军人能提出一些想法那就太好了

最佳答案

os.listdir 不保证确定的文件排序;它会因操作系统和文件系统组合而异。来自 the docs :

os.listdir(path='.')

Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order [...]

就您而言,这意味着 JSON_FILES[1] 在某些系统上将是 vehicle_data.json ,而在其他系统上将是 orbit_data.json ,导致测试失败。解决方案是自己强制执行命令,例如通过排序:

JSON_FILES = sorted(os.listdir('inputdata'))

关于python - 关键 - 在 Travis CI 中运行时出错| pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57443795/

相关文章:

docker - 更改 docker 容器内的主机名时出错

python - 如何将填充的 0 行添加到 Pandas 数据框?

django - 使用迁移运行的 pytest-django 忽略数据库触发器

installation - 如何共享在 pytest/unittest 测试中具有作为 session 范围的 fixture 中创建的 session 对象

php - Travis CI 只执行单个测试文件 phpunit

maven - Travis CI 无法安装 openjdk11

python - 如何在一个 .travis.yml 中测试 Python 和 C++,而不需要多次运行 C++?

python - Tensorflow - 使用自定义比较器对张量进行排序

python - Spark 与 pyspark - 客户端的 python 版本会影响驱动程序 python 版本吗?

python - 如何在 pytest 中将单元测试和集成测试分开