GitHub - MichaelTamm/junit-toolbox: Useful classes for writing automated tests with JUnit 4 (original) (raw)

Overview

The JUnit Toolbox provides some useful classes for writing automated tests with JUnit:

ParallelRunner, ParallelParameterized, and ParallelSuite share a common Fork-Join-Pool. You can control the maximum number of worker threads by specifying the system property maxParallelTestThreads. If this system property is not set, there will be as many worker threads as the number of processors available to the JVM.

How to use it

If you use Maven, add the following dependency to your pom.xml file:

<dependency>
    <groupId>com.googlecode.junit-toolbox</groupId>
    <artifactId>junit-toolbox</artifactId>
    <version>2.4</version>
</dependency>

Release Notes

Version 2.4 (for Java 8) and Version 1.11 (for Java 6)

Version 2.3 (for Java 8)

Version 2.2 (for Java 8) and Version 1.10 (for Java 6)

Version 2.1 (for Java 8) and Version 1.9 (for Java 6)

Version 2.0

private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS)
                                            .pollEvery(100, MILLISECONDS);

@Test
public void test_login() throws Exception {
    // ... enter credentials into login form ...
    clickOnButton("Login");
    wait.until(() -> webDriver.findElement(By.linkText("Logout")).isDisplayed());
    // ...
}

protected void clickOnButton(String label) {
    WebElement button = findButton(label);
    wait.until(button::isDisplayed);
    button.click();
}

Version 1.8

Version 1.7

@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@ExcludeCategories({SlowTests.class, FlakyTests.class})
public class NormalTests {}

Version 1.6

Version 1.5

Version 1.4

@RunWith(WildcardPatternSuite.class)
@SuiteClasses({"**/*Test.class", "!samples/**"})
public class AllTests {}

Version 1.3

private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS).pollEvery(100, MILLISECONDS);

@Test
public void test_auto_complete() throws Exception {
    // Enter "cheese" into auto complete field ...
    ...
    wait.until(new RunnableAssert("'cheesecake' is displayed in auto-complete <div>") { @Override public void run() throws Exception {
        WebElement autoCompleteDiv = driver.findElement(By.id("auto-complete"));
        assertThat(autoCompleteDiv, isVisible());
        assertThat(autoCompleteDiv, containsText("cheesecake"));
    }});
}

Version 1.2

Version 1.1

@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@IncludeCategory(SlowTests.class)
public class OnlySlowTests {}
@RunWith(InnerTestClassesSuite.class)
public class LoginBeanTests {

    public static class UnitTests {
        @Test
        public void test1() { ... }
    }

    @Configuration
    public static class IntegrationTestsConfig { ... }

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = IntegrationTestsConfig.class)
    public static class IntegrationTests {
        @Test
        public void test2() { ... }
    }
}

Version 1.0

@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
public class AllTests {}
@RunWith(ParallelSuite.class)
@SuiteClasses({
    LoginFrontendTest.class,
    FillOutFormFrontendTest.class,
    ...
})
public class AllFrontendTests {}

or you can use a wildcard pattern if you use the @SuiteClasses annotation of JUnit Toolbox:

@RunWith(ParallelSuite.class)
@SuiteClasses("**/*FrontendTest.class")
public class AllFrontendTests {}
@RunWith(ParallelRunner.class)
public class FooTest {
    @Test
    public void test1() {
        // Will be executed in a worker thread
    }
    @Test
    public void test2() {
        // Will be executed concurrently in another worker thread
    }
}