【问题标题】:Junit Tests failing when running it through jenkins通过詹金斯运行时,Junit 测试失败
【发布时间】:2014-11-24 08:17:24
【问题描述】:

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

【问题讨论】:

  • 你在运行它们如何
  • 在 jenkins 服务器上使用 gradle build 命令。
  • 当你有一个扩展 TestCase 的类时,我通常认为我们在谈论 JUnit 3,但在这里你有“扩展 TestCase”和用 @Test 注释的方法,这意味着 JUnit 4。我不会尝试在同一个班级中混合使用 JUnit 3 和 4。当您说测试在本地运行时,您的意思是您是在 IDE 中本地运行测试,还是使用 Gradle 在本地运行测试?很明显,您在构建服务器上看到了一个错误,我假设它正在使用 Gradle。
  • 即使我在 gradle 本地运行但在 IDE 中没有运行它,我也会看到错误。我确实通过删除 SCSystemTestCase vut 中没有解决问题的 @Test 注释在 gradle 中运行了测试。

标签: java eclipse junit jenkins


【解决方案1】:

您混合使用 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 包或其子包之一的任何类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2018-05-31
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多