0

Historically, I've always written my unit tests like this:

public void WidgetTest {
    @Test
    public void test_whatever() {
        // Given...

        // When...

        // Then...
    }
}

This allows me to right-click my test file in Eclipse and Run As >> JUnit, and test that 1 particular test, directly from inside Eclipse. Then, when doing a local (Ant-based) build, I configure a <junit> Ant task to run all of my src/test/java tests at once.

I'm now looking for an in-between solution. That is, a way to run all of my test classes from inside Eclipse, all at once, with the click of a button. A co-worker recommended that Junit has a notion of a "test suite" that I could attach all of my test classes to, but it looks like this test suite is some sort of JAR/tool that I don't want to include in my project.

So I ask: how can I define such a "test suite", consisting of all my test classes, and run all of them in one fell swoop, from inside Eclipse? Thanks in advance.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • take a look at this so question http://stackoverflow.com/questions/457276/junit4-test-suites ... after you have create you suite and put all test classes in it you can run it with `right-click` and `run as junit test` – A4L Feb 26 '13 at 13:31

2 Answers2

9

You can right click a project or package in Eclipse and choose to 'Run As->JUnit Test', to run all tests in that project or package. No need for a test suite unless you only want to run a subset of the tests.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • This is for me the cleanest way to do it. TestSuite is not as useful as it was. It may still make sense in some projects (for example Ant + JUNIT 3.X) but useless in some others (maven + JUNIT 4). And from Eclipse, Perception solution will do be ok in any case. – Samuel EUSTACHI Feb 26 '13 at 13:35
  • Thanks @Perception +1 - please see my comment underneath kolja TM's answer - I have the same question for you! Thanks again! – IAmYourFaja Feb 26 '13 at 13:49
  • @DirtyMikeAndTheBoys - np, and yes, it effectively scans your resource (project, package etc) for methods annotated with `@Test`, and then runs them with the embedded jUnit plugin. – Perception Feb 26 '13 at 13:53
  • Would there be a way to do this same thing from within the code? I'm looking to run the entire test suite from the command line, and I figure it'd be easiest to write a separate class which runs the test suite, then call that class. I'm running JUnit 4. – meanderingmoose Jul 24 '14 at 20:54
  • @meanderingmoose - have you considered using Maven to run your tests? That would save a considerable amount of time and trouble as opposed to coding up your own launcher. – Perception Jul 25 '14 at 13:32
  • My end goal is to automate the running of the tests so they go off every week, and this is the easiest way I saw to do that. I haven't looked much into Maven, however. – meanderingmoose Jul 25 '14 at 13:39
  • 1
    @meanderingmoose - definitely consider looking into Maven, ideally plugged into a CM tool like Jenkins. You can look at this SO question [http://stackoverflow.com/questions/255370/automating-unit-tests-junit-for-eclipse-plugin-development] for some hints at interacting with the PDE from the command line, but frankly the tech described is old and I really do believe thats more trouble than its worth. – Perception Jul 25 '14 at 14:39
1

Have you tried to right-click your project and selecting and selecting Run as -> JUnit test? That runs all Unittests in the project.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • 1
    Thanks @kolja TM (+1) - how does it know which are unit tests? Does it just scan for the `@Test` method? Thanks again! – IAmYourFaja Feb 26 '13 at 13:49