Reputation: 3067
I have a test class with multiple test methods that I would like to group by some criteria. For this purpose, using JUnit's @Category
annotation on a method level seemed like a fine solution:
public class TestClass {
@Test
@Category(AssignmentServiceCategory.class)
public void testMethod1() {}
@Test
@Category(OtherCategory.class)
public void testMethod2() {}
}
I would like to create different run configurations in IntelliJ IDEA for those separate categories so that only the test methods annotated with certain category are executed. My configuration looks like this:
However, when I run this, all of the tests from the class where the method is declared are run, instead of only the ones annotated with specified category. Is my configuration incorrect, or does IDEA allow only class-level @Category
annotations?
Versions:
Upvotes: 0
Views: 789
Reputation: 5242
I have (had) the same problem with Intellij 2020.1.2
There seems to be a bug related to what setting you select for search for tests
.
If i choose in single module
all the test are executed, even the ones annotated with other Categories.
If I choose any of the other options (in whole project
, across module dependencies
) it simply works as expected..
Upvotes: 0
Reputation: 14031
UPDATED
I tried to reproduce the issue and could not.
Here's my Test Class
package com.mytests.category;
import org.junit.Test;
import org.junit.experimental.categories.Category;
public class MyTest {
@Test
@Category(PerformanceTests.class)
public void testMethod1() {
System.out.println("method1");
}
@Test
@Category(RegressionTests.class)
public void testMethod2() {
System.out.println("method2");
}
}
Make sure you have the necessary interfaces. In JUnit, you need to create marker interfaces to represent the categories:
package com.mytests.category;
public interface RegressionTests {}
and
package com.mytests.category;
public interface PerformanceTests {}
Then in IntelliJ, I ran the tests once and it creates a configuration for me automatically. Then I edit the configuration
The results were as expected:
Only testMethod1
was executed.
OLDER ANSWER
Or from IntelliJ's Doc (https://www.jetbrains.com/help/idea/run-debug-configuration-junit.html)
Category Select this option if you only want to run test classes and test methods that are annotated either with the category given with the
@IncludeCategory
annotation, or a subtype of this category. Fill in the following fields:Category Specify the desired category. Type category name, or click browseButton and select the desired category in the dialog that opens.
Or You could create a TestSuite
and specify (in there) which categories the suite is to include.
Something like
package org.mytests.category;
import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Categories.class)
@Categories.IncludeCategory(RegressionTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class RegressionTestSuite {
}
Upvotes: 1