Friday, 3 September 2021

Junit: Test Suite: @IncludeClassNamePatterns

@IncludeClassNamePatterns specifies regular expressions that are used to match against fully qualified class names when running a test suite on the JUnit Platform.

 


 

For example, below snippet runs all the test cases in the package "com.sample.app.dummy" and its subpackages.

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
public class IncludePackagesDemo {

}

 

What if you want to selectively run the test classes that matches given class name pattern?

You can use @IncludeClassNamePatternsand @ExcludeClassNamePatterns to explicitly include and exclude the classes.

 

Example
@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
@IncludeClassNamePatterns({"^.*D1Test$"})
public class IncludeClassNamePatternsDemo {

}

 

Above snippet execute below test classes.

 

a.   Dummy1D1Test

b.   Dummy2D1Test

 

Find the below working application.

 

DummyTest.java

 

package com.sample.app.dummy;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class DummyTest {

    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy1Test.java

package com.sample.app.dummy.dummy1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy1D1Test.java

package com.sample.app.dummy.dummy1.d1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1D1Test {

    @Test
    public void test1() {
        assertTrue(true);
    }

}

 

Dummy1D2Test.java

package com.sample.app.dummy.dummy1.d2;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1D2Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy2D1Test.java

package com.sample.app.dummy.dummy2.d1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy2D1Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

IncludeClassNamePatternsDemo.java

package com.sample.app.suite;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
@IncludeClassNamePatterns({"^.*D1Test$"})
public class IncludeClassNamePatternsDemo {

}

 

Run ‘IncludeClassNamePatternsDemo’ class, you will see the executed test methods result in junit report window.


Note

By default, @SelectPackages will only include test classes whose names either begin with Test or end with Test or Tests.


 


Previous                                                    Next                                                    Home

No comments:

Post a Comment