John
John

Reputation: 1601

How to run filtering tests in gradle?

I have to run tests in a specific order using build.gradle file. I have the build.gradle file looks like the following:

test {
    include 'com.my-project.MyTestClass'
    include 'com.my-project.MyTestClass1'
}

but when I running test task I have the following message:

Tests event were not received

How can I fix this problem?

Upvotes: 1

Views: 1426

Answers (1)

Bjørn Vester
Bjørn Vester

Reputation: 7600

That message just means that no tests were actually run. There could be a number of reasons for that, but the most likely given your example is that the include method takes an Ant style file pattern, but you have given it (fully qualified) class names. Also, 'my-project' is not a valid package name, but I assume this is just a error in your example here.

But more importantly, if your intent is to run tests in a specific order, you will not achieve that with a single test task. The specified includes just tell Gradle what tests are part of the suite, but doesn't affect the order.

I don't know what test framework you are using, but I also don't think it is possible with JUnit 4 and 5. The only way I can think of is to create multiple Test tasks in Gradle, where each task represent a single unit test (or group of tests that can be run in any order), and where you order each task through dependsOn. So something like this:

task myTest1(type: Test) {
    include 'example/MyTestClass1.class'
}

task myTest2(type: Test) {
    dependsOn myTest1
    include 'example/MyTestClass2.class'
}

test {
    exclude 'example/**'
    dependsOn myTest2
}

Upvotes: 2

Related Questions