2014-09-29 213 views
1

我在Jenkins运行测试时遇到以下错误。他们在Eclipse中运行良好。通过jenkins运行Junit测试失败

junit.framework.AssertionFailedError: No tests found in SCSystemTestCase 

SCSystemTestCase是一个扩展了TestCase并被其他Junit测试用来运行测试的类。 SCSystemTestCase的片段如下所示

import java.io.File; 
import java.util.HashMap; 
import java.util.Hashtable; 
import java.util.Vector; 

import junit.framework.TestCase; 
import junit.framework.AssertionFailedError; 

import org.jtestcase.JTestCase; 
import org.jtestcase.JTestCaseException; 
import org.jtestcase.TestCaseInstance; 
import org.junit.Test; 


public class SCSystemTestCase extends TestCase 
{ 

    protected HashMap<String, Vector<String>> params = null; 
    protected String testCaseFName = ""; 
    protected String testGoal = ""; 
    protected String testCaseFFolder = ""; 
    protected String testCaseClass = ""; 
    protected String testCaseLocationprefix = "SC"; 
    protected String testCaseLocation = ""; 
    protected String jerseyEndpoint = ""; 
    protected String requestMethod = ""; 
    protected String requestPath = ""; 
    protected String responseData = ""; 
    protected String description = ""; 
    protected String dataDir = ""; 
    protected String testCaseName; 
    protected String testCaseMethod; 
    protected TestCaseInstance testCaseCur; 
    protected Vector<?> testCases = null; 

    private JTestCase thisJtestCase; 

    public SCSystemTestCase(String s, String t) 
    { 
    super(s); 
    testCaseName = s; 
    testCaseMethod = t; 

    } 

    public SCSystemTestCase(String s) 
    {  
    super(s); 
    } 

    public SCSystemTestCase() 
    { 
    } 

    public void setup() throws Exception 
    { 
    try 
    { 
     testCaseLocation = testCaseFFolder + File.separator + testCaseFName; 
     setThisJtestCase(new JTestCase(testCaseLocation, testCaseClass)); 
    } 
    catch (JTestCaseException jte) 
    { 
     System.out.println(jte.getMessage()); 
    } 
    } 


    @Override 
    protected void runTest() throws Throwable 
    { 
    setup(); 
    triggerTest(); 
    } 

    @Test 
    protected void triggerTest() throws Exception 
    { 
    StringBuffer errorBucket = new StringBuffer(); 
    Hashtable<?,?> globalParams = null; 
    // Hashtable<String, String> globalParams = null; 
    try 
    { 
     testCases = getThisJtestCase().getTestCasesInstancesInMethod(testCaseMethod); 
     globalParams = getThisJtestCase().getGlobalParams(); 
     jerseyEndpoint = (String) globalParams.get("jerseyEndpoint"); 
     requestMethod = (String) globalParams.get("requestMethod"); 
     requestPath =  (String) globalParams.get("requestPath"); 
     dataDir =   (String) globalParams.get("dataDir"); 

     for (int i = 0; i < testCases.size(); i++) 
     { 

     testCaseCur = (TestCaseInstance) testCases.elementAt(i); 
     if (testCaseCur.getTestCaseName().equals(testCaseName)) 
     { 
      System.out.println("Starting test: " + testCaseCur.getTestCaseName()); 
      System.out.println("======================================================"); 
      params = testCaseCur.getTestCaseParams(); 
      testGoal = (String) ((params.get("testGoal") != null) ? params.get("testGoal") : " Not Specified "); 
      System.out.println("TEST-GOAL: " + testGoal); 
      boolean isNegative = (Boolean) ((params.get("isNegative") != null) ? params.get("isNegative") : false); 

      try 
      { 
      XMLDataParser test = new XMLDataParser(); 
      test.testExecuteTestSteps(params); 
      } 
      catch (Throwable t) 
      { 
      t.printStackTrace(); 
      if (!isNegative) 
      { 
       System.out.println("It is NOT a negative test, why did it throw an Exception!"); 
       errorBucket.append("\n-----" + testCaseMethod + "." + testCaseCur.getTestCaseName() + "-----"); 
       errorBucket.append("\nIt is NOT a negative test, why did it throw an Exception!\n"); 
      } 
      else 
      { 
       System.out.println("It is a negative test!"); 
      } 
      } 
      finally 
      { 
      System.out.println("--Ending test: " + testCaseCur.getTestCaseName() + "--"); 
      } 
     } 
     } 

    } 
    catch (Throwable t) 
    { 
     t.printStackTrace(); 
    } 
    finally 
    { 
     System.out.println("======================================================"); 
    } 

    if (errorBucket.length() > 0) 
    { 
     throw new AssertionFailedError(errorBucket.toString()); 
    } 

    } 

// protected abstract void testExecuteTestSteps() throws Exception, Throwable; 


    public JTestCase getThisJtestCase() 
    { 
    return thisJtestCase; 
    } 

    public void setThisJtestCase(JTestCase thisJtestCase) 
    { 
    this.thisJtestCase = thisJtestCase; 
    } 

请问你能帮我解决吗?这个类没有自己的TestSuite()或测试。 build.gradle文件和eclipse都使用Junit4.10。在任何扩展this的测试中都没有@Test注释。我需要将gradle文件中的junit版本更改为junit 3吗?如果是,我应该使用哪个版本?

+0

您正在运行它们* how *? – chrylis 2014-09-29 20:36:43

+0

在jenkins服务器上使用gradle build命令。 – 2014-09-29 20:37:45

+0

当你有一个扩展TestCase的类时,我通常会提到我们正在讨论JUnit 3,但是在这里你既有“扩展TestCase”又有用@Test注解的方法,这意味着JUnit 4.我不会尝试将JUnit 3和4个同班同学。当你说测试在本地运行时,你的意思是你在IDE中本地运行测试,还是在本地使用Gradle?很明显,你在构建服务器上看到一个错误,我认为这是使用Gradle。 – unigeek 2014-09-29 21:32:31

回答

2

您混合使用JUnit 3和JUnit 4.我认为Eclipse选择将测试作为JUnit 4测试运行,并且Gradle决定它是JUnit 3测试。这是不同行为的原因。

如果你想使用JUnit 3,比你要改变SCSystemTestCase

  • 重命名方法setup作为setUp
  • 删除runTest方法(它不能被重写,根据其的Javadoc)
  • 取出@Test注释
  • 重命名方法triggerTesttestSomething(它的实现方法具重要ds名称以test开头)。
  • 通常情况下,JUnit 3测试不得导入org.junit包或其子包之一的任何类别。

但我强烈建议使用JUnit 4。对于这一点,你必须改变SCSystemTestCase

  • 删除类声明的extends TestCase部分
  • 在构造函数中删除super电话
  • 删除方法runTest
  • 每一个测试类(即从SCSystemTestCase继承的类)必须有一个默认构造函数(无参数的构造函数)
  • 一般JUnit 4测试不能导入任何类junit.framework包或其子包之一。