2

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?

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
Sid J
  • 33
  • 1
  • 6
  • 3
    Why don't you modularize your tests, then create a suite and runner for the specific tests you want? – Vince Apr 27 '15 at 05:50
  • Are you using Maven for your project? – SubOptimal Apr 27 '15 at 05:57
  • @SubOptimal : No, I am using Ivy dependency management and Ant builder as the build tool. – Sid J Apr 27 '15 at 08:52
  • @VinceEmigh : The tests are already modularized, but there are 100's of tests in each testSuite, and i am only required to check few tests from various suites. What i exactly want is a method that accepts a list of all the tests i wana run (From different testCases), and runs them. – Sid J Apr 27 '15 at 08:56
  • @user2359572 I updated my answer with a link how to solve it with `ant` and `JUnit`. This might help you. – SubOptimal Apr 27 '15 at 10:14
  • Why can't you create a new suite containing the specific cases you need to test, rather than create some method where you pass in your tests? – Vince Apr 28 '15 at 17:00

3 Answers3

0

You can invoke your Junit using JUnitCore, it would be better if you run the whole test suite using Maven as @SubOptimal pointed out.

    JUnitCore junit = new JUnitCore();
    Result result = junit.run(CommonConstantsUnitTest.class);
Sunil Rajashekar
  • 350
  • 2
  • 18
  • But this way all the tests within the mentioned class will get run. I want to run only the ones i specify. – Sid J Apr 27 '15 at 08:57
0

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.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
-2

Try using @Category Annotations You can categories your tests and then invoke them which ever test using @Category or a combination of @Category

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
0o'-Varun-'o0
  • 735
  • 1
  • 5
  • 22