如何使用 junit4 runListner 创建测试描述
how to create a test description with junit4 runListner
以下是我从 junit RunListner
扩展而来的 JsonListner
。
我正在使用它得到 json 测试后的报告 运行
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);
}
}
这是 class 我正在 运行 宁通过 运行ner 的测试之一。
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);
}
}
我的问题是如何在 运行Listner 的 testFinished
方法中获取 this.desc
值?
这是我考虑在每次测试 运行 完成后用于获取描述字段值的替代方法。
通过上述实现,我无法编译 运行ner
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
有什么方法可以实现?在 运行ner 中是否已经存在创建和获取测试描述的方法?
您在方法
中遇到编译错误
public void testFinished(Description description) throws Exception {
// trying to prit the description of the test running
System.out.println(palindromeStringTest.desc);
}
因为 palindromeStringTest
变量未定义。
然后要调用您的侦听器,您需要运行通过 JUnitCore 进行测试。
public void main(String... args) {
JUnitCore core= new JUnitCore();
core.addListener(new JsonListener ());
core.run(MyTestClass.class);
}
您必须在 JsonListner
中使用 java.lang.reflect.*
才能访问描述变量。
在 palindromeStringTest
class 和 return 描述中创建方法 getDescrition
。
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;
//public method to get the value of desc
public function getDescription(){
return 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);
}
}
现在您可以在 java reflection
class
的帮助下访问 testFinished
方法中的 description
public void testFinished(Description description) throws Exception {
Class<?> testClass = description.getTestClass();
Method m = testClass.getDeclaredMethod("getDescription");
Object o = m.invoke(null);
System.out.println(o.toString());
}
以下是我从 junit RunListner
扩展而来的 JsonListner
。
我正在使用它得到 json 测试后的报告 运行
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);
}
}
这是 class 我正在 运行 宁通过 运行ner 的测试之一。
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);
}
}
我的问题是如何在 运行Listner 的 testFinished
方法中获取 this.desc
值?
这是我考虑在每次测试 运行 完成后用于获取描述字段值的替代方法。
通过上述实现,我无法编译 运行ner
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
有什么方法可以实现?在 运行ner 中是否已经存在创建和获取测试描述的方法?
您在方法
中遇到编译错误 public void testFinished(Description description) throws Exception {
// trying to prit the description of the test running
System.out.println(palindromeStringTest.desc);
}
因为 palindromeStringTest
变量未定义。
然后要调用您的侦听器,您需要运行通过 JUnitCore 进行测试。
public void main(String... args) {
JUnitCore core= new JUnitCore();
core.addListener(new JsonListener ());
core.run(MyTestClass.class);
}
您必须在 JsonListner
中使用 java.lang.reflect.*
才能访问描述变量。
在 palindromeStringTest
class 和 return 描述中创建方法 getDescrition
。
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;
//public method to get the value of desc
public function getDescription(){
return 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);
}
}
现在您可以在 java reflection
class
testFinished
方法中的 description
public void testFinished(Description description) throws Exception {
Class<?> testClass = description.getTestClass();
Method m = testClass.getDeclaredMethod("getDescription");
Object o = m.invoke(null);
System.out.println(o.toString());
}