gpt4 book ai didi

java - 通过 jenkins 运行 Junit 测试时失败

转载 作者:行者123 更新时间:2023-11-30 03:44:22 24 4
gpt4 key购买 nike

当我在 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吗?如果是,我应该使用哪个版本?

最佳答案

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

关于java - 通过 jenkins 运行 Junit 测试时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26108360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com