NỘI DUNG BÀI HỌC
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