でテスト記述を作成する方法は、私が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することができますか?ランナーにテスト記述を作成して取得するための既存の方法がありますか?