TDD测试显示,即使响应正确错误(TDD test shows error even though

2019-10-30 05:06发布

我下面一个约API教程这里和我下面确切的代码,我也适应代码为Etsy的应用程序,这是我第二次测试代码,将教程代码是相同的教程,和作品。 下面的代码已被改编与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文件的测试结果已经出来好与结果网址完全一样我想,这里有来自终端错误的详细信息

______________________________ test_listings_info _________________________listing_keys = [ 'listing_id'] @ vcr.use_cassette( '测试/ vcr_cassettes /挂牌info.yml')高清test_listings_info(listing_keys): “” “测试API调用来获取清单” “” listings_instance =人数(648597757)响应= listings_instance.info()断言isinstance(响应,字典)断言响应[ 'ID'] == 648597757, “ID应该在响应” E KeyError异常: 'ID' 测试/ test_etsywrapper.py:18:KeyError异常

这里是我所期望的网址

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

你可以看到预计在测试中的“ID”号码是存在的,但测试无法看到它。 这是至关重要的测试是正确的,任何人可以看到我的代码中的错误? 在beggining我说,我都跟着教程,这是我,该代码是这里没有显示,但它是相同的教程和它的作品通过了所有测试。

Answer 1:

测试失败,因为没有id的响应JSON的关键,而不是在URL中。 你应该总是使用dict.get(key)从字典检索值,如使用dict[key]抛出KeyError异常,如果key是没有字典。



文章来源: TDD test shows error even though response is correct