[JAVA] How to filter JUnit Test in Gradle

--tests option

You can specify the target test class (HogeTest) as follows

./gradlew test --tests com.example.HogeTest

It is also possible to specify the method

./gradlew test --tests com.example.HogeTest.fooMethodTest

You can also specify multiple tests using wildcards (*)

./gradlew test --tests com.example.\*Test

This option allows you to specify "tests to run", but not "tests not to run". There is also an option called -Dtest.single, but it can be replaced with --tests.

ʻInclude / ʻexclude function

You can specify tests to include / exclude in the test target

test {
    include 'com/example/HogeTest.class'
    exclude '**/*Foo*'
}

Unlike --tests, specify the path to the .class file.

Test Grouping

By writing in build.gradle, you can include / exclude tests belonging to a specific category.

test {
    useJUnit {
        includeCategories 'com.example.CategoryA'
        excludeCategories 'com.example.CategoryB', 'com.example.CategoryC'
    }
}

Category is created as Interface and specified through @Category annotation

interface MyCategory

@Category(MyCategory::class)
class HogeTest {
    // some tests...
}

By the way, when I used this, the test with @RunWith (JUnitRunner :: class) was not recognized (mystery).

Bonus: Android settings

On Android, describe the settings in the following block of build.gradle

android {
    unitTests.all {
        // here
    }
}

reference

Recommended Posts

How to filter JUnit Test in Gradle
JUnit 5: How to write test cases in enum
How to run JUnit in Eclipse
How to run Ant in Gradle
How to implement Kalman filter in Java
How to fix system date in JUnit
How to test private scope with JUnit
How to use JUnit 5
How to use Gradle
[Java] How to test for null with JUnit
How to test interrupts during Thread.sleep with JUnit
How to use "sign_in" in integration test (RSpec)
How to test private methods with arrays or variadic arguments in JUnit
How to use JUnit (beginner)
Test private methods in JUnit
Test private methods in JUnit
How to test file upload screen in Spring + Selenium
How to write Junit 5 organized
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit
How to dynamically switch JDK when building Java in Gradle
How to use Lombok in Spring
How to find May'n in XPath
How to unit test Spring AOP
How to hide scrollbars in WebView
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to use InjectorHolder in OpenAM
Output JUnit test report in Maven
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
[RSpec] How to write test code
How to concatenate strings in java
How to install Swiper in Rails
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to implement UI automated test using image comparison in Selenium
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails
How to implement date calculation in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
Control test order in Junit4 with enumeration
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
[Swift] How to fix Label in UIPickerView
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial