0

The solution I am using to create a JUnit test suite dynamically can be found in this similar question here: How do I Dynamically create a Test Suite in JUnit 4?

The solution I am trying to adapt looks like such:

@RunWith(AllTests.class)
public class SomeTests
{
    public static TestSuite suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JUnit4TestAdapter(Test1.class));
        suite.addTest(new JUnit4TestAdapter(Test2.class));

        return suite;
     }
}

However, I would not only like to be able to dynamically create a test suite, but also be able to allow for the user running my program to specify which tests they would like to run using a properties file.

Is there a way I can annotate my classes with a String such that I can get the actual class type given the annotation String? Are there any viable solutions for this or is it just bad practice in general?

Community
  • 1
  • 1
kshah
  • 1,667
  • 2
  • 16
  • 24
  • Do you want to achieve something similar like [JUnit Categories](https://github.com/junit-team/junit4/wiki/Categories) or rather something dynamic like [Maven test](http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html), where you can specify via `-Dtest=NameOfTestClass,NameOfOtherTestClass` or even `-Dtest=NameOfTestClass#testMethod+othertestMethod` what tests classes or methods should be executed? – Roman Vottner Jun 09 '16 at 11:22

1 Answers1

0

As I understand you would like to mark your test classes as belonging to one or more groups, then user defines the group of test cases to execute. Is it correct?

Bad or good this practice is, it is already implemented in TestNG. There is no such feature in JUnit. But you could easily add annotation scanner to your code and select proper test classes dynamically (e.g. by using https://github.com/ronmamo/reflections to collect all your annotated tests in class path with just single line of code).

volkovs
  • 1,153
  • 2
  • 11
  • 24
  • 1
    As Roman indicated, JUnit does have this feature. You can even specify which categories to include on the command line as of JUnit 4.12 – NamshubWriter Jun 11 '16 at 15:08