简体   繁体   中英

Key - Error when running in Travis CI| Pytest

while running Pytest on Travis CI ,i am getting Key -Error. Please find my program below:

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

The corresponding pytest is as follows:

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

The above test successfully runs on my local machine. But not i Travis CI, Please find the link to Travis CI logs:

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

It would be great if veterans can suggest some ideas

os.listdir doesn't guarantee a deterministic file ordering; it will vary between OS and file system combinations. From 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 [...]

In your case, it means that JSON_FILES[1] will be vehicle_data.json on some systems and orbit_data.json on others, causing test failures. The solution is to enforce an ordering yourself, eg via sorting:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM