org.junit.Test 和 junit.framework.Test 包之间的区别

问题描述 投票:0回答:1
<project name="JunitSuite" basedir="." default="clean">

    <property name="${src}" value="./src/JunitSuiteProject" />
    <property name="${build}" value="./build" />
    <property name="package" value="JunitSuiteProject"/>
    <property name= "jar" value="./build/jar"/>

    <target name="clean">
        <delete dir="./build"/>
        <mkdir dir="./build"/>
        <mkdir dir="./build/jar"/>
    </target>

<target name="run">
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
            <classpath location="C:\Program Files\Java\apache-ant-1.9.2\lib\selenium-server-standalone-2.35.0.jar"/>
            <classpath location="E:\Swaroop Don't Touch\Selenium\eclipse-jee-juno-SR2-win32\eclipse\plugins\org.junit_3.8.2.v3_8_2_v20100427-1100\junit.jar"/>
            <classpath location="C:\Program Files\Java\apache-ant-1.9.2\lib\*.jar"/>
            <classpath location="E:\Swaroop Don't Touch\Selenium\eclipse-jee-juno-SR2-win32\eclipse\plugins\"/>
            <formatter type="brief" usefile="false"/>               
        <batchtest fork="yes"> 
            <fileset dir="C:\Program Files\Java\apache-ant-1.9.2\lib" includes="selenium-server-standalone-2.35.0.jar"/>
            <fileset dir="E:\Swaroop Don't Touch\Selenium\eclipse-jee-juno-SR2-win32\eclipse\plugins\org.junit_3.8.2.v3_8_2_v20100427-1100" includes="junit.jar"/>
            <fileset dir="C:\Program Files\Java\apache-ant-1.9.2\lib" includes="*.jar"/>
            <fileset dir="./build" includes="**/AllTests.*"/> 
        </batchtest>
     </junit>
</target>   

    <target name="compile">
        <javac srcdir="./src/JunitSuiteProject" destdir="./build" includeantruntime="true">
                  <classpath location="C:\Program Files\Java\apache-ant-1.9.2\lib\selenium-server-standalone-2.35.0.jar" />
        </javac>
    </target>

    <target name="jar">
        <jar destfile="./build/jar/JunitSuite.jar" basedir="./build">
        </jar>
      </target>

</project>
    **************************************************************

    package JunitSuiteProject;


    import junit.framework.TestSuite;
    import junit.framework.Test;
    import org.junit.runners.Suite;
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite.SuiteClasses;

    @RunWith(Suite.class)
    @SuiteClasses({JunitTest1.class})


    public class AllTests extends TestSuite{

        public static Test Suite(){
        TestSuite g = new TestSuite();
        g.addTest(new JunitTest1("testprintTest"));
        return g;
    }
        public static void main(String[]args){
            junit.textui.TestRunner.run(Suite());
        }
    }

当我在 Junit 中指定了 Jars 的位置时,有人可以告诉我这里的问题是什么吗?为什么当我触发 Ant 文件时会出现 ClassNotFoundException 错误。

The output says: 

[junit] Running AllTests
    [junit] Testsuite: AllTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Null Test:  Caused an ERROR
    [junit] AllTests
    [junit] java.lang.ClassNotFoundException: AllTests

使用的 junit 是 3.8 版本,作为 junit 测试它执行得很好,但从 build.xml 文件启动时出现错误。

selenium webdriver junit4 junit3
1个回答
39
投票

junit.framework.Test
包是使用JUnit v3的旧项目使用的遗留命名空间,它适用于不支持注释的Java版本。

org.junit.Test
包是JUnit v4使用的新命名空间,它的注释需要Java v1.5或更高版本。 如果可能的话,使用 JUnit 4 和最新的 Java,因为新的注释非常优越。

JUnit 4 的当前发行版包含旧命名空间的副本,以方便迁移;但是,您应该避免使用 JUnit3 形式进行新测试,并尽可能将旧测试转换为 JUnit 4。

在 JUnit 4 中,两个包都包含在库 jar 中以用于迁移目的,但您应该使用 org.junit.Test 来生成新代码,并且这些测试不需要扩展 TestCase。

您可以查看Junit4测试用例的基本示例

© www.soinside.com 2019 - 2024. All rights reserved.