0

I'm trying to automate a way to grade multiple students' files at once in my intro to Java class.

How can I run the same test(s) on all the files in a folder assuming the following conditions are true:

  • All files exist in the same folder (no sub-folders)
  • All files have a .java extension
  • All filenames are important identifiers
  • The code in every file meets the same requirements
    SayHelloWorld
    |-bsmithSayHelloWorld.java
    |-athomasSayHelloWorld.java
    |-cjenkinsSayHelloWorld.java

I'm comfortable with JUnit but I've only used it in a single-file, clunky way in the past through IntelliJ.

Here's a simple sample file I'd be testing against:

public class Main {

    public static void main(String[] args) {
        System.out.println(sayHello());
    }
    public static String sayHello(){
        return "hello Java";
    }
}

I would really like to be able to automate unit testing on all the .java files that exist in a given folder and output the results to something I can work with (console, file, whatever).

Possible output might look like:

bsmithSayHelloWorld helloWorldTest: pass
athomasSayHelloWorld helloWorldTest: fail
cjenkinsSayHelloWorld  helloWorldTest: pass
tkruse
  • 10,222
  • 7
  • 53
  • 80
mpcsf
  • 1
  • 1
  • As long as I understand, every class you want to test has the same method, let's say `sayHello()` and you want to execute the same test for each class, let's say: `testHelloWorld()` is it correct? – sirandy May 29 '19 at 23:32
  • 1
    Is this for comparing student submissions? – msg45f May 29 '19 at 23:34
  • Yes, exactly @sirandy. I will provide students with a base file with something wrong, in this case an empty `sayHello()` method, and they will have to fill it in with the correct return statement. – mpcsf May 29 '19 at 23:37
  • 2
    I suggest you implementing `Paremeterized` tests, here's an approach for it: https://stackoverflow.com/a/6724555/1670134 it works with interfaces but you could modify to accept concrete classes. – sirandy May 29 '19 at 23:38
  • @msg45f I want to use it to do an initial sweep of an assignment to see who got it _exactly_ right and I can give a 100% and then I'll dig into the ones that failed unit tests to manually grade further. – mpcsf May 29 '19 at 23:39
  • @mpcsf Not an answer as it's a very different solution (and another level of complexity), but what I've done in the past is to use Github classrooms and Jenkins (automation server) with an organization build pipeline. Automatically builds projects when my students pushed their code, ran tests, and generated a report for me. – msg45f May 29 '19 at 23:54
  • @msg45f do you have your process documented anywhere? – mpcsf May 30 '19 at 11:24
  • @mpcsf Sadly, not really, though I would love to build up some documentation for it when I have some time. – msg45f May 30 '19 at 15:27

1 Answers1

0

Since Java11 you can run single java classes without compiling.

So if you are free to use Java 11, you can loop over the java files in the folder with a given input, and then check the program output against expectations.

The same can be achieved using many other scripting solutions, like ant, make, gradle, python, ruby, ...

tkruse
  • 10,222
  • 7
  • 53
  • 80