Junit测试在通过jenkins运行时失败

时间:2014-09-29 20:32:02

标签: java eclipse junit jenkins

当我在Jenkins中运行测试时出现以下错误。它们在Eclipse中运行良好。

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。在任何扩展此测试的测试中都没有@Test注释。我是否需要将gradle文件中的junit版本更改为junit 3?如果是,我应该使用哪个版本?

1 个答案:

答案 0 :(得分:2)

混合JUnit 3和JUnit 4.我认为Eclipse选择以JUnit 4测试运行测试,Gradle决定它是JUnit 3测试。这就是行为不同的原因。

如果您想使用JUnit 3,则必须更改SCSystemTestCase

  • 将方法setup重命名为setUp
  • 删除runTest方法(不得根据其Javadoc覆盖它)
  • 删除@Test注释
  • 将方法triggerTest重命名为testSomething(方法名称以test开头非常重要。)
  • 通常,JUnit 3测试不得导入任何类org.junit包或其子包。

但我强烈建议您使用JUnit 4.为此,您必须更改SCSystemTestCase

  • 删除班级声明的extends TestCase部分
  • 删除构造函数中的super调用
  • 删除runTest方法。
  • 每个测试类(继承自SCSystemTestCase的类)必须具有默认构造函数(不带参数的构造函数)
  • 通常,JUnit 4测试不得导入任何类junit.framework包或其子包。