gpt4 book ai didi

eclipse - 自动化 Eclipse 插件单元测试

转载 作者:行者123 更新时间:2023-11-28 21:34:24 26 4
gpt4 key购买 nike

我正在尝试通过此处描述的方法自动执行 Eclipse RCP 应用程序的插件单元测试:http://www.eclipse.org/articles/article.php?file=Article-PDEJUnitAntAutomation/index.html请注意,我正在运行 Eclipse 3.6

但是,我遇到如下编译错误:

package org.eclipse.jdt.internal.junit.model does not exist
[javac] import org.eclipse.jdt.internal.junit.model.ITestRunListener2;

cannot find symbol
[javac] symbol: class ITestRunListener2
[javac] public class PDETestListener implements ITestRunListener2 {
[javac] ^

似乎与Eclipse中显示的以下警告有关:

不鼓励访问:由于所需库 C:\eclipse\plugins\org.eclipse.jdt.junit.core_3.6.1.r361_v20100825-0800.jar 的限制,类型 ITestRunListener2 不可访问

这到底是什么意思,我该如何解决?

作为引用,这里是我要编译的其中一个类的代码(取自上面链接中的示例代码):

提前致谢。


package pde.test.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import junit.framework.TestResult;

import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
import org.eclipse.jdt.internal.junit.model.ITestRunListener2;

public class PDETestListener implements ITestRunListener2 {
private Object resultsCollector;
private int totalNumberOfTests;
private int testsRunCount;
private int numberOfTestsPassed;
private int numberOfTestsFailed;
private int numberOfTestsWithError;
private boolean testRunEnded = false;
private XMLJUnitResultFormatter xmlResultsFormatter;
private File outputFile;
private String suiteName;
private JUnitTest junitTestSuite;
private TestCase currentTest;

public PDETestListener(Object collector, String suite) {
resultsCollector = collector;
suiteName = suite;
junitTestSuite = new JUnitTest(suiteName);
junitTestSuite.setProperties(System.getProperties());
}

public void setOutputFile(String filename) {
outputFile = new File(filename);
}

public File getOutputFile() {
if (outputFile == null) {
setOutputFile("TEST-" + suiteName + ".xml");
}
return outputFile;
}

public boolean failed() {
return ((numberOfTestsFailed + numberOfTestsWithError) > 0) || (testRunEnded && (testsRunCount == 0));
}

public int count() {
return testsRunCount;
}

private XMLJUnitResultFormatter getXMLJUnitResultFormatter() {
if (xmlResultsFormatter == null) {
xmlResultsFormatter = new XMLJUnitResultFormatter();
try {
xmlResultsFormatter.setOutput(new FileOutputStream(getOutputFile()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return xmlResultsFormatter;
}

public synchronized void testRunStarted(int testCount) {
totalNumberOfTests = testCount;
testsRunCount = 0;
numberOfTestsPassed = 0;
numberOfTestsFailed = 0;
numberOfTestsWithError = 0;
testRunEnded = false;
getXMLJUnitResultFormatter().startTestSuite(junitTestSuite);
System.out.println("PDE Test Run Started - running " + totalNumberOfTests + " tests ...");
}

public synchronized void testRunEnded(long elapsedTime) {
testRunEnded = true;
junitTestSuite.setCounts(testsRunCount, numberOfTestsFailed, numberOfTestsWithError);
junitTestSuite.setRunTime(elapsedTime);
getXMLJUnitResultFormatter().endTestSuite(junitTestSuite);
System.out.println("Test Run Ended - " + (failed() ? "FAILED" : "PASSED") + " - Total: " + totalNumberOfTests
+ " (Errors: " + numberOfTestsWithError
+ ", Failed: " + numberOfTestsFailed
+ ", Passed: " + numberOfTestsPassed + "), duration " + elapsedTime + "ms.");

synchronized (resultsCollector) {
resultsCollector.notifyAll();
}
}

public synchronized void testRunStopped(long elapsedTime) {
System.out.println("Test Run Stopped");
testRunEnded(elapsedTime);
}

public synchronized void testRunTerminated() {
System.out.println("Test Run Terminated");
testRunEnded(0);
}

public synchronized void testStarted(String testId, String testName) {
testsRunCount++;
currentTest = new WrapperTestCase(testName);
getXMLJUnitResultFormatter().startTest(currentTest);
System.out.println(" Test Started - " + count() + " - " + testName);
}

public synchronized void testEnded(String testId, String testName) {
numberOfTestsPassed = count() - (numberOfTestsFailed + numberOfTestsWithError);
getXMLJUnitResultFormatter().endTest(currentTest);
System.out.println(" Test Ended - " + count() + " - " + testName);
}

public synchronized void testFailed(int status, String testId, String testName, String trace, String expected, String actual) {
String statusMessage = String.valueOf(status);
if (status == ITestRunListener2.STATUS_OK) {
numberOfTestsPassed++;
statusMessage = "OK";
} else if (status == ITestRunListener2.STATUS_FAILURE) {
numberOfTestsFailed++;
statusMessage = "FAILED";
getXMLJUnitResultFormatter().addFailure(currentTest, new AssertionFailedError(trace));
} else if (status == ITestRunListener2.STATUS_ERROR) {
numberOfTestsWithError++;
statusMessage = "ERROR";
getXMLJUnitResultFormatter().addError(currentTest, new Exception(trace));
}
System.out.println(" Test Failed - " + count() + " - " + testName + " - status: " + statusMessage
+ ", trace: " + trace + ", expected: " + expected + ", actual: " + actual);
}

public synchronized void testReran(String testId, String testClass, String testName, int status, String trace, String expected, String actual) {
String statusMessage = String.valueOf(status);
if (status == ITestRunListener2.STATUS_OK) {
statusMessage = "OK";
} else if (status == ITestRunListener2.STATUS_FAILURE) {
statusMessage = "FAILED";
} else if (status == ITestRunListener2.STATUS_ERROR) {
statusMessage = "ERROR";
}

System.out.println(" Test ReRan - " + testName + " - test class: " + testClass + ", status: " + statusMessage
+ ", trace: " + trace + ", expected: " + expected + ", actual: " + actual);
}

public synchronized void testTreeEntry(String description) {
System.out.println("Test Tree Entry - Description: " + description);
}

class WrapperTestCase extends TestCase {

public WrapperTestCase(String name) {
super(name);
}

public int countTestCases() {
return 1;
}

public void run(TestResult result) {
}
}
}

最佳答案

问题是提供ITestRunListener2接口(interface)的插件没有导出对应的包。对于解决方案,您可能需要查看相应的 bug 140503 中提供的补丁。链接在文章中,其中包含标题为 PDETestListener/Collector for 3.5 + 3.6 的附件。这可能是您运行测试用例所需要的。

PS.:我不确定这是否是个好主意,但我相信,对于测试(和构建)自动化,使用 Buckminster 或 Maven/Tycho 是个好主意。两者都是 Eclipse 项目,并提供了一种处理测试自动化的好方法 - 与使用 Ant 脚本相比,我更喜欢它们。

关于eclipse - 自动化 Eclipse 插件单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5265932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com