【发布时间】:2020-08-01 20:51:21
【问题描述】:
我正在尝试从我的 pytest 夹具中的内部函数访问响应。我不确定这是 python 问题还是 pytest 构建方式独有的问题。下面的 sn-p 是一个愚蠢的例子,但它演示了这个问题。我遇到了问题:TypeError: 'function' object is not subscriptable。不知道如何解决这个问题,希望得到一些帮助。
import pytest
import requests
@pytest.fixture
def my_fixture():
def make_request(url):
response = requests.get(url)
return {'code': response.status_code, 'content': response.content}
response = make_request
save_for_after_yield = response['content']
yield response
# print simulates doing something with the content as part of the clean-up
print(save_for_after_yield)
def test_making_requests(my_fixture):
response = my_fixture('http://httpbin.org')
assert 200 == response
【问题讨论】:
标签: python python-3.x unit-testing pytest python-unittest