TheFirstClassTest
package com.anhtester;
import org.testng.annotations.Test;
public class TheFirstClassTest {
@Test
public void testMethod_01(){
System.out.println("testMethod_01");
}
@Test
public void testMethod_02(){
System.out.println("testMethod_02");
}
@Test
public void testMethod_03(){
System.out.println("testMethod_03");
}
}
TheSecondClassTest
package com.anhtester;
import org.testng.annotations.Test;
public class TheSecondClassTest {
@Test
public void testCase_01() {
System.out.println("testCase_01");
}
@Test
public void testCase_02() {
System.out.println("testCase_02");
}
@Test
public void testCase_03() {
System.out.println("testCase_03");
}
}
Các bạn trỏ vào thư mục source code cần chạy và mở cmd lên, nhập lệnh sau:
mvn test
Như mẫu có 2 class gồm 6 test cases sẽ được chạy hết
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS D:\PROJECT_FRAMEWORK\MavenProjectSample> mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.anhtester:MavenProjectSample >------------------
[INFO] Building MavenProjectSample 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ MavenProjectSample ---
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ MavenProjectSample ---
[INFO] skip non existing resourceDirectory D:\PROJECT_FRAMEWORK\MavenProjectSample\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ MavenProjectSample ---
[INFO] Using auto detected provider org.apache.maven.surefire.testng.TestNGProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
testMethod_01
testMethod_02
testMethod_03
testCase_01
testCase_02
testCase_03
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.346 s -- in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.571 s
[INFO] Finished at: 2024-07-16T10:52:43+07:00
Nếu bạn muốn thực thi một class test duy nhất, bạn có thể thực thi lệnh mvn test -Dtest="TheFirstClassTest"
mvn test -Dtest="TheFirstClassTest"
Kết quả chạy test:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS D:\PROJECT_FRAMEWORK\MavenProjectSample> mvn test -Dtest="TheFirstClassTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.anhtester:MavenProjectSample >------------------
[INFO] Building MavenProjectSample 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ MavenProjectSample ---
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ MavenProjectSample ---
[INFO] skip non existing resourceDirectory D:\PROJECT_FRAMEWORK\MavenProjectSample\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ MavenProjectSample ---
[INFO] Using auto detected provider org.apache.maven.surefire.testng.TestNGProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.anhtester.TheFirstClassTest
testMethod_01
testMethod_02
testMethod_03
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.334 s -- in com.anhtester.TheFirstClassTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.551 s
[INFO] Finished at: 2024-07-16T10:58:49+07:00
Nếu bạn muốn thực thi một phương thức test trong một class nào đó, bạn có thể thực thi lệnh mvn test -Dtest="TheFirstClassTest#testMethod_01"
nó sẽ thực thi phương thức testMethod_01 trong class TheFirstClassTest
mvn test -Dtest="TheFirstClassTest#testMethod_01"
Kết quả chạy test:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS D:\PROJECT_FRAMEWORK\MavenProjectSample> mvn test -Dtest="TheFirstClassTest#testMethod_01"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.anhtester:MavenProjectSample >------------------
[INFO] Building MavenProjectSample 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ MavenProjectSample ---
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ MavenProjectSample ---
[INFO] skip non existing resourceDirectory D:\PROJECT_FRAMEWORK\MavenProjectSample\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ MavenProjectSample ---
[INFO] Using auto detected provider org.apache.maven.surefire.testng.TestNGProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.anhtester.TheFirstClassTest
testMethod_01
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.333 s -- in com.anhtester.TheFirstClassTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.578 s
[INFO] Finished at: 2024-07-16T11:03:59+07:00
Plugin Maven surefire có cung cấp cho chúng ta các tham số để có thể chạy các test linh hoạt hơn.
-Dtest="TestClassName1, TestClassName2, TestClassName3…"
-Dtest="*ServiceUnitTest" hoặc -Dtest="The*UnitTest, Controller*Test"
-Dtest="ClassName#method1+method2"
-Dtest="ClassName#whenSomethingHappens_*"
mvn test -Dtest="TheFirstClassTest#testMethod_*"
Trong class TheFirstClassTest có 3 phương thức test giống nhau đoạn đầu là testMethod_
Kết quả chạy test:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS D:\PROJECT_FRAMEWORK\MavenProjectSample> mvn test -Dtest="TheFirstClassTest#testMethod_*"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.anhtester:MavenProjectSample >------------------
[INFO] Building MavenProjectSample 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ MavenProjectSample ---
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ MavenProjectSample ---
[INFO] skip non existing resourceDirectory D:\PROJECT_FRAMEWORK\MavenProjectSample\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ MavenProjectSample ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ MavenProjectSample ---
[INFO] Using auto detected provider org.apache.maven.surefire.testng.TestNGProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.anhtester.TheFirstClassTest
testMethod_01
testMethod_02
testMethod_03
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.344 s -- in com.anhtester.TheFirstClassTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.572 s
[INFO] Finished at: 2024-07-16T11:12:44+07:00
Plugin Maven surefire cung cấp các tham số trong câu lệnh để thực thi các bài kiểm tra giúp chúng ta linh hoạt trong việc lựa chọn các bài kiểm tra mà chúng ta muốn thực hiện.
Ở phần trên Anh Tester đã chia sẻ một số định dạng với các giá trị thường được sử dụng của các tham số trong câu lệnh thực thi test. Chúng ta đã biết cách chỉ chạy các lớp kiểm tra hoặc phương thức kiểm tra được chỉ định với Maven. Rất tiện lợi cho việc chạy CICD sau này trên các server môi trường khác nhau mà không cần đến IDE như lúc đang coding.
Ngoài ra còn rất nhiều lệnh khác của Maven hỗ trợ, các bạn có thể tham khảo thêm để thực hiện đối với project test automation của mình nhé 🙌
Anh Tester
Đườ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