0
requests을 API 호출로 사용하여 라이브러리를 단위 테스트 할 때 pytest와 함께 requests-mock을 사용하고 있습니다.requests_mock을 사용하여 HTTP 요청 본문을 작성하십시오.
서버 응답을 조롱하는 것 외에도 내 라이브러리가 HTTP 본문에서 예상되는 페이로드를 전송하는지 확인해야합니다.
내 테스트에 additional_matcher 콜백을 사용하여 간접적이기는하지만,이 작업을 수행 할 수 있었다 :
def mylibrary_foo():
"""Library method that is under test."""
r = requests.post('http://example.com/foo', data='hellxo')
return r.text
@requests_mock.Mocker()
def test_foo(m):
def matcher(request):
assert request.body == 'hello'
return True
m.post('http://example.com/foo', text='bar', additional_matcher=matcher)
result = mylibrary_foo()
assert result == 'bar'
을하지만 그것으로, 요청의 형식을 확인하기 위해 additional_matcher
콜백을 사용하는 것이 조금 재미 느낌 이 특정 요청 호출이 전혀 조롱되어야하는지 여부를 결정하기위한 것입니다.
def test_foo():
# setup api_mock here...
mylibrary_foo()
api_mock.assert_called_with(data='hello')
일반적으로 HTTP 요청 검증을 지원하기 위해 요청-모의 사용 패턴이 있습니까 : 나는 요청-모의를 사용하지 않은, 내가 더 같은 일을하고있을 것 같다?
는 는