自动化Eclipse Plug-in Unit测试

时间:2011-03-10 21:08:07

标签: eclipse unit-testing testing plugins

我正在尝试通过此处描述的方法自动化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类型/ p>

这究竟是什么意思,如何解决?

作为参考,这里是我正在尝试编译的一个类的代码(取自上面链接中的示例代码):

提前致谢。


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) {
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题是提供ITestRunListener2接口的插件不会导出相应的包。对于解决方案,您可能希望查看文章中链接的相应bug 140503中提供的修补程序,其中包含标题为PDETestListener / Collector for 3.5 + 3.6的附件。这可能就是运行测试用例所需要的。

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

答案 1 :(得分:0)

我必须添加一些依赖项才能使该方法适用于Eclipse Juno(4.4)。

这三个是我为解决ITestRunListener2ISafeRunnableISchedulingRule而添加的内容,以及OP推荐的内容。

  • org.eclipse.jdt.junit.core_*.jar
  • org.eclipse.equinox.common_*.jar
  • org.eclipse.core.jobs_*.jar
  • org.eclipse.core.runtime_*.jar