Search code examples
javaunit-testingdynamicjunittestcase

How to dynamically invoke Junits


I have multiple instances of junit.framework.TestCase classes that contain various testcases. I want to write a program that runs particular testcases from these instances.

Is there a way to dynamically invoke junit tests, similar to the invoke() method from java.lang.reflect API?


Solution

  • As it's not really clear what you want to achieve. Find below some more general possiblities. If you are using Maven you could

    1. run specific tests

    (for more examples/possiblities see maven-surefire-plugin: single-test)

    mvn -Dtest=YourClass* test
    
    1. annotate classes for specific JUnit categories

    (for more examples/possiblities see maven-surefire-plugin: JUnit_Categories)

    mvn test -Dgroups="your.test.group.Example"
    

    edit For ivy this SO post how-to-run-all-junit-tests-in-a-category-suite-with-ant might help to solve youre requirement.