|
| 1 | +--- |
| 2 | +id: samples-microservices |
| 3 | +title: E-commerce Microservices |
| 4 | +sidebar_label: Microservices |
| 5 | +description: The following sample microservices app shows how Keploy helps you test microservices applications. In the sample microservices app, we have 3 microservices:user service, product service, and order service, each with its own MySQL database, plus LocalStack SQS for messaging. |
| 6 | + |
| 7 | +tags: |
| 8 | + - python |
| 9 | + - quickstart |
| 10 | + - samples |
| 11 | + - examples |
| 12 | + - tutorial |
| 13 | + - python-framework |
| 14 | + - microservices |
| 15 | + - mysql |
| 16 | + - flask-framework |
| 17 | +keyword: |
| 18 | + - Flask Framework |
| 19 | + - mysql |
| 20 | + - python microservices |
| 21 | + - Python |
| 22 | + - API Test generator |
| 23 | + - Auto case generation |
| 24 | +--- |
| 25 | + |
| 26 | +import InstallReminder from '@site/src/components/InstallReminder'; |
| 27 | + |
| 28 | +## Using Docker Compose 🐳 |
| 29 | + |
| 30 | +### Introduction |
| 31 | + |
| 32 | +This guide will walk you through testing an E-commerce microservices application with Keploy. The app contains three microservices: |
| 33 | + |
| 34 | +- **User Service** |
| 35 | +- **Product Service** |
| 36 | +- **Order Service** |
| 37 | + |
| 38 | +Each service uses its own **MySQL database**, and **LocalStack SQS** is used for messaging. Keploy will help you automatically generate test cases and mocks for these services. |
| 39 | + |
| 40 | +<InstallReminder /> |
| 41 | + |
| 42 | +### Clone the Sample Application |
| 43 | + |
| 44 | +First, clone the repository that contains the sample app: |
| 45 | + |
| 46 | +```bash |
| 47 | +git clone https://github.com/keploy/ecommerce_sample_app.git |
| 48 | +cd ecommerce_sample_app |
| 49 | +``` |
| 50 | + |
| 51 | +_Note: You can view the **architecture diagram** of the application [here](https://keploy-devrel.s3.us-west-2.amazonaws.com/Microservices-architecture.png)_ |
| 52 | + |
| 53 | +#### Start the Microservices |
| 54 | + |
| 55 | +The app is set up with **Docker Compose**, making it easy to start all services together. Let’s begin with the **User Service**. |
| 56 | + |
| 57 | +### Capture Test Cases with Keploy |
| 58 | + |
| 59 | +To start capturing API test cases, use the following command: |
| 60 | + |
| 61 | +```bash |
| 62 | +keploy record -c "docker compose up" --container-name="order_service" --build-delay 40 --path="./order_service" --config-path="./order_service" |
| 63 | +``` |
| 64 | + |
| 65 | +<img src="https://keploy-devrel.s3.us-west-2.amazonaws.com/keploy-record-microservice-updated.png" alt="Sample Keploy Record Microservices" width="100%" style={{ borderRadius: '5px' }} /> |
| 66 | + |
| 67 | +Once the services are up, use the **Postman collection** provided with the app to make some API calls. Keploy will capture these requests and create test cases. |
| 68 | + |
| 69 | +You can see in the logs that Keploy starts recording all the network calls. |
| 70 | + |
| 71 | +<img src="https://keploy-devrel.s3.us-west-2.amazonaws.com/keploy-capture-test-updated.png" alt="Sample Keploy Record microservices" width="100%" style={{ borderRadius: '5px' }} /> |
| 72 | + |
| 73 | +### Stop the Recording |
| 74 | + |
| 75 | +And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the **keploy** directory and you'll discover your handiwork in `tests` directory and `mocks.yml`. |
| 76 | + |
| 77 | +```yaml |
| 78 | +# Generated by Keploy (2.10.9) |
| 79 | +version: api.keploy.io/v1beta1 |
| 80 | +kind: Http |
| 81 | +name: test-1 |
| 82 | +spec: |
| 83 | + metadata: {} |
| 84 | + req: |
| 85 | + method: POST |
| 86 | + proto_major: 1 |
| 87 | + proto_minor: 1 |
| 88 | + url: http://localhost:8080/api/v1/orders |
| 89 | + header: |
| 90 | + Accept: "*/*" |
| 91 | + Accept-Encoding: gzip, deflate, br |
| 92 | + Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiMTNjNWJhZS04OTc5LTExZjAtOGM0Ny1iNmM3ZmQwZmY2MmQiLCJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNzU2OTg0ODQxLCJleHAiOjE3NTk1NzY4NDF9.1OVaOL09j10oB7ahwOKu4mi-ZgnI8ha72MhhaUzKAnE |
| 93 | + Connection: keep-alive |
| 94 | + Content-Length: "141" |
| 95 | + Content-Type: application/json |
| 96 | + Host: localhost:8080 |
| 97 | + Idempotency-Key: 904a1d88-707b-4c14-b7d6-9bd9accea3e7 |
| 98 | + Postman-Token: ede87575-325e-42f6-83e5-5cd55a9dca7e |
| 99 | + User-Agent: PostmanRuntime/7.45.0 |
| 100 | + body: |- |
| 101 | + { |
| 102 | + "userId": "afdc272e-d748-4108-a701-59802b93ea29", |
| 103 | + "items": [ { "productId": "11111111-1111-4111-8111-111111111111", "quantity": 1 } ] |
| 104 | + } |
| 105 | + timestamp: 2025-09-04T11:30:48.75326438Z |
| 106 | + resp: |
| 107 | + status_code: 201 |
| 108 | + header: |
| 109 | + Content-Length: "65" |
| 110 | + Content-Type: application/json |
| 111 | + Date: Thu, 04 Sep 2025 11:30:48 GMT |
| 112 | + Server: Werkzeug/3.1.3 Python/3.11.13 |
| 113 | + body: | |
| 114 | + {"id":"44b0885e-e6e7-4e27-8ffe-97d87791b0b1","status":"PENDING"} |
| 115 | + status_message: Created |
| 116 | + proto_major: 0 |
| 117 | + proto_minor: 0 |
| 118 | + timestamp: 2025-09-04T11:30:50.896837215Z |
| 119 | + objects: [] |
| 120 | + assertions: |
| 121 | + noise: |
| 122 | + header.Date: [] |
| 123 | + created: 1756985450 |
| 124 | +``` |
| 125 | +
|
| 126 | +This is how the `mocks.yml` looks like: |
| 127 | + |
| 128 | +```yaml |
| 129 | +# Generated by Keploy (2.10.9) |
| 130 | +version: api.keploy.io/v1beta1 |
| 131 | +kind: MySQL |
| 132 | +name: mock-0 |
| 133 | +spec: |
| 134 | + metadata: |
| 135 | + connID: "0" |
| 136 | + requestOperation: HandshakeV10 |
| 137 | + responseOperation: OK |
| 138 | + type: config |
| 139 | + requests: |
| 140 | + - header: |
| 141 | + header: |
| 142 | + payload_length: 32 |
| 143 | + sequence_id: 1 |
| 144 | + packet_type: SSLRequest |
| 145 | + message: |
| 146 | + capability_flags: 431991437 |
| 147 | + max_packet_size: 1073741824 |
| 148 | + character_set: 255 |
| 149 | + filler: |
| 150 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 151 | + - header: |
| 152 | + header: |
| 153 | + payload_length: 301 |
| 154 | + sequence_id: 2 |
| 155 | + packet_type: HandshakeResponse41 |
| 156 | + message: |
| 157 | + capability_flags: 431991437 |
| 158 | + max_packet_size: 1073741824 |
| 159 | + character_set: 255 |
| 160 | + filler: |
| 161 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 162 | + username: user |
| 163 | + auth_response: |
| 164 | + [ |
| 165 | + 209, |
| 166 | + 97, |
| 167 | + 143, |
| 168 | + 83, |
| 169 | + 102, |
| 170 | + 55, |
| 171 | + 162, |
| 172 | + 43, |
| 173 | + 183, |
| 174 | + 166, |
| 175 | + 61, |
| 176 | + 254, |
| 177 | + 156, |
| 178 | + 7, |
| 179 | + 42, |
| 180 | + 33, |
| 181 | + 137, |
| 182 | + 77, |
| 183 | + 61, |
| 184 | + 133, |
| 185 | + 242, |
| 186 | + 40, |
| 187 | + 130, |
| 188 | + 251, |
| 189 | + 227, |
| 190 | + 181, |
| 191 | + 38, |
| 192 | + 254, |
| 193 | + 127, |
| 194 | + 21, |
| 195 | + 114, |
| 196 | + 230, |
| 197 | + ] |
| 198 | + database: order_db |
| 199 | + auth_plugin_name: caching_sha2_password |
| 200 | + connection_attributes: |
| 201 | + _client_name: libmysql |
| 202 | + _client_version: 9.0.0 |
| 203 | + _connector_license: GPL-2.0 |
| 204 | + _connector_name: mysql-connector-python |
| 205 | + _connector_version: 9.0.0 |
| 206 | + _os: Linux |
| 207 | + _pid: "1090" |
| 208 | + _platform: aarch64 |
| 209 | + _source_host: 44b86cee3ae3 |
| 210 | + zstdcompressionlevel: 0 |
| 211 | + - header: |
| 212 | + header: |
| 213 | + payload_length: 9 |
| 214 | + sequence_id: 4 |
| 215 | + packet_type: plain_password |
| 216 | + message: cGFzc3dvcmQA |
| 217 | + responses: |
| 218 | + - header: |
| 219 | + header: |
| 220 | + payload_length: 74 |
| 221 | + sequence_id: 0 |
| 222 | + packet_type: HandshakeV10 |
| 223 | + message: |
| 224 | + protocol_version: 10 |
| 225 | + server_version: 8.0.43 |
| 226 | + connection_id: 9 |
| 227 | + auth_plugin_data: |
| 228 | + [ |
| 229 | + 116, |
| 230 | + 65, |
| 231 | + 114, |
| 232 | + 34, |
| 233 | + 83, |
| 234 | + 28, |
| 235 | + 115, |
| 236 | + 61, |
| 237 | + 126, |
| 238 | + 49, |
| 239 | + 53, |
| 240 | + 28, |
| 241 | + 111, |
| 242 | + 34, |
| 243 | + 76, |
| 244 | + 37, |
| 245 | + 91, |
| 246 | + 73, |
| 247 | + 114, |
| 248 | + 88, |
| 249 | + 0, |
| 250 | + ] |
| 251 | + filler: 0 |
| 252 | + capability_flags: 3758096383 |
| 253 | + character_set: 255 |
| 254 | + status_flags: 2 |
| 255 | + auth_plugin_name: caching_sha2_password |
| 256 | + - header: |
| 257 | + header: |
| 258 | + payload_length: 2 |
| 259 | + sequence_id: 3 |
| 260 | + packet_type: AuthMoreData |
| 261 | + message: |
| 262 | + status_tag: 1 |
| 263 | + data: PerformFullAuthentication |
| 264 | + - header: |
| 265 | + header: |
| 266 | + payload_length: 20 |
| 267 | + sequence_id: 5 |
| 268 | + packet_type: OK |
| 269 | + message: |
| 270 | + header: 0 |
| 271 | + affected_rows: 0 |
| 272 | + last_insert_id: 0 |
| 273 | + status_flags: 16386 |
| 274 | + warnings: 0 |
| 275 | + info: "\0\v\x01\t\border_db" |
| 276 | + created: 1756985266 |
| 277 | + reqtimestampmock: 2025-09-04T11:27:46.15043888Z |
| 278 | + restimestampmock: 2025-09-04T11:27:46.162389255Z |
| 279 | +``` |
| 280 | + |
| 281 | +## Check Test Coverage |
| 282 | + |
| 283 | +Keploy also helps you track **test coverage** for your app. |
| 284 | + |
| 285 | +The overall coverage report by files: |
| 286 | + |
| 287 | +<img src="https://keploy-devrel.s3.us-west-2.amazonaws.com/Microservices-test-report.png" alt="Sample Keploy test coverage" width="100%" style={{ borderRadius: '5px' }} /> |
| 288 | + |
| 289 | +The overall coverage report by functions: |
| 290 | + |
| 291 | +<img src="https://keploy-devrel.s3.us-west-2.amazonaws.com/Microservices-python-coverage-report.png" alt="Sample Keploy test coverage" width="100%" style={{ borderRadius: '5px' }} /> |
| 292 | + |
| 293 | +### Run the Tests |
| 294 | + |
| 295 | +Now, let’s run the tests that were automatically generated by Keploy. Use this command: |
| 296 | + |
| 297 | +```bash |
| 298 | +keploy test -c "docker compose up" --containerName="order_service" --delay 30 --path="./order_service" --config-path="./order_service" |
| 299 | +``` |
| 300 | + |
| 301 | +<img src="https://keploy-devrel.s3.us-west-2.amazonaws.com/keploy-test-replay-microservice.png" alt="Sample Keploy Record microservices" width="100%" style={{ borderRadius: '5px' }} /> |
| 302 | + |
| 303 | +The `--delay` flag gives the app a short pause (in seconds) before running the tests. After the tests finish, you can inspect the results and tweak the test data in the `mocks.yml` or `test-x.yml` files. |
| 304 | + |
| 305 | +_Note: We have just tested only one microservice (Order Service). You can use the same command to test other microservices by changing the name and config path._ |
| 306 | + |
| 307 | +## Conclusion 🎉 |
| 308 | + |
| 309 | +Well done! You’ve seen how Keploy helps test your microservices without writing any code. You've generated test cases, run tests, and checked coverage—all with just a few commands. Keep experimenting and enhancing your tests to ensure your app’s reliability. |
0 commit comments