2016-09-23 85 views
0

我想弄清楚Travis CI爲Selenium項目運行我的JUnit測試。當我從我的機器上運行它們(在IntelliJ IDEA中)時,該單元測試工作。Travis CI上的JUnit測試用例初始化錯誤

由於測試類似乎沒有錯過任何東西,我想知道爲什麼測試在我的機器上運行得非常好,但在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> 

發生故障的建立輸出I特拉維斯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(); 
    } 
} 

回答

0

看來我把TestNg和JUnit搞混了。看看我誤用的進口產品:

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