Tags:

Vô hiệu hoá unit test trong Junit 5

Junit 5 @Disable là một annotation mới thay thế @Ignore annotation từ phiên bản cũ JUnit4 cho phép vô hiệu hoá một test method hay một test class. Điều này sẽ hữu dụng khi các unit test liên quan đến mã code mà bạn đang phải làm lại, hoặc sữa đổi, thay đổi data model etc chờ đến khi code xong rồi mới sữa unit test.

Vô hiệu hoá test method

Để vô hiệu hoá test method đặt @Disable trên method cần vô hiệu hoá.

package disable;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DisableTestMethod {

    @Disabled("Disabled until CustomerService is up!")
    @Test
    void testCustomerServiceGet() {
        assertEquals(2, 1 + 1);
    }

    @Test
    void test3Plus3() {
        assertEquals(6, 3 + 3);
    }


}

Chạy DisableExample trên Intellij chúng ta được kết quả disable test method

Có thể thấy logo màu vàng thì ghi chú của test method được vô hiệu hoá, chúng ta chỉ còn test3Plus3() được thực thi.

Vô hiệu hoá test class

Để vô hiệu hoá test class đặt @Disable trên class cần vô hiệu hoá.

package disable;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled("It Will be open laster")
public class DisableTestClass {

    @ParameterizedTest
    @ValueSource(strings = {"test 1", "test 2"})
    public void testStringIsNotBlank(String input) {
        assertTrue(input != "" && input != null);
    }


    @RepeatedTest(3)
    public void test2plus1() {
        assertTrue(3 ==2 + 1);
    }

}

Kết quả thực thi trên Intellij

disable test class

Lưu ý các bạn phải chạy nguyên package hoặc chạy tất cả các test class trên intellij thì mới có được kết quả trên. Nếu bạn chỉ định DisableTestClass thì nó vẫn chạy bình thường nhé.

Như kết quả trên các bạn thấy @Disable được đặt ở đầu class sẽ tương ứng với việc bạn đặt @Disbale trên tất cả các test method có trong class. Như console bạn thấy có đến 2 dòng console

It Will be open later
It Will be open later

Tương ứng với 2 lần chạy của các test method nhưng bị vô hiệu hoá với @Disable annotation.

Tóm lược

@Disable thường được dùng khi bạn triển khai unit test trước khi implement task, tránh gây phiền phức ở mỗi lần kiểm tra. Sau khi thực hiện task xong hãy nhớ xoá @Disable để các unit test chạy bình thường nha. Lâu lâu mình cũng dùng @Disable để merge code lên gấp cho các anh chị test rồi sữa unit test sau, vì thường trong các dự án lớn muốn merge code vào nhánh Develop thì bắt buộc pull request của phải build thành công và tất cả các unit test phải passed, nên các bạn cũng có thể tận dụng trong các trường hợp này. Mà nhớ phải fix nhé, để @Disable người khác thấy là chửi ai buồn biết liền à!

Nguồn tham khảo

https://mkyong.com/junit5/junit-5-how-to-disable-tests/

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x