在 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/