【发布时间】: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