Skip to content

Commit 4d25835

Browse files
Merge pull request #84 from wiremock/quickstart
Restructure examples + Create the quickstart example + Add examples to the website
2 parents aa33a9c + 0f1822d commit 4d25835

23 files changed

+157
-30
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ jobs:
4242

4343
- name: Integration Tests
4444
run: |
45-
cd example/
45+
cd examples/
4646
docker-compose build overview_srv
4747
docker-compose run overview_srv pytest --tb=short

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ General Rules:
2020
Setup the project using poetry.
2121

2222
`poetry install`
23+
24+
## Contributing examples
25+
26+
Please submit new examples as a pull requests to the [examples directory](./examples/).
27+
You can also also add links to external examples and tutorials to the `README.md`
28+
file in the directory.
29+
30+
When adding new examples,
31+
make sure to update the [documentation site page](./docs/examples.md) too.

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ WireMock can run in unit tests, as a standalone process or a container. Key feat
2323

2424
- [Testcontainers Python](https://github.com/testcontainers/testcontainers-python) module to easily start WireMock server for your tests
2525
- REST API Client for a standalone WireMock Java server
26-
- Supports most of the major [Wiremock](https://wiremock.org/docs) features (more on their way soon)
26+
- Support for most of ajor [WireMock features ](https://wiremock.org/docs) (more on their way soon)
2727

2828
## References
2929

@@ -33,9 +33,9 @@ WireMock can run in unit tests, as a standalone process or a container. Key feat
3333

3434
## Examples
3535

36-
There are several example projects included to demonstrate the different ways that wiremock can be used to mock
36+
There are several [example projects](./examples/) included to demonstrate the different ways that wiremock can be used to mock
3737
services in your tests and systems. The example test modules demonstrate different strategies for testing against
3838
the same "product service" and act as a good demonstration of real world applications to help you get started.
3939

40-
- [Testcontainers Python](example/tests/test_testcontainers.py)
41-
- [Standalone Java Server Version](example/tests/test_java_server.py)
40+
- [Testcontainers Python](examples/intro/tests/test_testcontainers.py)
41+
- [Standalone Java Server Version](examples/intro/tests/test_java_server.py)

docs/examples.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python WireMock examples
2+
3+
- [quickstart](https://github.com/wiremock/python-wiremock/tree/master/examples/quickstart) -
4+
example for the[Quick Start Guide](./quickstart.md)
5+
- [intro](https://github.com/wiremock/python-wiremock/tree/master/examples/intro) -
6+
End-to-End example for both Testcontainers module
7+
and native `pytest` integration.
8+
9+
## External examples
10+
11+
No external examples are referenced at the moment.
12+
Please be welcome to [contribute]([./../CONTRIBUTING.md](https://github.com/wiremock/python-wiremock/blob/master/CONTRIBUTING.md))!
13+
14+
## Contributing examples
15+
16+
More examples are always welcome!
17+
See the [Contributor Guide]([./../CONTRIBUTING.md](https://github.com/wiremock/python-wiremock/blob/master/CONTRIBUTING.md)).

docs/quickstart.md

+58-19
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,98 @@
1-
Quickstart
2-
=====
1+
# Quickstart
32

43
The preferred way of using WireMock to mock your services is by using the provided `WireMockContainer`
54
that uses [testcontainers-python](https://github.com/testcontainers/testcontainers-python)
65
and provisions WireMock as a test container on-demand.
76

8-
### Prerequisites
7+
In this example we will use the [pytest](https://docs.pytest.org/) framework.
8+
9+
## Prerequisites
910

1011
- 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
1217

13-
### Install Python WireMock
18+
To install the most recent version of the Python WireMock library,
19+
use the following command:
1420

1521
```bash
1622
pip install wiremock
1723
```
1824

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.
2036

2137
```python
2238
import pytest
39+
import requests
2340

2441
from wiremock.testing.testcontainer import wiremock_container
42+
from wiremock.constants import Config
43+
from wiremock.client import *
2544

26-
@pytest.fixture(scope="session") # (1)
27-
def wm_server():
45+
@pytest.fixture # (1)
46+
def wiremock_server():
2847
with wiremock_container(secure=False) as wm:
29-
3048
Config.base_url = wm.get_url("__admin") # (2)
31-
3249
Mappings.create_mapping(
3350
Mapping(
3451
request=MappingRequest(method=HttpMethods.GET, url="/hello"),
3552
response=MappingResponse(status=200, body="hello"),
3653
persistent=False,
3754
)
38-
) # (3)
55+
) # (3)
3956
yield wm
57+
```
4058

59+
## Write your first test with WireMock
4160

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:
4363

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"))
4567

46-
assert resp1.status_code == 200
47-
assert resp1.content == b"hello"
68+
assert response.status_code == 200
69+
assert response.content == b"hello"
4870
```
4971

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:
5175

52-
2. Set the wiremock sdk config url to the url exposed by the container
76+
```bash
77+
pytest test.py -v
78+
```
5379

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+
```
5594

56-
4. Use the `wm_server` fixture in your tests and make requests against the mock server.
95+
## Read More
5796

5897
You can read more about Testcontainers support in Python WireMock [here](./testcontainers.md).
5998

docs/testcontainers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ services:
119119
overview_srv:
120120
build:
121121
context: ../
122-
dockerfile: example/Dockerfile
122+
dockerfile: examples/intro/Dockerfile
123123
ports:
124124
- "5001:5001"
125125
environment:

examples/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Python WireMock examples
2+
3+
- [Quick Start Guide](./quickstart/)
4+
- [End-to-End Example](./intro/) for
5+
both Testcontainers module and native
6+
pytest integration.
7+
8+
## External examples
9+
10+
No external examples are referenced at the moment.
11+
Please be welcome to [contribute](./../CONTRIBUTING.md)!
12+
13+
## Contributing
14+
15+
Feel free to contribute the examples!
File renamed without changes.

example/Dockerfile renamed to examples/intro/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ WORKDIR /app/example
1818

1919
# Install dependencies
2020
COPY . /app
21-
COPY ./example/pyproject.toml ./example/poetry.lock /app/example/
21+
COPY ./examples/intro/pyproject.toml ./examples/intro/poetry.lock /app/example/
2222
RUN poetry config virtualenvs.create false \
2323
&& poetry install --no-root --no-interaction --no-ansi
2424

2525

2626
# Copy the rest of the application code
27-
COPY ./example /app/example
27+
COPY ./examples/intro /app/example
2828

2929
# Expose the port that the FastAPI app will listen on
3030
EXPOSE 5001

example/README.md renamed to examples/intro/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python Wiremock Example
1+
# Python Wiremock End-to-End Example
22

33
## Introduction
44

example/docker-compose.yml renamed to examples/intro/docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
overview_srv:
55
build:
66
context: ../
7-
dockerfile: example/Dockerfile
7+
dockerfile: examples/intro/Dockerfile
88
ports:
99
- "5001:5001"
1010
environment:
@@ -20,7 +20,7 @@ services:
2020
products_srv:
2121
build:
2222
context: ../
23-
dockerfile: example/Dockerfile
23+
dockerfile: examples/intro/Dockerfile
2424
ports:
2525
- "5002:5002"
2626
volumes:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/quickstart/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Python WireMock - Quickstart
2+
3+
This example shows using WireMock to mock your services is by using the provided `WireMockContainer`
4+
that uses [testcontainers-python](https://github.com/testcontainers/testcontainers-python)
5+
and provisions WireMock as a test container on-demand.
6+
7+
See the step-by-step guide [here](../../docs/quickstart.md)
8+
9+
## Prerequisites
10+
11+
- Python 3.7 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+
## TL;DR
17+
18+
```bash
19+
pip install wiremock
20+
pytest test.py -v
21+
```

examples/quickstart/test.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
import requests
3+
4+
from wiremock.testing.testcontainer import wiremock_container
5+
from wiremock.constants import Config
6+
from wiremock.client import *
7+
8+
@pytest.fixture # (1)
9+
def wiremock_server():
10+
with wiremock_container(secure=False) as wm:
11+
Config.base_url = wm.get_url("__admin") # (2)
12+
Mappings.create_mapping(
13+
Mapping(
14+
request=MappingRequest(method=HttpMethods.GET, url="/hello"),
15+
response=MappingResponse(status=200, body="hello"),
16+
persistent=False,
17+
)
18+
) # (3)
19+
yield wm
20+
21+
def test_get_hello_world(wiremock_server): # (4)
22+
response = requests.get(wiremock_server.get_url("/hello"))
23+
24+
assert response.status_code == 200
25+
assert response.content == b"hello"

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ nav:
1616
- Testcontainers: testcontainers.md
1717
- Standalone: api-client.md
1818
- Resources:
19+
- Examples: examples.md
1920
- Changelog: changelog.md

0 commit comments

Comments
 (0)