Home > Mobile >  How to exclude/include certain class or method into test build with gradle task?
How to exclude/include certain class or method into test build with gradle task?

Time:03-18

I try to execute certain tests with Gradle task, but can't understand in which way it is possible:

build.gradle:

plugins {
    id 'java'
}

test{
    testLogging.showStandardStreams = true
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.4'
}

task runTest(type: Test){
    include "Test1" // ---> 1
    filter{
        include "Test1" // ---> 2 
        includeTestsMatching "Test1" // ---> 3
    }
}

code:

-> test
------>java
----------> Test1.java 
----------> Test2.java
public class Test1 {
    @Test
    public void test1(){
        System.out.println("***************** Test 1 ****************");
    }
}

public class Test2 {
    @Test
    public void test2(){
        System.out.println("***************** Test 2 ****************");
    }
}


After I executed gradle clean runTest I expected the *** Test 1 *** will be output but it looks like neither 1 nor 2 or '3' option doesn't work - empty result in the consol

How to correctly set certain tests for execution without xml in the Gradle task?

gradle -version:

------------------------------------------------------------
Gradle 7.4.1
------------------------------------------------------------

CodePudding user response:

You will see at documentation to Test filtering, Gradle's approach would be different.

If you have multiple test types in the same project, you can target a scope constraint that you have based on the settings you have for the test task.

To make this run approach in your case, you can define default scopes:

test{
    testLogging.showStandardStreams = true
    filter{
        includeTestsMatching "Test*" 
    }
}

Alternatively, you can change the default approach by running the command line.

Do you want to just run Test1?

gradle test --tests "*1"

good coding! ¯_(ツ)_/¯

CodePudding user response:

Add @Ignore

public class Test1 {
@Ignore
@Test
public void test1(){
    System.out.println("***************** Test 1 ****************");
}
}

public class Test2 {
@Test
@Ignore
public void test2(){
    System.out.println("***************** Test 2 ****************");
}
}
  • Related