There are 200 test cases. In different classes, each of them has several methods that have several annotations, for example: @RUS @USA @CAN @WORK @STUDY @TRAVEL .

Some contain all the annotations, and some partially, and some generally only a couple.

How to run only those methods that satisfy only the requirement, say: @USA @STUDY , and ignores those in which they do not exist?

    2 answers 2

    Option two:

    1. Write your org.junit.runner.Runner , which will check through the reflexion annotations on the tests and run only the ones you need according to your filter.

    2. Abandon annotations in favor of the existing Categories mechanism, which in principle already solves your problem: each test can be assigned to one or several categories, while running, you can select categories to include and exclude. For your example, it will look something like this:

       public interface Rus {} public interface Usa {} public interface Work {} public interface Study {} public class SomeTests { @Test @Category(Rus.class) public void testFoo() { ... } @Test @Category(Usa.class) public void testBar() { ... } @Test @Category(Study.class) public void testBaz() { ... } } @RunWith(Categories.class) @IncludeCategory({ Usa.class, Study.class }) @SuiteClasses({ SomeTests.class }) public UsaStudyTestSuite {} 
    • Thank you very much! But most likely the first option will approach more, since it is impossible to refuse annotations. I will look for more. If you can, describe in more detail the first option? - Sergio Odintsov pm

    a little custom method, but it works.

     public class TestTemplate extends BaseTest{ @Category(DefaultCategory.CategoryA.class) @Test public void testA(){ System.out.println("A"); } @Category(DefaultCategory.CategoryB.class) @Test public void testB1(){ System.out.println("B1"); } @Category(DefaultCategory.CategoryB.class) @Test public void testB(){ System.out.println("B"); } } 

    Next comes the description of the base class, where it is determined which methods need to be executed. You yourself can create your own categories Rus, Usa, Study in the image of CategoryA and CategoryB

     import lombok.extern.slf4j.Slf4j; import org.junit.*; import org.junit.experimental.categories.Category; import org.junit.rules.TestName; import org.junit.rules.TestRule; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import java.lang.annotation.Annotation; import java.util.HashSet; @Slf4j public class BaseTest { public static String caseCategories; public static String[] currentCaseCategories = null; public static HashSet<String> ignoringCategories = new HashSet<>(); static { System.setProperty("test.category.for.exec", "CategoryB"); } @Rule public TestName name = new TestName(); @Before public void beforeMethod() { currentCaseCategories = getCategories(); if (currentCaseCategories != null) for (String category : currentCaseCategories) { if (!category.contains(System.getProperty("test.category.for.exec"))) ignoringCategories.add(category); } boolean isCurrentCatIgnore = isContains(ignoringCategories, currentCaseCategories); org.junit.Assume.assumeFalse("Ignore next test cases", isCurrentCatIgnore); log.info("###################################################################"); log.info(name.getMethodName()); log.info(System.lineSeparator()); currentCaseCategories = null; } /** * Нам нужно создать категории, которые будем вешать на тестовый метод. */ public static class DefaultCategory { public static class CategoryA implements Category { @Override public Class<?>[] value() { return new Class<?>[0]; } /** * Returns the annotation type of this annotation. * * @return the annotation type of this annotation */ @Override public Class<? extends Annotation> annotationType() { return null; } } public static class CategoryB implements Category { @Override public Class<?>[] value() { return new Class<?>[0]; } /** * Returns the annotation type of this annotation. * * @return the annotation type of this annotation */ @Override public Class<? extends Annotation> annotationType() { return null; } } } /** * Возвращает текущие категории * * @return */ public String[] getCategories() { String cases = null; String[] caseArr = null; if (this.caseCategories == null) return null; // удаляем все лишнеи пути до класса с категориями cases = this.caseCategories.substring(51, this.caseCategories.length() - 2); cases = cases.replace("class ", "").replaceAll("\\s+", ""); caseArr = cases.split(","); for (int i = 0; i < caseArr.length; i++) { caseArr[i] = caseArr[i].replace("$", "."); } return caseArr; } @Rule public TestRule watchman = new TestWatcher() { // этот ментод выполняется до выполнения метода @Test @Override protected void starting(Description description) { super.starting(description); caseCategories = null; for (Annotation annotation : description.getAnnotations()) { String d = annotation.toString(); if (d.startsWith("@org.junit.experimental.categories.Category")) { // запоминаем все аннотации caseCategories = d; } } } }; private boolean isContains(HashSet<String> hashSet, String[] categories) { if (categories != null) for (String s : categories) { if (hashSet.contains(s)) return true; } return false; } } 

    in this case, the testA test () will be ignored just as with the @Ignore annotation