以下是我的JsonListner
,我已經從junit RunListner
擴展。 我使用它得到的試運行如何用junit4創建測試描述runListner
package org.junit.runner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
class JsonListener extends RunListener {
private JSONObject _tests_passed = new JSONObject();
private JSONObject _tests_started = new JSONObject();
private JSONObject _tests_finished = new JSONObject();
private JSONObject _tests_failures = new JSONObject();
private JSONObject _tests_ignored = new JSONObject();
public void testRunStarted(Description description) throws Exception {
}
public void testRunFinished(Result result) throws Exception {
System.out.println(_tests_passed);
}
public void testStarted(Description description) throws Exception {
String key = description.getDisplayName();
long value = System.currentTimeMillis();
_tests_started.put(key, value);
}
public void testFinished(Description description) throws Exception {
// trying to prit the description of the test running
System.out.println(palindromeStringTest.desc);
}
public void testFailure(Failure failure) throws Exception {
String key = failure.getDescription().getDisplayName();
long value = System.currentTimeMillis();
_tests_failures.put(key, value);
}
public void testAssumptionFailure(Failure failure) {
String key = failure.getDescription().getDisplayName();
long value = System.currentTimeMillis();
_tests_failures.put(key, value);
}
public void testIgnored(Description description) throws Exception {
String key = description.getMethodName();
long value = System.currentTimeMillis();
_tests_ignored.put(key, value);
}
}
這是我經過澆道運行測試類的一個後一個JSON報告。
import java.io.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class palindromeStringTest {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
static String desc;
@Before
public void setUpStream() {
System.setOut(new PrintStream(outContent));
}
@After
public void cleanUpStream() {
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
}
@Test
public void revTest() {
palindromeStringTest.desc = "this should reverse the string";
String a = "Madam";
palindromeString obj = new palindromeString();
String b = obj.rev(a);
assertEquals("madaM", b);
}
}
我的問題是如何獲得在runListner的testFinished
方法this.desc
價值? 這是一個替代方案,我想在每次測試運行完成後使用它來獲取描述字段值。 通過上述實施,我不能編譯亞軍
Buildfile: /home/bonnie/WorkStation/JUnit-Json-Runner/build.xml
clean:
[delete] Deleting directory /home/bonnie/WorkStation/JUnit-Json-Runner/build
[delete] Deleting directory /home/bonnie/WorkStation/JUnit-Json-Runner/dist
mkdir:
[mkdir] Created dir: /home/bonnie/WorkStation/JUnit-Json-Runner/build/classes
[mkdir] Created dir: /home/bonnie/WorkStation/JUnit-Json-Runner/dist
compile:
[javac] Compiling 2 source files to /home/bonnie/WorkStation/JUnit-Json-Runner/build/classes
[javac] /home/bonnie/WorkStation/JUnit-Json-Runner/src/JsonListner.java:39: error: cannot find symbol
[javac] System.out.println(palindromeStringTest.desc);
[javac] ^
[javac] symbol: variable palindromeStringTest
[javac] location: class JsonListener
[javac] Note: /home/bonnie/WorkStation/JUnit-Json-Runner/src/JsonListner.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
BUILD FAILED
/home/bonnie/WorkStation/JUnit-Json-Runner/build.xml:22: Compile failed; see the compiler error output for details.
Total time: 1 second
什麼方式,這可以acomplished?有沒有一種方法可以在跑步者身上創建並獲得測試描述?