The Gradle test task runs your automated tests to check if your code works correctly. It helps catch mistakes early.
Gradle test task in 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.
gradle test
gradle test --info
gradle test --tests "com.example.MyTestClass"gradle test --tests "com.example.*"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.
plugins {
id 'java'
}
test {
useJUnitPlatform()
testLogging {
events 'passed', 'failed'
}
}
// Run with: gradle testMake 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).
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.
Practice
test task in a JUnit project?Solution
Step 1: Understand the Gradle
Thetesttask roletesttask in Gradle is designed to run automated tests, such as those written with JUnit, to verify code correctness.Step 2: Differentiate from other Gradle tasks
Tasks likecompileJavacompile code,cleanremoves build files, and deployment is separate. Onlytestruns tests.Final Answer:
To run automated tests and check if the code works correctly -> Option BQuick Check:
Gradletest= run tests [OK]
- Confusing test task with compile or clean tasks
- Thinking test deploys the app
- Assuming test cleans build files
Solution
Step 1: Recall the standard Gradle test command
The official Gradle command to run tests isgradle test. This triggers the test task which runs JUnit tests.Step 2: Verify other options are invalid
Commands likerunTests,executeTests, andcheckTestsare not standard Gradle commands and will cause errors.Final Answer:
gradle test -> Option DQuick Check:
Run tests withgradle test[OK]
- Adding extra words like run or execute
- Using non-existent Gradle commands
- Confusing test with build or run
build.gradle:
test {
useJUnitPlatform()
testLogging {
events "passed", "failed"
}
}
What will be the output when you run gradle test and all tests pass?Solution
Step 1: Understand the
ThetestLoggingconfigurationtestLoggingblock withevents "passed", "failed"means Gradle will log passed and failed test events when they occur.Step 2: Predict output when all tests pass
Since all tests pass, passed test events are shown. Failed events do not occur, so they are not shown. Therefore, only passed test events appear in the console.Final Answer:
Both passed and failed test events are shown in the console -> Option AQuick Check:
Passed and failed events configured to show; failed events absent if none fail [OK]
- Assuming no output if tests pass
- Thinking only failed tests show by default
- Ignoring testLogging configuration
build.gradle:
test {
useJUnitPlatform()
testLogging {
events "passed", "failed"
}
include '**/MyTests.java'
}
But running gradle test runs no tests. What is the likely problem?Solution
Step 1: Understand the include pattern usage
Theincludepattern filters test classes by their compiled class file names, not source files. It should match**/MyTests.classor use package style with forward slashes.Step 2: Identify why no tests run
Using**/MyTests.javadoes not match any compiled test classes, so no tests run.Final Answer:
The include pattern should use forward slashes and match class files, not source files -> Option AQuick Check:
Include matches class files, not .java [OK]
- Using .java instead of .class in include patterns
- Confusing useJUnitPlatform with test execution
- Thinking testLogging disables tests
test task to run only tests tagged with @Tag("fast") in JUnit 5. Which build.gradle snippet correctly configures this?Solution
Step 1: Recall how to filter tests by tags in Gradle
Gradle'suseJUnitPlatformaccepts a configuration block where you can specifyincludeTagsto run only tests with certain tags.Step 2: Check each option for correct syntax
test { useJUnitPlatform { includeTags 'fast' } } correctly placesincludeTags 'fast'inside theuseJUnitPlatformblock. Other options place it incorrectly or omituseJUnitPlatform.Final Answer:
test { useJUnitPlatform { includeTags 'fast' } } -> Option CQuick Check:
Tag filtering goes inside useJUnitPlatform block [OK]
- Placing includeTags outside useJUnitPlatform
- Adding includeTags inside testLogging
- Omitting useJUnitPlatform call
Ready to prove it?
Pass the test and this pattern moves unknown
