简体   繁体   中英

TDD test shows error even though response is correct

I am following a tutorial about api here and I am following the exact code and I am also adapting the code for an Etsy app, here is my code for the second test, the tutorial code is identical to the tutorial, and works. The following code has been adapted to work with the Etsy APi.

#etsywrapper/__core.py   
from . import session

class Listings(object):

    def __init__(self, id):
        self.id = id

    def info(self):
        path = 'https://openapi.etsy.com/v2/listings/{}/inventory'.format(self.id)
        response = session.get(path)
        return response.json()

    @staticmethod
    def active():
        path = 'https://openapi.etsy.com/v2/shops/:shop_id/listings/active'
        response = session.get(path)
        return response.json()

and

#etsywrapper/__init__.py    
import os
import requests

ETSY_API_KEY = os.environ.get('ETSY_API_KEY', None)

class APIKeyMissingError(Exception):
    pass

if ETSY_API_KEY is None:
    raise APIKeyMissingError(
        "All methods require an API key. See "
        "https://developers.themoviedb.org/3/getting-started/introduction "
        "for how to retrieve an authentication token from "
        "The Movie Database"
    )
session = requests.Session()
session.params = {}
session.params['api_key'] = ETSY_API_KEY

from .__core import Listings

and

#tests/test_etsywrapper.py    
from pytest import fixture
from etsywrapper import Listings
import vcr

@fixture
def listing_keys():
    # Responsible only for returning the test data
    return ['listing_id']

@vcr.use_cassette('tests/vcr_cassettes/listing-info.yml')
def test_listings_info(listing_keys):
    """test api call to get listings"""

    listings_instance = Listings(648597757)
    response = listings_instance.info()

    assert isinstance(response, dict)
    assert response['id'] == 648597757, "id should be in response"
    assert set(listing_keys).issubset(response.keys()), "All keys should be in the response"

@vcr.use_cassette('tests/vcr_cassettes/listings_active.yml')
def test_listings_active():
    """tests shop active listings"""

    response = Listings.active()

    assert isinstance(response, dict)
    assert isinstance(response['results'], list)
    assert isinstance(response['results'][0], dict)
    assert set(listing_keys()).issubset(response['results'][0].keys())

I then run the tests with "ETSY_API_KEY='my_api_code_here' py.test" The results of the test show that I have an error in the first test, but when I look at the results in the vcr file the test Has come ok with the URL exactly as I want, here are the error details from terminal

______________________________ test_listings_info _________________________listing_keys = ['listing_id'] @vcr.use_cassette('tests/vcr_cassettes/listing-info.yml') def test_listings_info(listing_keys): """test api call to get listings""" listings_instance = Listings(648597757) response = listings_instance.info() assert isinstance(response, dict) assert response['id'] == 648597757, "id should be in response" E KeyError: 'id' tests/test_etsywrapper.py:18: KeyError

and here is the url I expected

https://openapi.etsy.com/v2/listings/648597757/inventory?api_key= "my_api_key"

as you can see the 'id' number expected in the test is there, but the test cannot see it. it is vital the test is correct, can anybody see the error in my code? At the beggining I said that i have followed the tutorial, which I have, That code is not shown here but it is identical to the tutorial and it works all tests pass.

The test is failing because there's no id key in the response JSON, not in the URL. You should always use dict.get(key) to retrieve values from a dict, as using dict[key] throws KeyError if key isn't in dict.

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