python 响应 - 并非所有请求都已执行
python responses - Not all requests have been executed
我正在尝试测试用例来模拟 api 调用并使用 python 响应来模拟 api 调用。
下面是我的模拟,
with responses.RequestsMock() as rsps:
url_re = re.compile(r'.*udemy.com/api-2.0/courses.*')
url_re = re.compile(r'https://www.udemy.com/api-2.0/courses')
rsps.add(
responses.GET, url_re,
body=mocked_good_json,
status=200,
content_type='application/json',
match_querystring=True
)
courses = self.app.courses.get_all(page=1, page_size=2)
for course in courses:
self.assertTrue(isinstance(course, Course))
self.assertTrue(hasattr(course, 'id'))
self.assertTrue(hasattr(course, 'title'))
self.assertIsNotNone(course.id)
当我执行这个模拟时,我得到这个错误 -
AssertionError: Not all requests have been executed [(u'GET', 'https://www.udemy.com/api-2.0/courses/')]
当我删除模拟并直接调用 api 时,它工作正常。
关于为什么我的模拟失败的任何输入?
错误消息 -
======================================================================
FAIL: test_get_all_courses (tests.test_courses.TestApiCourses)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/rem/git/udemy/tests/test_courses.py", line 136, in test_get_all_courses
courses = self.app.courses.get_all(page=1, page_size=2)
File "/Users/rem/.virtualenvs/udemyapp/lib/python2.7/site-packages/responses.py", line 536, in __exit__
self.stop(allow_assert=success)
File "/Users/rem/.virtualenvs/udemyapp/lib/python2.7/site-packages/responses.py", line 620, in stop
[(match.method, match.url) for match in not_called]
AssertionError: Not all requests have been executed [(u'GET', 'https://www.udemy.com/api-2.0/courses/')]
您正在模拟该请求,但该请求并未在此测试中调用。您正在调用 courses = self.app.courses.get_all (page = 1, page_size = 2)
,我怀疑此方法 courses.get_all
正在调用请求库。
根据docs,为mock添加response后,预计会调用request。而且你不是在之后调用请求,你正在调用 get_all
,这个方法正在调用,requests.
所以,你必须移动这个测试,并调整它,get_all
方法,或者从 class 使用它的地方模拟请求,看看你的代码,我想是在Course.get_all
.
我正在尝试测试用例来模拟 api 调用并使用 python 响应来模拟 api 调用。
下面是我的模拟,
with responses.RequestsMock() as rsps:
url_re = re.compile(r'.*udemy.com/api-2.0/courses.*')
url_re = re.compile(r'https://www.udemy.com/api-2.0/courses')
rsps.add(
responses.GET, url_re,
body=mocked_good_json,
status=200,
content_type='application/json',
match_querystring=True
)
courses = self.app.courses.get_all(page=1, page_size=2)
for course in courses:
self.assertTrue(isinstance(course, Course))
self.assertTrue(hasattr(course, 'id'))
self.assertTrue(hasattr(course, 'title'))
self.assertIsNotNone(course.id)
当我执行这个模拟时,我得到这个错误 -
AssertionError: Not all requests have been executed [(u'GET', 'https://www.udemy.com/api-2.0/courses/')]
当我删除模拟并直接调用 api 时,它工作正常。
关于为什么我的模拟失败的任何输入?
错误消息 -
======================================================================
FAIL: test_get_all_courses (tests.test_courses.TestApiCourses)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/rem/git/udemy/tests/test_courses.py", line 136, in test_get_all_courses
courses = self.app.courses.get_all(page=1, page_size=2)
File "/Users/rem/.virtualenvs/udemyapp/lib/python2.7/site-packages/responses.py", line 536, in __exit__
self.stop(allow_assert=success)
File "/Users/rem/.virtualenvs/udemyapp/lib/python2.7/site-packages/responses.py", line 620, in stop
[(match.method, match.url) for match in not_called]
AssertionError: Not all requests have been executed [(u'GET', 'https://www.udemy.com/api-2.0/courses/')]
您正在模拟该请求,但该请求并未在此测试中调用。您正在调用 courses = self.app.courses.get_all (page = 1, page_size = 2)
,我怀疑此方法 courses.get_all
正在调用请求库。
根据docs,为mock添加response后,预计会调用request。而且你不是在之后调用请求,你正在调用 get_all
,这个方法正在调用,requests.
所以,你必须移动这个测试,并调整它,get_all
方法,或者从 class 使用它的地方模拟请求,看看你的代码,我想是在Course.get_all
.