簡體   English   中英

如何使用junit4 runListner創建測試描述

[英]how to create a test description with junit4 runListner

以下是我的JsonListner我從JUnit的擴展RunListner 我正在使用它在測試運行后得到一個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);
  }

}

這是我正在跑步者中進行的測試課程之一。

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);
  }
}

我的問題是如何在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

有什么方法可以實現? 是否已經有一種方法可以在運行器中創建和獲取測試說明?

您在方法中遇到編譯錯誤

  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類中創建方法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類訪問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());   
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM