From f0698620b00a3963de71e096c5f3dff4967b1ea2 Mon Sep 17 00:00:00 2001 From: Mahmoud Bahaa Date: Wed, 11 Oct 2023 03:53:29 +0300 Subject: [PATCH] Added ParallelInnerTestClassesSuite -- resolves MichaelTamm#23 --- README.md | 27 +++++++++++- .../ParallelInnerTestClassesSuite.java | 44 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/googlecode/junittoolbox/ParallelInnerTestClassesSuite.java diff --git a/README.md b/README.md index 8abacb7..a79305a 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ The JUnit Toolbox provides some useful classes for writing automated tests with * [WildcardPatternSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/WildcardPatternSuite.html) -- A replacement for the JUnit runners `Suite` and `Categories`, which allows you to specify the children classes of your test suite class using a wildcard pattern. Furthermore you can include and/or exclude multiple categories. * [ParallelSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/ParallelSuite.html) -- An extension of the `WildcardPatternSuite`, which executes its children classes concurrently using several worker threads. Although it extends `WildcardPatternSuite` you are not forced to use a wildcard pattern, you can also list the children class using the `@SuiteClasses` annotation known from JUnit. * [InnerTestClassesSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/InnerTestClassesSuite.html) -- A replacement for the JUnit runner `Enclosed` which executes all inner test classes of the class annotated with ` @RunWith(InnerTestClassesSuite.class)`. In contrast to the `Enclosed` runner provided by JUnit it detects if an inner class is actually a test class and ignores all other inner classes. + * [ParallelInnerTestClassesSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/ParallelInnerTestClassesSuite.html) -- A replacement for `InnerTestClassesSuite` which executes its children classes concurrently using several worker threads. -`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. +`ParallelRunner`, `ParallelParameterized`, `ParallelSuite` and `ParallelInnerTestClassesSuite` 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 # @@ -24,6 +25,30 @@ If you use [Maven](http://maven.apache.org), add the following dependency to you # Release Notes # +## Version 2.5 (for Java 8) and Version 1.12 (for Java 6) ## + + * New runner `ParallelInnerTestClassesSuite` which runs all inner test classes of the class annotated with `@RunWith(ParallelInnerTestClassesSuite.class)`. In contrast to the `Enclosed` runner provided by JUnit, it detects if an inner class is actually a test class and ignores all other inner classes. In contrast to `InnerTestClassesSuite` it executes its children classes concurrently using several worker threads. run Example: +``` +@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 2.4 (for Java 8) and Version 1.11 (for Java 6) ## * [WildcardPatternSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/WildcardPatternSuite.html) can now handle wildcard patterns starting with "../" (fixes [#16](https://github.com/MichaelTamm/junit-toolbox/issues/16)) * Fixed edge case where too many threads were created when using one of [ParallelRunner](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/ParallelRunner.html), [ParallelParameterized](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/ParallelParameterized.html), or [ParallelSuite](//michaeltamm.github.io/junit-toolbox/com/googlecode/junittoolbox/ParallelSuite.html) contributed by Till Klister diff --git a/src/main/java/com/googlecode/junittoolbox/ParallelInnerTestClassesSuite.java b/src/main/java/com/googlecode/junittoolbox/ParallelInnerTestClassesSuite.java new file mode 100644 index 0000000..416b223 --- /dev/null +++ b/src/main/java/com/googlecode/junittoolbox/ParallelInnerTestClassesSuite.java @@ -0,0 +1,44 @@ +package com.googlecode.junittoolbox; + +import org.junit.experimental.runners.Enclosed; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.RunnerBuilder; + +/** + * Runs all inner test classes of the class + * annotated with @RunWith(ParallelInnerTestClassesSuite.class). + * It executes its children classes concurrently. You can specify the maximum number + * of parallel test threads using the system property maxParallelTestThreads. + * If this system property is not specified, the maximum number of test threads + * will be the number of {@link Runtime#availableProcessors() available processors.} + * In contrast to the {@link Enclosed} runner provided by + * JUnit, + * it detects if an inner class is actually a test class + * and ignores all other inner classes. + * Example:
+ *     @RunWith(ParallelInnerTestClassesSuite.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() { ... }
+ *         }
+ *     }
+ * 
+ */ +public class ParallelInnerTestClassesSuite extends InnerTestClassesSuite { + public ParallelInnerTestClassesSuite(Class klass, RunnerBuilder runnerBuilder) throws InitializationError { + super(klass, runnerBuilder); + setScheduler(new ParallelScheduler()); + } +}