<bdo id='LPnjb'></bdo><ul id='LPnjb'></ul>

        <i id='LPnjb'><tr id='LPnjb'><dt id='LPnjb'><q id='LPnjb'><span id='LPnjb'><b id='LPnjb'><form id='LPnjb'><ins id='LPnjb'></ins><ul id='LPnjb'></ul><sub id='LPnjb'></sub></form><legend id='LPnjb'></legend><bdo id='LPnjb'><pre id='LPnjb'><center id='LPnjb'></center></pre></bdo></b><th id='LPnjb'></th></span></q></dt></tr></i><div id='LPnjb'><tfoot id='LPnjb'></tfoot><dl id='LPnjb'><fieldset id='LPnjb'></fieldset></dl></div>

      1. <tfoot id='LPnjb'></tfoot>
      2. <legend id='LPnjb'><style id='LPnjb'><dir id='LPnjb'><q id='LPnjb'></q></dir></style></legend>

        <small id='LPnjb'></small><noframes id='LPnjb'>

        在套件级别上并行运行 JUnit 测试?

        时间:2023-10-01

        • <bdo id='ZYL7j'></bdo><ul id='ZYL7j'></ul>
          <legend id='ZYL7j'><style id='ZYL7j'><dir id='ZYL7j'><q id='ZYL7j'></q></dir></style></legend>

          <small id='ZYL7j'></small><noframes id='ZYL7j'>

        • <tfoot id='ZYL7j'></tfoot>
            <tbody id='ZYL7j'></tbody>

            <i id='ZYL7j'><tr id='ZYL7j'><dt id='ZYL7j'><q id='ZYL7j'><span id='ZYL7j'><b id='ZYL7j'><form id='ZYL7j'><ins id='ZYL7j'></ins><ul id='ZYL7j'></ul><sub id='ZYL7j'></sub></form><legend id='ZYL7j'></legend><bdo id='ZYL7j'><pre id='ZYL7j'><center id='ZYL7j'></center></pre></bdo></b><th id='ZYL7j'></th></span></q></dt></tr></i><div id='ZYL7j'><tfoot id='ZYL7j'></tfoot><dl id='ZYL7j'><fieldset id='ZYL7j'></fieldset></dl></div>
                1. 本文介绍了在套件级别上并行运行 JUnit 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一堆在 JUnit 测试套件中组织的测试.这些测试极大地利用 selenium 来测试 Web 应用程序.因此,对于 selenium,这些测试的运行时间很长.由于测试数据库中有一些重叠,套件中的测试类无法并行运行,我想并行运行这些套件.

                  I have a bunch of tests that are organized in JUnit test suites. These tests are greatly utilizing selenium to test a web application. So, naturaly for selenium, the runtime of these tests is quite long. Since the test classes in the suites can not run parallel due some overlaps in the test database, i would like to run the suites parallel.

                  JUnit ParallelComputer 只能在类或方法级别并行执行测试,JUnit 是否有任何标准方法可以通过套件执行此操作?

                  The JUnit ParallelComputer can only execute tests on class or method level in parallel, are there any standard ways for JUnit to do that with suites?

                  如果我只是将套件类传递给 junit 运行器并将计算机配置为在类级别并行化,它会选择测试类本身,而不是套件.

                  If i just pass suite classes to the junit runner and configure the computer to parallelize on class level, it picks the test classes itself, not the suites.

                  br弗兰克

                  推荐答案

                  这里有一些对我有用的代码.我没有写这个.如果您使用 @RunWith(ConcurrentSuite.class) 而不是 @RunWith(Suite.class) 它应该可以工作.下面还需要一个注释.

                  Here is some code that worked for me. I did not write this. If you use @RunWith(ConcurrentSuite.class) instead of @RunWith(Suite.class) it should work. There is an annotation that is also needed which is found below.

                  package utilities.runners;
                  
                  import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
                  import org.junit.runner.Runner;
                  import org.junit.runners.Suite;
                  import org.junit.runners.model.InitializationError;
                  import org.junit.runners.model.RunnerBuilder;
                  import org.junit.runners.model.RunnerScheduler;
                  
                  import utilities.annotations.Concurrent;
                  
                  import java.util.Arrays;
                  import java.util.LinkedList;
                  import java.util.List;
                  import java.util.Queue;
                  import java.util.concurrent.CompletionService;
                  import java.util.concurrent.ExecutorCompletionService;
                  import java.util.concurrent.ExecutorService;
                  import java.util.concurrent.Executors;
                  import java.util.concurrent.Future;
                  import java.util.concurrent.ThreadFactory;
                  import java.util.concurrent.atomic.AtomicInteger;
                  
                  /**
                   * @author Mathieu Carbou (mathieu.carbou@gmail.com)
                   */
                  public final class ConcurrentSuite extends Suite {
                      public ConcurrentSuite(final Class<?> klass) throws InitializationError {
                          super(klass, new AllDefaultPossibilitiesBuilder(true) {
                              @Override
                              public Runner runnerForClass(Class<?> testClass) throws Throwable {
                                  List<RunnerBuilder> builders = Arrays.asList(
                                          new RunnerBuilder() {
                                              @Override
                                              public Runner runnerForClass(Class<?> testClass) throws Throwable {
                                                  Concurrent annotation = testClass.getAnnotation(Concurrent.class);
                                                  if (annotation != null)
                                                      return new ConcurrentJunitRunner(testClass);
                                                  return null;
                                              }
                                          },
                                          ignoredBuilder(),
                                          annotatedBuilder(),
                                          suiteMethodBuilder(),
                                          junit3Builder(),
                                          junit4Builder());
                                  for (RunnerBuilder each : builders) {
                                      Runner runner = each.safeRunnerForClass(testClass);
                                      if (runner != null)
                                          return runner;
                                  }
                                  return null;
                              }
                          });
                          setScheduler(new RunnerScheduler() {
                              ExecutorService executorService = Executors.newFixedThreadPool(
                                      klass.isAnnotationPresent(Concurrent.class) ?
                                              klass.getAnnotation(Concurrent.class).threads() :
                                              (int) (Runtime.getRuntime().availableProcessors() * 1.5),
                                      new NamedThreadFactory(klass.getSimpleName()));
                              CompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
                              Queue<Future<Void>> tasks = new LinkedList<Future<Void>>();
                  
                              @Override
                              public void schedule(Runnable childStatement) {
                                  tasks.offer(completionService.submit(childStatement, null));
                              }
                  
                              @Override
                              public void finished() {
                                  try {
                                      while (!tasks.isEmpty())
                                          tasks.remove(completionService.take());
                                  } catch (InterruptedException e) {
                                      Thread.currentThread().interrupt();
                                  } finally {
                                      while (!tasks.isEmpty())
                                          tasks.poll().cancel(true);
                                      executorService.shutdownNow();
                                  }
                              }
                          });
                      }
                  
                      static final class NamedThreadFactory implements ThreadFactory {
                          static final AtomicInteger poolNumber = new AtomicInteger(1);
                          final AtomicInteger threadNumber = new AtomicInteger(1);
                          final ThreadGroup group;
                  
                          NamedThreadFactory(String poolName) {
                              group = new ThreadGroup(poolName + "-" + poolNumber.getAndIncrement());
                          }
                  
                          @Override
                          public Thread newThread(Runnable r) {
                              return new Thread(group, r, group.getName() + "-thread-" + threadNumber.getAndIncrement(), 0);
                          }
                      }
                  
                  }
                  

                  并且注解如下.

                  package utilities.annotations;
                  
                  import java.lang.annotation.ElementType;
                  import java.lang.annotation.Retention;
                  import java.lang.annotation.RetentionPolicy;
                  import java.lang.annotation.Target;
                  
                  /**
                   * @author Mathieu Carbou (mathieu.carbou@gmail.com)
                   */
                  @Retention(RetentionPolicy.RUNTIME)
                  @Target({ ElementType.TYPE })
                  public @interface Concurrent {
                      int threads() default 5;
                  }
                  

                  这篇关于在套件级别上并行运行 JUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将特定键绑定到 Eclipse 中的不同启动配置? 下一篇:TestNG retryAnalyzer 仅在方法@Test 中定义时有效,在

                  相关文章

                  最新文章

                  <legend id='C5FMU'><style id='C5FMU'><dir id='C5FMU'><q id='C5FMU'></q></dir></style></legend>
                2. <i id='C5FMU'><tr id='C5FMU'><dt id='C5FMU'><q id='C5FMU'><span id='C5FMU'><b id='C5FMU'><form id='C5FMU'><ins id='C5FMU'></ins><ul id='C5FMU'></ul><sub id='C5FMU'></sub></form><legend id='C5FMU'></legend><bdo id='C5FMU'><pre id='C5FMU'><center id='C5FMU'></center></pre></bdo></b><th id='C5FMU'></th></span></q></dt></tr></i><div id='C5FMU'><tfoot id='C5FMU'></tfoot><dl id='C5FMU'><fieldset id='C5FMU'></fieldset></dl></div>

                  <small id='C5FMU'></small><noframes id='C5FMU'>

                      <bdo id='C5FMU'></bdo><ul id='C5FMU'></ul>
                    1. <tfoot id='C5FMU'></tfoot>