Urbanleg
Urbanleg

Reputation: 6532

Junit - Executing a class with variety of tests that resides on different classes

I have the following test classes:

TestClass1 (10 tests)
TestClass2 (20 tests)
TestClass3 (15 tests)
TestClass4 (25 tests)

I would like to create a test class called

 SmallTestClass

that executes 2 tests from TestClass1, 3 tests from TestClass2, 5 tests from TestClass3, 4 tests from TestClass4

How can a achieve this ? I Use spring framework and junit 4

Upvotes: 0

Views: 66

Answers (1)

mystarrocks
mystarrocks

Reputation: 4088

You would need a combination of Junit 4 Category and Suite to achieve this. Note that the Category is still an experimental feature, having been introduced in v4.8.

The below example would only ensure only someTest1() gets run (I used v4.11 myself to test) since it's categorized to be a SanityTest.

The aggregator:

import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;

@RunWith (Categories.class)
@SuiteClasses (SomeTest.class)
@IncludeCategory (SanityTests.class)
public class JunitSuiteTest {
}

The class containing the actual tests:

import org.junit.Test;
import org.junit.experimental.categories.Category;

public class SomeTest {
    @Test
    @Category (SanityTests.class)
    public void someTest1()
    {
        System.out.println("test 1");
    }

    @Test
    public void someTest2()
    {
        System.out.println("test 2");
    }
}

A marker class to help categorize:

public class SanityTests {
}

Upvotes: 1

Related Questions