str
str

Reputation: 44959

Dynamically run JUnit test methods

How can a subset of different test methods be run dynamically using JUnit?

The goal is to have a jar file that can be invoked from the command line with arguments (e.g. TestClass1#testMethod1,TestClass1#testMethod2,TestClass2#testMethod1). Running a single test method can easily be done using:

Request request = Request.method('CLASSNAME', 'METHODNAME');
new JUnitCore().run(request);

But how can I add more test methods to the run? Ideally, this would not just repeat the JUnitCore().run() for every method, but combine all the defined methods to a single run. How can this be done?

Upvotes: 0

Views: 331

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

I think you can filter classes/methods to run:

    Request request = Request.classes(...
    Filter filter = new Filter() {
        @Override
        public boolean shouldRun(Description description) {
            ... get class and method and decide...

        }
        @Override
        public String describe() {
            return null;
        }
    };
    request.filterWith(filter);

Upvotes: 1

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

Maybe you can use runClasses(java.lang.Class<?>... classes) (Run the tests contained in classes).

http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html

Upvotes: 0

Related Questions