iT邦幫忙

2026 iThome 鐵人賽

DAY 4
0

In Day4, I prepared the development environment and created the basic project structure.

Instead of building a complete test framework immediately, I would started with a small business flow:

Check API → Create Booking → Retrieve Booking

The goal is to confirm that pytest and requests can successfully interact with the Restful Booker API.

Check the API

Before testing any business operation, I used the /ping endpoint to check whether the API was available.

This became the first smoke test:

  • Smoke — API is available and responding.

The test sends a request and checks the returned HTTP status code. It is simple, but it helps separate environment problems from actual test failures.

Create the First Booking

After confirming that the API was available, I created the first booking through:

POST /booking

The request contained basic booking information, including:

  • Customer name
  • Total price
  • Deposit status
  • Booking dates
  • Additional needs

The test then checked whether the request succeeded, a new bookingid was generated, and the returned data matched the original request.

The basic process was:Request → Response → Assertion

This became the first positive test:

  • Positive — Valid requests behave as expected.

Retrieve the Booking

Receiving a successful response from POST /booking does not necessarily prove that the data was stored correctly.

Therefore, I used the generated bookingid to retrieve the same booking:

POST /booking

bookingid

GET /booking/{bookingid}

The retrieved booking data should match the data used when creating it.

This introduced another important type of validation:

  • State Validation — Data actually changes after operations.

The test is no longer checking only a single response. It now verifies a small workflow and confirms the state of the system after an operation.

Run the Tests

Finally, I ran the tests with pytest:

pytest

All three tests passed:

3 passed

At this point, the project can:

  • Check whether the API is available
  • Create test data
  • Capture a generated booking ID
  • Retrieve the created booking
  • Validate the stored data

Now the tests work.


上一篇
Python + pytest Project Setup
系列文
不只會測試:30 天用 Side Project 打造 T 型技能樹5
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言