Skip to content

Commit 0b26f39

Browse files
committed
📝 add unit test using mock transport
1 parent 2bac460 commit 0b26f39

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

docs/usage/unit-test.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Unit Test
22

3-
If you are using githubkit in your business logic, you may want to mock the github API in your unit tests. You can custom the response by mocking the `request`/`arequest` method of the `GitHub` class. Here is an example of how to mock githubkit's API calls:
3+
If you are using githubkit in your business logic, you may want to mock the github API in your unit tests. There are two ways to reach this.
4+
5+
## Mocking the API Calls
6+
7+
If you can't provide a githubkit test client to your business logic, you can mock the `request`/`arequest` method of the `GitHub` class to custom the response. Here is an example of how to mock githubkit's API calls:
48

59
=== "Sync"
610

@@ -106,3 +110,66 @@ If you are using githubkit in your business logic, you may want to mock the gith
106110
1. Example function you want to test, which calls the GitHub API.
107111
2. other request parameters including headers, json, etc.
108112
3. When the request is made, return a fake response
113+
114+
## Using a Test Transport
115+
116+
You can also create a test client with mock transport and provide it to your business logic. Here is an example:
117+
118+
=== "Sync"
119+
120+
```python
121+
import json
122+
from pathlib import Path
123+
124+
import httpx
125+
import pytest
126+
127+
from githubkit import GitHub
128+
from githubkit.versions.latest.models import FullRepository
129+
130+
FAKE_RESPONSE = json.loads(Path("fake_response.json").read_text())
131+
132+
def target_sync_func(github: GitHub):
133+
resp = github.rest.repos.get("owner", "repo")
134+
return resp.parsed_data
135+
136+
def mock_transport_handler(request: httpx.Request) -> httpx.Response:
137+
if request.method == "GET" and request.url.path == "/repos/owner/repo":
138+
return httpx.Response(status_code=200, json=FAKE_RESPONSE)
139+
raise RuntimeError(f"Unexpected request: {request.method} {request.url.path}")
140+
141+
def test_sync_mock():
142+
g = GitHub("xxxxx", transport=httpx.MockTransport(mock_transport_handler))
143+
repo = target_sync_func(g)
144+
assert isinstance(repo, FullRepository)
145+
```
146+
147+
=== "Async"
148+
149+
```python
150+
import json
151+
from pathlib import Path
152+
153+
import httpx
154+
import pytest
155+
156+
from githubkit import GitHub
157+
from githubkit.versions.latest.models import FullRepository
158+
159+
FAKE_RESPONSE = json.loads(Path("fake_response.json").read_text())
160+
161+
async def target_async_func(github: GitHub):
162+
resp = await github.rest.repos.async_get("owner", "repo")
163+
return resp.parsed_data
164+
165+
def mock_transport_handler(request: httpx.Request) -> httpx.Response:
166+
if request.method == "GET" and request.url.path == "/repos/owner/repo":
167+
return httpx.Response(status_code=200, json=FAKE_RESPONSE)
168+
raise RuntimeError(f"Unexpected request: {request.method} {request.url.path}")
169+
170+
@pytest.mark.anyio
171+
async def test_async_mock():
172+
g = GitHub("xxxxx", async_transport=httpx.MockTransport(mock_transport_handler))
173+
repo = await target_async_func(g)
174+
assert isinstance(repo, FullRepository)
175+
```

tests/test_unit_test/test_mock_transport.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import json
22
from pathlib import Path
3-
from typing import TypeVar
43

54
import httpx
65
import pytest
76

87
from githubkit import GitHub
98
from githubkit.versions.latest.models import FullRepository
109

11-
T = TypeVar("T")
12-
13-
1410
FAKE_RESPONSE = json.loads((Path(__file__).parent / "fake_response.json").read_text())
1511

1612

0 commit comments

Comments
 (0)