- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
所以我有一个套件,像这样:
@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class, TestClass3.class})
public class TestSuite {
static List<ExtentTest> extentTestList = new ArrayList<>();
@ClassRule
public static ExtentWatcher extentWatcher = new ExtentWatcher() {
@Override
protected void starting(Description description) {
extentTestList.addAll(extentWatcher.getTests());
}
@Override
protected void finished(Description description) {
extentWatcher.flushReports(extentTestList);
}
};
}
上面的代码有效,但问题是它导致我的 Watcher 报告套件的结果,而不是单个测试的结果。此外,如果测试失败,套件仍会报告为已通过。我的观察者是这样的:
public class ExtentWatcher extends TestWatcher {
// A little set up to give the report a date in the file name
private DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
private Date date = new Date();
private String fileDate = dateFormat.format(date);
private String reportName = "./Test_Report_" + fileDate + ".html";
public ExtentReports extent;
ArrayList<ExtentTest> testList = new ArrayList<>();
public ExtentWatcher() {
extent = createReport();
}
// If test passed, watcher will record this with Extent Reports
@Override
protected void succeeded(Description description) {
ExtentTest test = extent.startTest(description.getDisplayName());
test.log(LogStatus.PASS, "Test Run Successful");
testList.add(test);
}
// Likewise in case of failure
@Override
protected void failed(Throwable e, Description description) {
ExtentTest test = extent.startTest(description.getDisplayName());
test.log(LogStatus.FAIL, "Test Failure: " + e.toString());
testList.add(test);
}
/**
* These methods do the legwork - this file is based off of an example I found @ www.kieftsoft.nl/?p=81
* Eventually we can add some screenshot logic here, but just a clean test report and version info will do
*/
private ExtentReports createReport() {
// Create the report - Extent just needs a little config
ExtentReports extent = new ExtentReports(reportName, false);
extent.config().reportName("Test Report: " + fileDate);
return extent;
}
public void flushReports(List<ExtentTest> testList) {
// This ends the test and then sends (flushes) everything to the html document
for(ExtentTest test : testList) extent.endTest(test);
extent.flush();
}
public List<ExtentTest> getTests() {
return testList;
}
}
这段代码很好地注释为单个测试的@Rule(每个测试都有一个单独的报告,不可取),但如上所述,这在套件级别上不起作用,我真的不确定如何让它工作。我想我可以收集所有测试的列表,并在套件中结束测试并刷新它们,这将允许 ExtentReport 给我一份所有测试的报告。但是,我无法具体获得单独的测试结果 - 我将获得一个测试结果,其中 displayName() = 套件名称。
我如何跟踪单个测试,然后在所有测试完成后刷新它们,并让 ExtentWatcher 在逐个测试的基础上处理通过/失败,而不是只为套件一次?
最佳答案
我使用一个 RunListener 实现,它将结果记录在一个文件中。首先,我有一个为每个测试套件调用的测试运行器类:
public Result run(String suite) {
Class<?> suiteClass = null;
try {
suiteClass = Class.forName(suite);
} catch (ClassNotFoundException ex) {
return null;
}
JUnitCore core = new JUnitCore();
this.testRun = new TestRun(testLogging, suite);
core.addListener(new TestRunListener(testLogging, testRun));
Result result = core.run(suiteClass);
return(result);
}
TestRun 类是一个自定义类,它只计算已开始、通过、失败、忽略的测试的数量。
TestRunListener 实现 org.junit.runner.notification.RunListener,并根据回调(例如 testFailure()、testFinished() 等)跟踪有关测试状态的信息。
@Override
public void testFailure(Failure failure) throws Exception {
classLogger.log(Level.CONFIG, failure.getMessage());
classLogger.log(Level.CONFIG, failure.getTestHeader());
classLogger.log(Level.CONFIG, failure.getTrace());
classLogger.log(Level.CONFIG, failure.getDescription().getDisplayName());
if (failure.getException() != null) {
classLogger.log(Level.CONFIG, failure.getException().getMessage());
}
super.testFailure(failure);
Description description = failure.getDescription();
Test test = processTestResult(TestStatus.FAIL, description);
if (test == null) {
classLogger.log(Level.SEVERE, "TEST IS NULL:" + description.getDisplayName());
return;
}
classLogger.log(Level.CONFIG, failure.getMessage()+ " " + description.getDisplayName());
test.setFailureMessage(description.getDisplayName());
test.setFailureException(failure.getException());
LogRecord record = new LogRecord(Level.CONFIG, failure.getMessage());
record.setParameters(new Object[] { test, failure });
testFailuresLogger.log(record);
}
关于java - 一名测试观察员报告 JUnit Suite 中各个测试的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000531/
本着this question from JUnit 3 to JUnit 4的精神, 是否有任何正则表达式列表到 有效地从 junit 4 API 迁移到 junit 5 API ,不管代码大小?
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
当尝试在JUNITTEST的内存数据库中使用derby时,出现以下异常。 java.sql.SQLNonTransientConnectionException: Database 'memory:t
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to run Junit testcases from command line? 如何在 Linu
可以将 Junitperf 与 junit4 一起使用吗?我有一个带有多个测试的简单 Junit4 测试类,我想对该类的单个测试进行 TimedTest。我该怎么做? 更清楚地说,我的 Junit4
我想将 JUnit 4 测试添加到使用 JUnit 3 测试套件(“AllTests”)来组织测试的遗留项目中。 测试已经用 JUnit 4 执行了,所以我知道 JUnit 4 测试在原则上是有效的。
我正在将我的代码库从 junit4 迁移到 junit5。我在我的测试用例中使用了 mockito。下面是我用于依赖项的不同版本。 5.2.0 1.2.0 1.10.19 or
我刚刚使用 qunit-reporter-junit 生成了以下 XML: 但是当我运行它时,我在以下位置找到了 xsd:http
我已经编写了一个自定义 JUnit 运行器,我希望它成为 eclipse 插件的一部分,该插件将使用该运行器启动测试,而无需将 @RunWith 注释应用于该类。我已经设法使用 org.eclipse
我发现我的Sonar实例5.1或5.1.1(带有最新的声纳运行器2.x)停止在项目的仪表板上显示部分单元测试信息(单元测试小部件)。 我拥有的属性是(在Gradle的sonarRunner> sona
我有一个 JUnit 测试。但是当我使用“Run as -> JUnit”时它会成功,而当我使用“Cover as -> JUnit”时它会失败。这是为什么?代码确实有问题。在代码中,我使用了一些遗留
这个问题在这里已经有了答案: How to test code dependent on environment variables using JUnit? (20 个答案) 关闭 8 年前。 我
当我们的临时服务器因每周维护而停机时,我们有许多集成测试失败。当临时服务器关闭时,我们会发送一个特定的响应,我可以在集成测试中检测到该响应。当我得到这个响应而不是测试失败时,我想知道是否可以跳过/忽略
我需要测试一个程序,它首先预处理一些数据,然后使用这些预处理过的数据计算几个不同的结果——为每个计算编写单独的测试是有意义的。 官方 JUnit 政策似乎是我应该在每次计算测试之前运行预处理。 我如何
JUnit 是否可以为每个测试方法添加描述文本,以便描述文本稍后出现在surefire/failsave xml 报告中!? 背景:我在受监管的环境中工作,必须编写大量文档、测试规范和测试报告。 JU
当 JUnit 中的断言失败时,我想做一些“自己的事情”。我想要这个: public class MyAssert extends org.junit.Assert { // @Overrid
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我想将参数从运行配置传递给我的 JUnit 测试。我如何到达 JUnits 的主要方法来访问这些参数?有谁知道如何做到这一点? 谢谢 最佳答案 您可以使用 -D 系统属性运行单元测试,并使用 Syst
我是一名优秀的程序员,十分优秀!