См. Этот пример.
mook_2.py
from datetime import datetime
import requests
from constants import STATUS_ENDPOINT
class BuildStatus:
"""The CI status of a pull request."""
@staticmethod
def build_date() -> str:
return datetime.utcnow().isoformat()
@classmethod
def notify(cls, merge_request_id, status):
build_status = {
"id": merge_request_id,
"status": status,
"built_at": cls.build_date(),
}
response = requests.post(STATUS_ENDPOINT, json=build_status)
response.raise_for_status()
return response
test_mook.py
from unittest import mock
from constants import STATUS_ENDPOINT
from mock_2 import BuildStatus
@mock.patch("mock_2.requests") # mock_2.requests를 호출하면 mock_requests 모의 객체가 대신할것
def test_build_notification_sent(mock_requests):
build_date = "2018-01-01T00:00:01"
with mock.patch("mock_2.BuildStatus.build_date", return_value=build_date): # build_date 호출시 설정한 날짜 반환하도록함,
BuildStatus.notify(123, "OK")
expected_payload = {"id": 123, "status": "OK", "built_at": build_date}
mock_requests.post.assert_called_with(
STATUS_ENDPOINT, json=expected_payload
)
constants.py
"""Definitions"""
STATUS_ENDPOINT = "http://localhost:8080/mrstatus"