5

现在我们有一个包含两个工作的项目。1) 是带有单元测试的标准构建。2)是集成测试。他们是这样工作的:

  1. 构建整个项目,运行单元测试,开始集成测试工作
  2. 构建整个项目,将其部署到集成服务器,针对集成服务器运行客户端集成测试

问题是第 2 步)现在需要一个多小时才能运行,我想并行化集成测试,以便它们花费更少的时间。但我不完全确定我可以/应该如何做到这一点。我的第一个想法是我可以有两个这样的步骤 2):

  1. 构建整个项目,运行单元测试,开始集成测试工作
  2. 构建整个项目,将其部署到集成服务器1,针对集成服务器1运行客户端集成测试
  3. 构建整个项目,将其部署到集成服务器2 ,针对集成服务器2运行客户端集成测试

但是,如何在集成服务器 1 上运行一半的集成测试,在集成服务器 2 上运行另一半?我正在使用 maven,所以我可能会找出一些具有故障安全和复杂包含/排除模式的东西。但这听起来像是需要付出很多努力才能维护的东西。EG:当有人添加一个新的集成测试类时,我如何确保它在两台服务器之一上运行?开发人员是否必须修改 maven 模式?

4

3 回答 3

2

我找到了这篇关于如何做到这一点的好文章,但它提供了一种在 Groovy 代码中实现这一点的方法。我几乎遵循了这些步骤,但我还没有编写代码来按持续时间均匀分布测试。但这仍然是一个有用的工具,所以我会分享它。

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;
import org.junit.Ignore;
import org.junit.extensions.cpsuite.ClassesFinder;
import org.junit.extensions.cpsuite.ClasspathFinderFactory;
import org.junit.extensions.cpsuite.SuiteType;
import org.junit.runner.RunWith;
import org.junit.runners.AllTests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RunWith(AllTests.class)
public class DistributedIntegrationTestRunner {

    private static Logger log = LoggerFactory.getLogger(DistributedIntegrationTestRunner.class);

    public static TestSuite suite() {
        TestSuite suite = new TestSuite();

        ClassesFinder classesFinder = new ClasspathFinderFactory().create(true,
                new String[]{".*IntegrationTest.*"},
                new SuiteType[]{SuiteType.TEST_CLASSES},
                new Class[]{Object.class},
                new Class[]{},
                "java.class.path");

        int nodeNumber = systemPropertyInteger("node.number", "0");
        int totalNodes = systemPropertyInteger("total.nodes", "1");

        List<Class<?>> allTestsSorted = getAllTestsSorted(classesFinder);
        allTestsSorted = filterIgnoredTests(allTestsSorted);
        List<Class<?>> myTests = getMyTests(allTestsSorted, nodeNumber, totalNodes);
        log.info("There are " + allTestsSorted.size() + " tests to choose from and I'm going to run " + myTests.size() + " of them.");
        for (Class<?> myTest : myTests) {
            log.info("I will run " + myTest.getName());
            suite.addTest(new JUnit4TestAdapter(myTest));
        }

        return suite;
    }

    private static int systemPropertyInteger(String propertyKey, String defaultValue) {
        String slaveNumberString = System.getProperty(propertyKey, defaultValue);
        return Integer.parseInt(slaveNumberString);
    }

    private static List<Class<?>> filterIgnoredTests(List<Class<?>> allTestsSorted) {
        ArrayList<Class<?>> filteredTests = new ArrayList<Class<?>>();
        for (Class<?> aTest : allTestsSorted) {
            if (aTest.getAnnotation(Ignore.class) == null) {
                filteredTests.add(aTest);
            }
        }
        return filteredTests;
    }

    /*
    TODO: make this algorithm less naive.  Sort each test by run duration as described here: http://blog.tradeshift.com/just-add-servers/
     */
    private static List<Class<?>> getAllTestsSorted(ClassesFinder classesFinder) {
        List<Class<?>> allTests = classesFinder.find();
        Collections.sort(allTests, new Comparator<Class<?>>() {
            @Override
            public int compare(Class<?> o1, Class<?> o2) {
                return o1.getSimpleName().compareTo(o2.getSimpleName());
            }
        });
        return allTests;
    }

    private static List<Class<?>> getMyTests(List<Class<?>> allTests, int nodeNumber, int totalNodes) {
        List<Class<?>> myTests = new ArrayList<Class<?>>();

        for (int i = 0; i < allTests.size(); i++) {
            Class<?> thisTest = allTests.get(i);
            if (i % totalNodes == nodeNumber) {
                myTests.add(thisTest);
            }
        }

        return myTests;
    }
}

ClasspathFinderFactory用于查找与模式匹配的所有测试类.*IntegrationTest

我做了 N 个作业,它们都运行它Runner,但它们都使用不同的node.number系统属性值,因此每个作业运行一组不同的测试。这是故障保护插件的外观:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12.4</version>
            <executions>
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/DistributedIntegrationTestRunner.java</include>
                </includes>
                <skipITs>${skipITs}</skipITs>
            </configuration>
        </plugin>

ClasspathFinderFactory来自_

        <dependency>
            <groupId>cpsuite</groupId>
            <artifactId>cpsuite</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

我认为应该有一些 Jenkins 插件来解决这个问题,但我一直找不到。接近的东西是Parallel Test Executor,但我认为这与我需要的不同。看起来它在单个作业/服务器而不是多个服务器上运行所有测试。它没有提供一种明显的方式来表示“在这里运行这些测试,在那里运行这些测试”。

于 2013-06-28T23:38:27.403 回答
1

我相信您现在已经找到了解决方案,但是我将为打开此页面并提出相同问题的其他人留下一条路径:
并行测试执行器插件:
“此插件添加了一个新构建器,可让您轻松执行定义的测试在一个单独的并行作业中。这是通过让 Jenkins 查看上次运行的测试执行时间,将测试分成大小大致相等的多个单元,然后并行执行它们来实现的。
https://wiki.jenkins-ci.org/display/JENKINS/Parallel+Test+Executor+Plugin

于 2015-05-06T12:48:47.477 回答
0

是的,Parallel Test Executor 是一个很酷的插件,如果你有 2 个从属或一个有 8 个执行器的从属,因为这个插件基于“测试拆分”,所以例如:你将你的 junit 测试分成 4 个不同的数组,这些数组将在你指定的那个奴隶上有4个不同的执行者。我希望你明白了:D,这取决于你想要运行并行测试的从属服务器上的执行程序数量,或者你应该将拆分测试计数从 4 个减少到 2 个。

于 2017-10-30T13:09:54.647 回答