java - 从 JUnit 命令行获取测试列表

标签 java unit-testing junit

简而言之,我的问题是我想从命令行运行一些 JUnit 测试,并且我希望能够看到:

  • 所有测试的列表(无论是否运行)。
  • 所有通过的测试的列表。
  • 所有失败测试的列表

第一个项目符号的原因是因为我相信如果一个测试调用 System.exit,其他测试将不会运行,我仍然想了解这些测试,即使它们没有运行。

假设我有这个测试类:

    import junit.framework.TestCase;
    public class ALoadOfTests extends TestCase {

    public void testFoo() {
        int a = 5;
        int b = 10;
        int result = a + b;
        assertEquals(15, result);
    }

    public void testBar() {
        fail();
    }

    public void testBaz() {
        fail();
    }       
}

如果我使用以下命令运行它:

javac -cp ".;C:\Users\username\Desktop\Test\junit-4.11.jar;C:\Users\username\Desktop\Test\hamcrest-core-1.3.jar" ALoadOfTests.java
java -cp ".;C:\Users\username\Desktop\Test\junit-4.11.jar;C:\Users\username\Desktop\Test\hamcrest-core-1.3.jar" junit.textui.TestRunner ALoadOfTsts

输出为:

.F.F.
Time: 0.002
There were 2 failures:
1) testBar(ALoadOfTests)junit.framework.AssertionFailedError
        at ALoadOfTests.testBar(ALoadOfTests.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
2) testBaz(ALoadOfTests)junit.framework.AssertionFailedError
        at ALoadOfTests.testBaz(ALoadOfTests.java:16)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

FAILURES!!!
Tests run: 3,  Failures: 2,  Errors: 0

这里的问题是输出没有告诉我存在并通过的 testFoo 的名称。 此外,如果我将其中之一更改为使用 System.exit,那么我们根本不会得到太多输出,我们只会得到:

.F.F.

理想情况下,为了能够获得我需要的所有信息,如果我可以在运行任何测试(以及 System.exit 的任何机会)之前先打印出测试名称列表,那么我可以看到哪些通过了,哪些通过了失败(或未运行)。

如果我可以获得类似于以下内容的输出:

Tests:
testFoo
TestBar
TestBaz
Results:
TestFoo(passed)
TestBar(failed:reason)

那么我可以放心地假设:

TestFoo passed
TestBar failed
TestBaz never ran, probably because some test called system.exit.

使用自定义运行器可以实现这一点吗?如果是,任何有关从哪里开始的提示将不胜感激。

不幸的是,我无法控制代码,应该假设它可能在每个方法中都包含 System.exit。我对测试也几乎没有控制权,因此在设置方法中打印出测试名称并不理想。

谢谢

最佳答案

更改 JUnit 输出

如本blog中所述您可以编写自己的测试监听器,并在测试通过或失败时打印出自己的消息。

public class ExecutionListener extends RunListener
{
... //feel free to override other methods too

/**
 *  Called when an atomic test has finished, whether the test succeeds or fails.
 * */
public void testFinished(Description description) throws java.lang.Exception
{
    System.out.println("Finished execution of test case : "+ description.getMethodName());
}

/**
 *  Called when an atomic test fails.
 * */
public void testFailure(Failure failure) throws java.lang.Exception
{
    System.out.println("Execution of test case failed : "+ failure.getMessage());
}

/**
 *  Called when a test will not be run, generally because a test method is annotated with Ignore.
 * */
public void testIgnored(Description description) throws java.lang.Exception
{
    System.out.println("Execution of test case ignored : "+ description.getMethodName());
}

}

然后编写您自己的类,并使用 main 来运行 Junit(而不是运行 junit.jar)

public class ExecuteWithRunListener
{
    public static void main(String[] args)
    {
        JUnitCore runner = new JUnitCore();
        //Adding listener here
        runner.addListener(new ExecutionListener());
        runner.run(TestFeatureOne.class, TestFeatureTwo.class);
    }
} 

如何防止来自 System.exit 的调用实际退出

您的测试可以使用自己的SecurityManager来阻止调用System.exit执行任何操作。下面的代码将使任何对 System.exit() 的调用抛出一个特殊的异常,我们可以在测试中捕获该异常,以确保我们调用了 exit 并捕获退出代码是什么:

public static final SecurityManager NON_EXITABLE_MANAGER = new SecurityManager(){

    @Override
    public void checkPermission(Permission perm) {
        //allow everything
    }
    /**
     * Throws a {@link TriedToExitException} instead of exiting.
     * <p/>
     * {@inheritDoc}
     */
    @Override
    public void checkExit(int status) {
        throw new TriedToExitException(status);
    }

};

public static final class TriedToExitException extends SecurityException{
    private static final long serialVersionUID = 1L;
    private final int exitCode;

    public TriedToExitException(int exitCode){
        this.exitCode=exitCode;
    }

    @Override
    public String getMessage() {
        return String.format("tried to System.exit(%d)",exitCode);
    }

    public int getExitCode() {
        return exitCode;
    }

}

然后在您的单元测试中,您将 SecurityManager 替换为我们的停止调用退出的版本(并保存旧的 SecurityManager,以便我们可以在测试后恢复它)

private SecurityManager previousManager=null;


@Before
protected void replaceManager() throws Throwable {
    previousManager = System.getSecurityManager();
    System.setSecurityManager(NON_EXITABLE_MANAGER);
}

@After
protected void restoreManager() {
    System.setSecurityManager(previousManager);
}

 @Test
    public void testCallToExit() throws IOException{

         try{
           //something that calls System.exit
            fail("should call exit");
         }catch(TriedToExitException e){
             //make sure we get correct exit code
             assertEquals(-1, e.getExitCode());

         }
    }

关于java - 从 JUnit 命令行获取测试列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27038351/

相关文章:

java - 如何通过 JPA 注释将不同的国家/州代码映射到基本实体?

visual-studio-2010 - 如何在VS单元测试中包含样本数据文件?

unit-testing - 使用 Apache Camel Exchange 作为参数的单元测试功能

java - 了解 Spring 中的 SessionFactory

java - Eclipse Job从运行Job开始获取NotesSession

c# - 验证属性从未使用 Moq 设置

java - JUnit 测试(正向和负向测试)

java - Spring Task Executor 在 JUNIT 中创建了额外的线程

java - 如何测试程序已经退出? [J单元]

java - 如何从日期获取时区