Sunday 5 September 2021

Junit5: Test Suite: @IncludeTags

@IncludeTags specifies the tags or tag expressions to be included 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 sub packages.

@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 tags?

You can use @IncludeTags annotation to explicitly include the classes that matches to given tags.

 

Example

 

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
@IncludeTags({"prod", "qa"})
public class IncludeTagsDemo {

}

 

Above snippet execute below test classes.

 

a.   DummyTest

b.   Dummy1D1Test

 

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.Tag;
import org.junit.jupiter.api.Test;

@Tag("prod")
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.Tag;
import org.junit.jupiter.api.Test;

@Tag("qa")
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.Tag;
import org.junit.jupiter.api.Test;

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

 

IncludeTagsDemo.java

package com.sample.app.suite;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.IncludeTags;
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"})
@IncludeTags({"prod", "qa"})
public class IncludeTagsDemo {

}

  Run above application, you will see the executed test classes information in junit report window.

 


 

 

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment