2

Let's say I have 50 different versions of the same project in 50 different directories under one parent directory. I have written the JUnit test to test the project in each directory and it works great. But the problem is I have to run it 50 times to get the test result.

Is there any way I can automate it to run the test on all the subdirectories under the parent directory?

I am using Eclipse right now. But willing to switch to Netbeans or any other tools but prefer Windows OS.

Note: I will use it to grade java programming assignments. Which means I have 50 submissions in 50 different directories under one parent directory. I have written test cases to grade each submission. Now I have to import each project and run test cases manually. Now I want to automate it to reduce time.

Osmani
  • 669
  • 7
  • 19
  • 1
    Possible duplicate of [Is it possible to run JUnit tests from multiple packages in Eclipse?](http://stackoverflow.com/questions/413214/is-it-possible-to-run-junit-tests-from-multiple-packages-in-eclipse) – Tim Biegeleisen Oct 15 '15 at 01:35
  • I am not using different packages for the same project. I have different versions of the same project in different directories. – Osmani Oct 15 '15 at 01:38
  • 1
    Sounds like you'll have to run each batch of tests in their own classpath that includes the version which your're _currently_ testing. However I don't know any IDE currently supporting such a feature. Perhaps you could create your own class which iterates through the version dirs, defines the classpath for each run, and invokes JUnit... – Morfic Oct 15 '15 at 21:07
  • 1
    How about committing all 50 solutions as 50 branches of the same project in e.g. git and run exactly the same build+test job outside of eclipse switching branches in between? So your test source tree remains static and the main source tree changes from branch to branch. This would be a couple of lines of scripting. – Oleg Sklyar Oct 24 '15 at 20:08
  • sounds like a very good idea. I was wondering is there any easy way or tool exists that I don't know...thanks! – Osmani Oct 24 '15 at 20:09
  • 2
    I do not think there is any ready-made solution for that, but a script in bash (gitbash shell on windows) or python should be really simple. Not pretending to be any scripting language, but an algorithm would look like: `for project in names` `git checkout $project` `mvn clean build > ../$project-results`. I could even script it for you if you want in bash, you would only need git and gitbash for windows – Oleg Sklyar Oct 24 '15 at 20:13
  • that would be great! I am still learing git. – Osmani Oct 24 '15 at 20:16
  • There are a lot of teachers doing similar things, this is a case where if you broaden your question on Google you will find some good results. For example "automatically checking student code submissions" results in a treasure trove of options. – Jonah Graham Oct 25 '15 at 10:45
  • __Q:__ What are these 50 version representing? Are these 50 really 50 version of the same library? Which are needed to be tested? Or are these 50 implementations of the same interface? (By students for example)? – Verhagen Oct 26 '15 at 15:44

3 Answers3

1

To run JUnit from the command line, you need to put three things on your classpath:

  1. The junit.jar
  2. Your test class
  3. The class version to test

This can be done like so (assuming you are in the directory with the version you want to test):

java -cp .;C:\path\to\junit.jar;C:\path\to\your\test\class org.junit.runner.JUnitCore TestClass

Since you are on Windows, you might want to simply use FOR to automate this (here assuming the version subdirs are in the C:\versions\ directory:

FOR /D %F IN (C:\versions\*) DO java -cp .;C:\path\to\junit.jar;%F org.junit.runner.JUnitCore TestClass

This will loop through all the version subdirs and run your test class in each of them.

Tomas
  • 1,315
  • 10
  • 17
  • I will test it and let you know. Thanks! – Osmani Oct 28 '15 at 01:09
  • 1
    I just updated the `FOR` example, I had forgotten the `%F` argument. By the way, make sure you use `%F` in both places since it is case-sensitive. Or use `%f` if you prefer that :) – Tomas Oct 28 '15 at 14:47
  • I have used the following command to execute one version at a time: java -cp .;"C:\Program Files\NetBeans 8.0\platform\modules\ext\junit-4.10.jar";C:\Test\HW4Test org.junit.runner.JUnitCore HW4Test but it gives an error saying NoClassDefFoundError: HW4Test$1 – Osmani Oct 28 '15 at 22:05
  • Hmm I was assuming you had compiled the classes first. What's in that directory? – Tomas Oct 29 '15 at 10:06
  • just java files of the classes I have to test. I also put compiled class file of the Test class, but it says NoClassDefFoundError for Test class. – Osmani Oct 30 '15 at 04:09
  • @Osmani You of course need to run `javac` to compile the classes first, JUnit doesn't do that for you. – Tomas Oct 30 '15 at 04:15
  • I did compile both test class and other two classes. Please look at the following two links where I put screenshots. Actor and Movie are classes that are being tested by the HW4Test class. http://www.tiikoni.com/tis/view/?id=37fe210 http://www.tiikoni.com/tis/view/?id=bdfb5ae – Osmani Oct 30 '15 at 04:26
  • Is the HW4Test class in the same package (possibly the default one)? It seems the anonymous inner class (the `$1` bit) is indeed missing from that dir (second image), what happens if you remove it from `.` and only keep it in the HW4Test dir? What does that dir look like: – Tomas Oct 30 '15 at 04:35
  • Yeah, its looking for HW4Test$1. What's the solution for that? I put HW4Test in the same dir with other two classes. Here is the screenshot of parent directory: http://www.tiikoni.com/tis/view/?id=e19f09b – Osmani Oct 30 '15 at 04:55
  • It did run when I put anonymous inner class inside the dir. Now Its giving me other errors. I will let you if I can't solve it. Thanks!! – Osmani Oct 30 '15 at 04:59
  • @Osmani Hmm not sure what's up here. This is however veering off a bit from the original question, maybe open a new one? – Tomas Oct 30 '15 at 05:01
0

You might create your own JUnit Runner. Which scans the 50 directories, or executes 50 times the same test cases, but for different class paths.

Verhagen
  • 3,885
  • 26
  • 36
-1

The CI servers are designed to do exatly this - manage the runs of your automated tests.

Is there any way I can automate it to run the test on all the subdirectories under the parent directory?

Every middle range server should give you all this functionality. Since you've tagged Java, the most common ones are:

You don't need to have 50 different jobs/builds, just pass the path to the project as param to the build. As example Jenkins support parameterized builds.

ekostadinov
  • 6,880
  • 3
  • 29
  • 47