|
1 | | -Quickstart |
2 | | -===== |
| 1 | +# Quickstart |
3 | 2 |
|
4 | 3 | The preferred way of using WireMock to mock your services is by using the provided `WireMockContainer` |
5 | 4 | that uses [testcontainers-python](https://github.com/testcontainers/testcontainers-python) |
6 | 5 | and provisions WireMock as a test container on-demand. |
7 | 6 |
|
8 | | -### Prerequisites |
| 7 | +In this example we will use the [pytest](https://docs.pytest.org/) framework. |
| 8 | + |
| 9 | +## Prerequisites |
9 | 10 |
|
10 | 11 | - Python 3.7 or above |
11 | | -- Pip 20.0.0 or above |
| 12 | +- Pip 20.0.0 or above (use `apt install python3-pip`, for example) |
| 13 | +- Pytest 7.3.0 or above (use `pip install pytest`) |
| 14 | +- Testcontainers 3.5.0 or above (use `pip install testcontainers`) |
| 15 | + |
| 16 | +## Install Python WireMock |
12 | 17 |
|
13 | | -### Install Python WireMock |
| 18 | +To install the most recent version of the Python WireMock library, |
| 19 | +use the following command: |
14 | 20 |
|
15 | 21 | ```bash |
16 | 22 | pip install wiremock |
17 | 23 | ``` |
18 | 24 |
|
19 | | -### Use Python WireMock |
| 25 | +## Create the Test Fixture |
| 26 | + |
| 27 | +As a first step, we will need to provision a test WireMock server to be used in tests: |
| 28 | + |
| 29 | +1. Create an empty `test.py` file |
| 30 | +2. In this file, create a pytest fixture to manage the container life-cycle. |
| 31 | + Use fixture `scope` to control how often the container is created |
| 32 | +3. Set the WireMock SDK config URL to the URL exposed by the container. |
| 33 | + It will route all Admin API requests to |
| 34 | + the mock server. |
| 35 | +4. Create REST API stub mapping for the `/hello` endpoint using the Admin SDK. |
20 | 36 |
|
21 | 37 | ```python |
22 | 38 | import pytest |
| 39 | +import requests |
23 | 40 |
|
24 | 41 | from wiremock.testing.testcontainer import wiremock_container |
| 42 | +from wiremock.constants import Config |
| 43 | +from wiremock.client import * |
25 | 44 |
|
26 | | -@pytest.fixture(scope="session") # (1) |
27 | | -def wm_server(): |
| 45 | +@pytest.fixture # (1) |
| 46 | +def wiremock_server(): |
28 | 47 | with wiremock_container(secure=False) as wm: |
29 | | - |
30 | 48 | Config.base_url = wm.get_url("__admin") # (2) |
31 | | - |
32 | 49 | Mappings.create_mapping( |
33 | 50 | Mapping( |
34 | 51 | request=MappingRequest(method=HttpMethods.GET, url="/hello"), |
35 | 52 | response=MappingResponse(status=200, body="hello"), |
36 | 53 | persistent=False, |
37 | 54 | ) |
38 | | - ) # (3) |
| 55 | + ) # (3) |
39 | 56 | yield wm |
| 57 | +``` |
40 | 58 |
|
| 59 | +## Write your first test with WireMock |
41 | 60 |
|
42 | | -def test_get_hello_world(wm_server): # (4) |
| 61 | +Use the `wiremock_server` fixture in your tests and make requests against the mock server. |
| 62 | +Add the following test to the `test.py` file: |
43 | 63 |
|
44 | | - resp1 = requests.get(wm_server.get_url("/hello"), verify=False) |
| 64 | +```python |
| 65 | +def test_get_hello_world(wiremock_server): # (4) |
| 66 | + response = requests.get(wiremock_server.get_url("/hello")) |
45 | 67 |
|
46 | | - assert resp1.status_code == 200 |
47 | | - assert resp1.content == b"hello" |
| 68 | + assert response.status_code == 200 |
| 69 | + assert response.content == b"hello" |
48 | 70 | ``` |
49 | 71 |
|
50 | | -1. Create a pytest fixture to manage the container life-cycle. use fixture `scope` to control how often the container is created |
| 72 | +## Run the test! |
| 73 | + |
| 74 | +Run the following command: |
51 | 75 |
|
52 | | -2. Set the wiremock sdk config url to the url exposed by the container |
| 76 | +```bash |
| 77 | +pytest test.py -v |
| 78 | +``` |
53 | 79 |
|
54 | | -3. Create response and request mappings using the Admin SDK. |
| 80 | +Sample output: |
| 81 | + |
| 82 | +``` |
| 83 | +$ pytest test.py -v |
| 84 | +================ test session starts ================ |
| 85 | +platform linux -- Python 3.8.10, pytest-7.4.0, pluggy-1.2.0 -- /usr/bin/python3 |
| 86 | +cachedir: .pytest_cache |
| 87 | +rootdir: /c/Users/Oleg/Documents/opensource/wiremock/python-wiremock |
| 88 | +configfile: pyproject.toml |
| 89 | +plugins: anyio-3.7.1 |
| 90 | +collected 1 item |
| 91 | +
|
| 92 | +test.py::test_get_hello_world PASSED [100%] |
| 93 | +``` |
55 | 94 |
|
56 | | -4. Use the `wm_server` fixture in your tests and make requests against the mock server. |
| 95 | +## Read More |
57 | 96 |
|
58 | 97 | You can read more about Testcontainers support in Python WireMock [here](./testcontainers.md). |
59 | 98 |
|
|
0 commit comments