我有一个自定义测试运行器,我已经完成了部分测试,所以我可以在不同的jenkins节点上分发测试。如果我的所有集成测试都运行了,则需要一个小时。所以我有3台服务器运行1/3的测试,总共需要20分钟。这是我套房的样子:
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;
}
}
这种工作,除非我尝试在不同的模块中使用多个DistibutedIntegrationTestRunners并一次运行它们,&#34;事情不起作用&#34;。通常我不会在SO上发布含糊不清的投诉,但很难弄清楚因为这段代码没有给出太多反馈。这是我得到的唯一记录:
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());
这在运行任何测试之前发生。如果我能获得比这更多的日志记录将非常有用。例如,如果我可以打印出来,那就非常有用了。我将要运行FooTest&#34;在该测试运行之前正确。有没有办法做到这一点?
我已经浏览了源代码并看到private final RunNotifier fNotifier;
中有一个名为org.junit.internal.runners.JUnit38ClassRunner
的字段,但我不确定如何挂钩或者如果我朝着正确的方向前进
我使用的是JUnit 4.10,但如果需要我可以升级。
答案 0 :(得分:0)
对我来说,这看起来像是一个XY问题。
日志文件不用于测试。它们用于监视调试。一旦你有一个失败的测试用例,你需要从思考测试转向思考调试。这意味着可能使用调试器单独运行失败的测试用例。
如果您的测试失败并显示无用的失败消息,则表明您的低级别测试覆盖率较差,或者测试用例的诊断消息较差。