Travis CI上JUnit测试用例的InitializationError

时间:2016-09-23 10:24:44

标签: java selenium junit travis-ci

我试图找出Travis CI为Selenium项目运行我的JUnit测试。当我从我的机器上运行它时,单元测试工作(在IntelliJ IDEA中)。

由于测试类似乎没有错过任何内容,我想知道为什么测试在我的机器上运行得非常好,但在Travis CI上失败了。我的设置有问题吗?

.travis.yml文件:

language: java

jdk:
  - oraclejdk8

script: ant test


#before_install:
# - cd java

构建文件:

<?xml version="1.0" encoding="UTF-8"?>
<project name="bromine" default="test">
    <property name="dir.src" value="src"/>
    <property name="dir.test.src" value="tests"/>
    <property name="dir.build" value="build"/>
    <property name="dir.build.test" value="build/test"/>
    <property name="dir.classes" value="${dir.build}/classes"/>
    <property name="dir.jar" value="${dir.build}/jar"/>

    <path id="classpath.base">
        <pathelement location="lib/selenium-server-standalone-3.0.0-beta2.jar"/>
    </path>

    <path id="classpath.test">
        <pathelement location="lib/junit-4.12.jar"/>
        <pathelement location="lib/hamcrest-core-1.3.jar"/>
        <pathelement location="${dir.build}"/>
        <path refid="classpath.base"/>
    </path>

    <target name="test" depends="run, clean"/>

    <target name="compile">
        <mkdir dir="${dir.build}"/>
        <javac srcdir="${dir.src}" destdir="${dir.build}" includeantruntime="false">
            <classpath refid="classpath.base"/>
        </javac>
    </target>

    <target name="build" depends="compile">
        <mkdir dir="${dir.build.test}"/>
        <javac srcdir="${dir.test.src}" destdir="${dir.build.test}" includeantruntime="false">
            <classpath refid="classpath.test"/>
        </javac>
        <echo message="Build done"/>
    </target>

    <!-- Test and build all files  -->
    <!-- To run this: use "ant" (default) or "ant run" -->
    <target name="run" depends="build">
        <junit printsummary="on" haltonfailure="yes">
            <classpath>
                <path refid="classpath.test"/>
                <pathelement location="${dir.build.test}"/>
            </classpath>
            <formatter type="brief" usefile="false"/>
            <batchtest>
                <fileset dir="${dir.test.src}" includes="**/*Test*.java"/>
            </batchtest>
        </junit>
    </target>

    <!-- delete all class files -->
    <!-- To run this: use "ant clean" -->
    <target name="clean">
        <delete>
            <fileset dir="${basedir}" includes="**/*.class"/>
        </delete>
        <echo message="Clean done"/>
    </target>
</project>

我从Travis CI得到的失败的构建输出:

$ jdk_switcher use oraclejdk8
Switching to Oracle JDK8 (java-8-oracle), JAVA_HOME will be set to /usr/lib/jvm/java-8-oracle
$ java -Xmx32m -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
$ javac -J-Xmx32m -version
javac 1.8.0_31
2.33s$ ant test
Buildfile: /home/travis/build/Thibstars/Bromine/build.xml
compile:
    [mkdir] Created dir: /home/travis/build/Thibstars/Bromine/build
    [javac] Compiling 21 source files to /home/travis/build/Thibstars/Bromine/build
build:
    [mkdir] Created dir: /home/travis/build/Thibstars/Bromine/build/test
    [javac] Compiling 2 source files to /home/travis/build/Thibstars/Bromine/build/test
     [echo] Build done
run:
    [junit] Running NavigatorTests
    [junit] Testsuite: NavigatorTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.028 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.028 sec
    [junit] 
    [junit] Testcase: initializationError(NavigatorTests):  Caused an ERROR
    [junit] No runnable methods
    [junit] java.lang.Exception: No runnable methods
    [junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    [junit] 
    [junit] 
BUILD FAILED
/home/travis/build/Thibstars/Bromine/build.xml:41: Test NavigatorTests failed
Total time: 2 seconds
The command "ant test" exited with 1.
Done. Your build exited with 1.

失败的测试:

import commands.InitTestFrameworkCommand;
import navigation.Navigator;
import navigation.NavigatorFactory;
import org.junit.Rule;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import rules.ScreenShotOnFailure;
import sut.Environment;

import java.net.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.assertTrue;

public class NavigatorTests {

    @Rule
    public ScreenShotOnFailure failure = new ScreenShotOnFailure();

    @BeforeClass
    public static void init() {
        new InitTestFrameworkCommand().execute();
        NavigatorFactory.createNavigator(new Environment("Google", "https://google.com"));
    }

    @Test
    public void shouldOpenGoogle() {
        try {
            Navigator.getInstance().navigateTo(new URL("https://google.com"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        assertTrue(Navigator.getInstance().getUrl().startsWith("https://www.google."));
    }

    @AfterClass
    public static void tearDown() {
        NavigatorFactory.destroyNavigator();
    }
}

1 个答案:

答案 0 :(得分:0)

似乎我把TestNg与JUnit混淆了。看看我错误使用的进口产品:

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;