0
0
JUnittesting~5 mins

Gradle test task in JUnit

Choose your learning style9 modes available
Introduction

The Gradle test task runs your automated tests to check if your code works correctly. It helps catch mistakes early.

When you want to run all your unit tests after writing new code.
Before sharing your code with teammates to make sure nothing is broken.
To automatically run tests during continuous integration (CI) builds.
When you want to check test results and see which tests passed or failed.
To run tests with specific settings like showing detailed logs or running only some tests.
Syntax
JUnit
gradle test

This command runs all tests in your project using the default test framework (like JUnit).

You can customize the test task in your build.gradle file to change how tests run.

Examples
Runs all tests in the project with default settings.
JUnit
gradle test
Runs tests and shows more detailed information in the console.
JUnit
gradle test --info
Runs only the tests in the specified test class.
JUnit
gradle test --tests "com.example.MyTestClass"
Runs all tests in the specified package.
JUnit
gradle test --tests "com.example.*"
Sample Program

This Gradle build script applies the Java plugin, configures the test task to use JUnit Platform, and sets test logging to show passed and failed tests.

When you run gradle test, Gradle will find and run all JUnit tests and show which tests passed or failed.

JUnit
plugins {
    id 'java'
}

test {
    useJUnitPlatform()
    testLogging {
        events 'passed', 'failed'
    }
}

// Run with: gradle test
OutputSuccess
Important Notes

Make sure your test classes and methods follow JUnit naming conventions so Gradle can find them.

You can customize the test task in build.gradle to change test behavior, like adding system properties or changing logging.

Use gradle test --scan to get a detailed test report online (requires Gradle Enterprise).

Summary

The Gradle test task runs your automated tests to check code correctness.

You run it with gradle test and can customize it in build.gradle.

It helps catch bugs early and shows which tests passed or failed.