|
1 | 1 | # Unit Test |
2 | 2 |
|
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: |
4 | 8 |
|
5 | 9 | === "Sync" |
6 | 10 |
|
@@ -106,3 +110,66 @@ If you are using githubkit in your business logic, you may want to mock the gith |
106 | 110 | 1. Example function you want to test, which calls the GitHub API. |
107 | 111 | 2. other request parameters including headers, json, etc. |
108 | 112 | 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 | + ``` |
0 commit comments