1

I want to keep my groovy source files in their own directory, with the tests being in a separate directory.

I have the directory structure as follows:

.
├── build
│   └── Messenger.class
├── build.xml
├── ivy.xml
├── lib
├── src
│   └── com
│       └── myapp
│           └── Messenger.groovy
└── test
    └── unit
        ├── AnotherTest.groovy
        └── MessengerTest.groovy

I can successfully run one test by using the groovy command and specifying the class path for the units under test using -cp to point to build/ but how do I run all the tests in the directory?

4
  • How are you running these tests? In Grails, a test-app usually runs all the tests unless you explicitly limit them so I assume you are using a non-standard way? Commented Dec 18, 2014 at 8:23
  • This isn't a Grails app. Commented Dec 18, 2014 at 15:12
  • Then remove the grails tag Commented Dec 18, 2014 at 15:13
  • I have it there because I'm using the directory structure. Commented Dec 18, 2014 at 15:14

2 Answers 2

1

Tou can run all unit test with command:

grails test-app unit:

If you have unit, integration, functional... tests you can run all tests with command:

grails test-app

Sign up to request clarification or add additional context in comments.

2 Comments

This isn't a Grails app, I'm just mimicking the directory structure a bit.
I'm not using an IDE, that's why I was running the tests using the groovy command.
0

I am new to groovy, but I wrote my own test runner and put it in root directory of my project. Source code:

import groovy.util.GroovyTestSuite
import junit.textui.TestRunner
import junit.framework.TestResult
import static groovy.io.FileType.FILES


public class MyTestRunner {
    public static ArrayList getTestFilesPaths(String test_dir) {
        // gets list of absolute test file paths
        ArrayList testFilesPaths = new ArrayList();

        new File(test_dir).eachFileRecurse(FILES) {
            if(it.name.endsWith(".groovy")) {
                testFilesPaths.add(it.absolutePath)
            }
        }

        return testFilesPaths;
    }

    public static GroovyTestSuite getTestSuite(ArrayList testFilesPaths) {
        // creates test suite using absolute test file paths
        GroovyTestSuite suite = new GroovyTestSuite();
        testFilesPaths.each {
            suite.addTestSuite(suite.compile(it));
        }
        return suite;
    }

    public static void runTests(GroovyTestSuite suite) {
        // runs test in test suite
        TestResult result = TestRunner.run(suite);
        // if tests fail return exit code non equal to 0 indicating that
        // tests fail it helps if one of your build step is to test files
        if (!result.wasSuccessful()) {
            System.exit(1);
        }
    }
}

ArrayList testFilesPaths = MyTestRunner.getTestFilesPaths("tests");
GroovyTestSuite suite = MyTestRunner.getTestSuite(testFilesPaths);
MyTestRunner.runTests(suite)

if you try to use this be aware that if it fails it is most likely that getTestFilesPaths is not working properly.

My directory structure

.
├── test_runner.groovy
├── src
│   └── ...
└── tests
    └── Test1.groovy
    └── someDir
        ├── Test2.groovy
        └── Test3.groovy

How to run
From the same directory where test_runner.groovy is run:

groovy test_runner.groovy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.