NỘI DUNG BÀI HỌC

Thiết lập chạy test cases song song trong REST Assured and TestNG Framework

1. Chuẩn bị test case chạy song song

Ví dụ gồm 3 test cases tương ứng 3 @Test để create book từ web demo, đặt data khác nhau thông qua thư viện Data Faker hỗ trợ random.

package com.anhtester.Bai19_ParallelExecution;

import com.anhtester.listeners.TestListener;
import com.anhtester.model.BookingBody;
import com.anhtester.model.BookingDates;
import com.google.gson.Gson;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import net.datafaker.Faker;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.Locale;

import static io.restassured.RestAssured.given;

@Listeners(TestListener.class)
public class BookingTest_Parallel {
    @Test
    public void testCreateBooking1() {

        String baseUri = "https://restful-booker.herokuapp.com";

        RequestSpecification request = given();
        request.baseUri(baseUri);
        request.header("Accept", "application/json")
                .header("Content-Type", "application/json");

        BookingBody bookingBody = new BookingBody();
        BookingDates bookingDates = new BookingDates();

        Faker faker = new Faker(new Locale("vi"));

        bookingBody.setFirstname("Anh");
        bookingBody.setLastname("Tester " + faker.random().hex(6));
        bookingBody.setTotalprice(120);
        bookingBody.setDepositpaid(false);
        bookingBody.setAdditionalneeds("Automation");
        bookingDates.setCheckin("2024-03-26");
        bookingDates.setCheckout("2024-03-30");

        bookingBody.setBookingdates(bookingDates);

        Gson gson = new Gson();

        request.body(gson.toJson(bookingBody));

        Response response = request.post("/booking");
        response.prettyPrint();
        response.then().statusCode(200);
    }

    @Test
    public void testCreateBooking2() {

        String baseUri = "https://restful-booker.herokuapp.com";

        RequestSpecification request = given();
        request.baseUri(baseUri);
        request.header("Accept", "application/json")
                .header("Content-Type", "application/json");

        BookingBody bookingBody = new BookingBody();
        BookingDates bookingDates = new BookingDates();

        Faker faker = new Faker(new Locale("vi"));

        bookingBody.setFirstname("Anh");
        bookingBody.setLastname("Tester " + faker.random().hex(6));
        bookingBody.setTotalprice(120);
        bookingBody.setDepositpaid(false);
        bookingBody.setAdditionalneeds("Automation");
        bookingDates.setCheckin("2024-03-26");
        bookingDates.setCheckout("2024-03-30");

        bookingBody.setBookingdates(bookingDates);

        Gson gson = new Gson();

        request.body(gson.toJson(bookingBody));

        Response response = request.post("/booking");
        response.prettyPrint();
        response.then().statusCode(200);
    }

    @Test
    public void testCreateBooking3() {

        String baseUri = "https://restful-booker.herokuapp.com";

        RequestSpecification request = given();
        request.baseUri(baseUri);
        request.header("Accept", "application/json")
                .header("Content-Type", "application/json");

        BookingBody bookingBody = new BookingBody();
        BookingDates bookingDates = new BookingDates();

        Faker faker = new Faker(new Locale("vi"));

        bookingBody.setFirstname("Anh");
        bookingBody.setLastname("Tester " + faker.random().hex(6));
        bookingBody.setTotalprice(120);
        bookingBody.setDepositpaid(false);
        bookingBody.setAdditionalneeds("Automation");
        bookingDates.setCheckin("2024-03-26");
        bookingDates.setCheckout("2024-03-30");

        bookingBody.setBookingdates(bookingDates);

        Gson gson = new Gson();

        request.body(gson.toJson(bookingBody));

        Response response = request.post("/booking");
        response.prettyPrint();
        response.then().statusCode(200);
    }
}


2. Khai báo thuộc tính "parallel" trong file Suite XML

Rất đơn giản để thiết lập test chạy song song trong TestNG Framework là chỉ cần sử dụng thuộc tính parallel trong file Suile XML.



Giá trị của thuộc tính parallel gồm có 3 loại giá trị chính là "methods", "tests" và "classes"

  • methods tương ứng chạy song song các @Test bên trong class chỉ định
  • tests tương ứng chạy song song giữa các module <test> trong file XML
  • classes tương ứng chạy song song giữa các class chỉ định, không chạy song song từng @Test bên trong


Còn để giá trị là "none" thì tắt chế độ parallel đi


3. Chạy file Suite XML để thực thi parallel

Ví dụ chạy chế độ parallel "methods" thì nó sẽ chạy song song các @Test bên trong class chỉ định

Teacher

Teacher

Anh Tester

Software Quality Engineer

Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ khóa học lên trang

Bạn có thể đăng khóa học của chính bạn lên trang Anh Tester để kiếm tiền

Danh sách bài học