❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 0939206009
  • thaian.it15@gmail.com
  • Facebook
  • Youtube
  • Zalo
Anh Tester Logo
  • Khoá học
    • All Courses
    • Website Testing
    • API Testing
    • Desktop Testing
    • Mobile Testing
    • Programming Language
    • CI/CD for Tester
    • Performance Testing
  • 💥Khai giảng
  • tools
    • TestGenAI - AI Test Cases Generator
    • Mobile Apps Demo
    • Automation Framework Selenium TestNG
    • Automation Framework Cucumber TestNG
    • Gherkin Convert Functions in Katalon
    • Convert object from Selenium Java to Playwright Python
    • Website Demo CRM
    • Website Demo HRM
    • Website Demo HRM GO
    • Website Demo POS
    • Website Demo eCommerce CMS
  • blog
    • Selenium C#
    • Selenium Java
    • Katalon Tools
    • Jenkins CI/CD
    • SQL cho Tester
    • Manual Testing
    • Tài liệu cho Tester
    • Automation Testing
    • akaAT Tools
    • Cucumber TestNG
    • API Testing with Postman
    • Apache Maven
    • AI in Software Testing
    • Lịch khai giảng
  • Liên hệ
  • Log in
    Sign up

Chạy test cases với Maven command line trong Maven project không cần mở IDE

  • Blog
  • Apache Maven
Chạy test cases với Maven command line trong Maven project không cần mở IDE

Chạy test cases với Maven command line trong Maven project không cần mở IDE

  • Anh Tester
  • Apache Maven
  • 2582
Anh Tester chia sẻ cách chạy test cases không cần mở IDE với sự hỗ trợ của Apache Maven tool, và tức nhiên chỉ áp dụng đối với Maven project đang sử dụng Java.

🔆 Ví dụ có 2 class mẫu trong Maven project

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");
    }
}

 

✅ Chạy tất cả các test cases đang tồn tại trong project

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

 

✅ Chạy một class test chỉ định

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

 

✅ Chạy một phương thức test chỉ định trong một class

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

 

✅ Chạy test với nhiều cách nâng cao khác

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.

  • Thực hiện nhiều lớp kiểm tra theo tên cụ thể:
    -Dtest="TestClassName1, TestClassName2, TestClassName3…"​
  • Thực hiện nhiều lớp kiểm tra theo format tên:
    -Dtest="*ServiceUnitTest" hoặc -Dtest="The*UnitTest, Controller*Test"​
  • Thực hiện nhiều phương thức kiểm tra theo tên cụ thể:
    -Dtest="ClassName#method1+method2"​
  • Thực hiện nhiều phương thức kiểm tra theo format tên:
    -Dtest="ClassName#whenSomethingHappens_*"​

Ví dụ trường hợp cụ thể đối với sample project bên trên, giờ mình chạy các phương thức test có tên theo format giống đoạn đầu trong tên chỉ định.

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é 🙌

  • Tags:
  • Apache Maven
  • Maven project
  • Maven Command Line

Chia sẻ bài viết

Facebook Linkedin Telegram Pinterest Share with Zalo Zalo

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

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

  • Anh Tester

    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

    • Facebook
    • Youtube
    • Zalo

Search Blogs

Related Blogs

Chạy test cases với Maven command line trong Maven project không cần mở IDE

Chạy test cases với Maven command line trong Maven project không cần mở IDE

Jul-16-2024 by Anh Tester
Tạo project test Selenium Java và TestNG Framework với Maven

Tạo project test Selenium Java và TestNG Framework với Maven

Jul-15-2024 by Anh Tester
Apache Maven là gì? Cài đặt môi trường Maven trên Windows

Apache Maven là gì? Cài đặt môi trường Maven trên Windows

Jul-15-2024 by Anh Tester
Cập nhật tất cả các thư viện trong Maven file pom.xml bằng lệnh ngắn gọn

Cập nhật tất cả các thư viện trong Maven file pom.xml bằng lệnh ngắn gọn

Apr-22-2024 by Anh Tester
view all

Blog Tags

  • Selenium
  • Xpath
  • Locator
  • Jenkins
  • Testing
  • Tester
  • Thuật ngữ
  • Lộ trình
  • Khóa học
  • Mindset
  • QA
  • QC
  • Checklist
  • Website
  • Mobile
  • Question
  • Answer
  • Phỏng vấn
  • Extension
  • Cucumber
  • Gherkin
  • Agile
  • Scrum
  • Document
  • Testing Level
  • Automation Test
  • Test Cases
  • Trường hợp
  • Katalon
  • JMeter
  • Postman
  • API
  • Manual Test
  • Developer

Anh Tester

Anh Tester profile
Đườ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

Connect me on

  • Facebook
  • Youtube
  • Zalo


Liên hệ

  • 0939206009
  • thaian.it15@gmail.com
  • Anh Tester
  • Donate for Anh Tester
QR Facebook Group
QR Discord Group

Copyright © 2021-2025 Anh Tester Automation Testing