关键 - 在 Travis CI 中 运行 时出错 |测试
Key - Error when running in Travis CI| Pytest
虽然 运行 在 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,请找到 link 到 Travis CI 的日志:
https://travis-ci.org/pythonprogsnscripts/geekttrustproblems/builds/570241873
如果老手能提点意见就好了
os.listdir
不保证确定的文件排序;它会在 OS 和文件系统组合之间变化。来自 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'))
虽然 运行 在 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,请找到 link 到 Travis CI 的日志:
https://travis-ci.org/pythonprogsnscripts/geekttrustproblems/builds/570241873
如果老手能提点意见就好了
os.listdir
不保证确定的文件排序;它会在 OS 和文件系统组合之间变化。来自 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'))