简体   繁体   English

无法让Ant运行我的JUnit测试

[英]Can't get Ant to run my JUnit test

I have created an Ant target to compile my code. 我已经创建了一个Ant目标来编译我的代码。

<target name="test" depends="compile">
    <junit>
        <classpath>
            <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
        </classpath>
        <classpath>
            <pathelement path="${build}"/>
        </classpath>
        <test name="BinarySearchTest" />
    </junit>
</target>

<target name="compile">
    <mkdir dir="${build}"/>
    <javac
        srcdir="${basedir}\src;${basedir}\..\SwaCore\src;${basedir}\..\SwaShared\src;${basedir}\..\SwaDictionary\src;${basedir}\..\SwaSuggestion\src;${basedir}\..\SwaServerSharedCore\src;${basedir}\..\SwaPrediction\src;"
        destdir="${build}"
        debug="true"
        debuglevel="lines,vars,source">
        <classpath>
            <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
            <pathelement path="${basedir}\..\SwaSuggestion\lib\ssce.jar"/>
            <pathelement path="C:\Java\javamail-1.4.1\mail.jar"/>
            <pathelement path="C:\Java\commons-net-2.0\commons-net-ftp-2.0.jar"/>
            <pathelement path="${basedir}\WebContent\WEB-INF\lib\gson-2.2.1.jar"/>
            <pathelement path="${tomcatLibs}\servlet-api.jar"/>
        </classpath>
    </javac>
</target>

The compile works fine, all my classes are in the folder pointed to by ${build}. 编译工作正常,我所有的类都在$ {build}指向的文件夹中。 I assume this ${build} will be the classpath for my JUnit test case. 我假设这个$ {build}将是我的JUnit测试用例的类路径。 I then have a test target that I want to run a JUnit test Called BinarySearchTest. 然后,我有一个要运行的JUnit测试目标叫BinarySearchTest。 When I run this I get the simple error 当我运行它时,我得到一个简单的错误

Test BinarySearchTest FAILED

I know my Unit test is ok because it tests nothing. 我知道我的单元测试还可以,因为它什么也不测试。

public class BinarySearchTest extends TestCase
{
    public void test()
    {       
    }
}

Even if I put the following in my test target I still get the same error 即使我将以下内容放入测试目标,我仍然会遇到相同的错误

<test name="AtestThatDoesNotExistTest" />

result = 结果=

Test AtestThatDoesNotExistTest FAILED

I would have expected a classNotFound error so there is obviously something fundamentally wrong with my Ant XML 我本来希望有classNotFound错误,所以我的Ant XML显然存在根本上的错误

JUnit jar is being picked up ok because if it's not in the classpath I will get the error 可以很好地拾取JUnit jar,因为如果不在类路径中,则会收到错误消息

The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

Update I modified my target to include 更新我修改了目标以包括

        <formatter type="brief" usefile="false" />
        <test name="com.swaserver.junit.BinarySearchTest" />

test:
[junit] Testsuite: com.swaserver.junit.BinarySearchTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.878 sec
[junit] ------------- Standard Output ---------------
[junit] Initializing Binary search class
[junit] Binary search class initialized
[junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL Total time: 2 seconds 成功建立总时间:2秒

Now I can see that my test is actually run. 现在,我可以看到我的测试实际上已在运行。 However I get a class not found unless I specify the fully qualified class name for my unit test. 但是,除非我为单元测试指定了完全合格的类名,否则我找不到一个类。 Why would this be? 为什么会这样呢?

You may try 你可以试试

    <classpath>
        <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
        <pathelement path="${build}"/>
    </classpath>

instead of 代替

    <classpath>
        <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
    </classpath>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM