即使响应正确,TDD 测试也会显示错误 [英] TDD test shows error even though response is correct

查看:61
本文介绍了即使响应正确,TDD 测试也会显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关 api 的教程 在这里,我正在遵循确切的代码,我也在调整 Etsy 应用程序的代码,这是我的第二个测试代码,教程代码与教程相同,并且有效.以下代码已经过调整以与 Etsy API 配合使用.

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()

#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

#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())

然后我使用ETSY_API_KEY='my_api_code_here' py.test"运行测试测试结果显示我在第一次测试中出现错误,但是当我查看vcr文件中的结果时,测试结果完全符合我的要求,这里是来自终端的错误详细信息

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):"""测试 api 调用以获取列表"""listings_instance = 列表(648597757)响应 = listings_instance.info()断言 isinstance(响应,字典)断言响应['id'] == 648597757,id 应该在响应中"E KeyError: 'id'测试/test_etsywrapper.py:18: KeyError

______________________________ 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

这是我期望的网址

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

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

如您所见,测试中预期的id"编号在那里,但测试看不到它.测试正确至关重要,有人能看到我代码中的错误吗?在开始时,我说我已经按照教程进行了操作,这里没有显示该代码,但它与教程相同,并且所有测试都通过了.

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.

推荐答案

测试失败,因为响应 JSON 中没有 id 键,而不是 URL.您应该始终使用 dict.get(key) 从字典中检索值,因为如果 key 不是,使用 dict[key] 会抛出 KeyErrort 在字典中.

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.

这篇关于即使响应正确,TDD 测试也会显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆