🏗️ Automation Testing · Chương 13 · Mục 13.5
Gộp AUTH-02 → AUTH-05 bằng parametrize
4 hàm ở Chương 11 giống hệt nhau về cấu trúc, chỉ khác dữ liệu đầu vào — gộp thành 1 test chạy 4 lần với 4 bộ dữ liệu khác nhau:
Sửa tests/test_03_auth.py, thay 4 hàm test_auth_02...05 bằng:
FILE tests/test_03_auth.py
import pytest@pytest.mark.parametrize("ten_case, username, password",[("AUTH-02 sai password", "admin", "sai-mat-khau"),("AUTH-03 sai username", "khong-ton-tai", "password123"),("AUTH-04 thieu password", "admin", None),("AUTH-05 thieu username", None, "password123"),],)def test_auth_dang_nhap_that_bai(api_context, ten_case, username, password):"""AUTH-02..05: moi truong hop sai/thieu deu cung 1 khuon HTTP 200 + Bad credentials -gop thanh 1 test tham so hoa thay vi lap 4 ham gan giong het nhau."""du_lieu = {}if username is not None:du_lieu["username"] = usernameif password is not None:du_lieu["password"] = passwordresponse = api_context.post("/auth", data=du_lieu)assert response.status == 200, ten_case # moi truong hop sai/thieu van tra 200body = response.json()assert "token" not in body, ten_case # khong duoc cap tokenassert body.get("reason") == "Bad credentials", ten_case # body phai bao dung ly do
Giải thích từng dòng lệnh:
| Dòng | Giải thích |
|---|---|
| @pytest.mark.parametrize("ten_case, username, password", [...]) | Danh sách tham số — mỗi dòng trong list là 1 lần chạy test, với 3 giá trị gán vào đúng 3 tên đã khai báo |
| def test_auth_dang_nhap_that_bai(api_context, ten_case, username, password): | 3 tham số cuối khớp tên với parametrize ở trên — pytest tự truyền từng bộ giá trị vào |
| if username is not None: | Bỏ qua field nếu giá trị là None (mô phỏng "thiếu field" ở AUTH-04/05) |
| assert ..., ten_case | Thêm ten_case sau dấu phẩy — nếu assert sai, pytest in kèm tên case, dễ biết đang FAIL ở bộ dữ liệu nào |
Chạy:
TERMINAL
pytest tests/test_03_auth.py -v
Kết quả (mỗi bộ dữ liệu hiện thành 1 dòng riêng trong tên test):
KẾT QUẢ
tests/test_03_auth.py::test_auth_01_dung_username_va_password PASSEDtests/test_03_auth.py::test_auth_dang_nhap_that_bai[AUTH-02 sai password-admin-sai-mat-khau] PASSEDtests/test_03_auth.py::test_auth_dang_nhap_that_bai[AUTH-03 sai username-khong-ton-tai-password123] PASSEDtests/test_03_auth.py::test_auth_dang_nhap_that_bai[AUTH-04 thieu password-admin-None] PASSEDtests/test_03_auth.py::test_auth_dang_nhap_that_bai[AUTH-05 thieu username-None-password123] PASSEDtests/test_03_auth.py::test_auth_token_dung_lai_duoc_o_cac_test_khac PASSED============================== 6 passed in 3.01s ==============================
Vẫn 6 kết quả như Chương 11 — chỉ khác 4 hàm rời rạc giờ gộp thành 1 test có 4 lần chạy.
Chạy lại toàn bộ dự án để chắc chắn không phá gì sau khi refactor:
TERMINAL
pytest -v
Kết quả: 22 passed.
Đến đây, bạn đã hoàn thành Checkpoint 6 — khớp với commit 07f8aba trên GitHub.
⚡ GHI NHỚ NHANH
- –Class: gói dữ liệu + hành vi liên quan vào 1 chỗ (ở đây:
RestfulBookerClient). - –
@pytest.mark.parametrize: chạy 1 test nhiều lần với nhiều bộ dữ liệu khác nhau, thay vì viết nhiều hàm gần giống nhau.
Chương sau bắt đầu Phần 5 — kết hợp UI và API trong cùng 1 test, trên Restful-Booker Platform.