Is there a way to force Maven Surefire or JUnit to

2019-06-21 05:18发布

My question: Is there a way to force Maven Surefire (or JUnit?) to indicate the name of the failing test class when this class has been executed from a Test Suite?

Now, the long story.

Imagine you have a JUnit test suite that launches two JUnit tests.

Examples of test classes:

public class TestOne {

    @Test
    public void foo() {
        assertTrue(false);
    }

    @Test
    public void bar() {
        assertTrue(false);
    }

}

@UnitTest
public class TestTwo {

    @Test
    public void foo() {
        assertFalse(true);
    }

}

and the test suite:

@RunWith(Suite.class)
@SuiteClasses(value = { TestOne.class, TestTwo.class })
public class MySuite {

}

Now, if I run the test suite (mvn test -Dtest=MySuite), I will have the following errors:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running caf.opm.preclosing.junit.MySuite
Tests run: 3, Failures: 3, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec <<< FAILURE!

Results :

Failed tests:
  foo(my.project.junit.TestOne)
  bar(my.project.junit.TestOne)
  foo(my.project.junit.TestTwo)

Tests run: 3, Failures: 3, Errors: 0, Skipped: 0

So reading these lines, I can easily detect which classes and tests are failing.

However, in target/surefire-reports/ directory, I have only one file, MySuite.txt where the three tests are failing. One of the consequences of such situation is that my Hudson build will show me the following information:

All Failed Tests
Test Name       Duration    Age   
>>> my.project.junit.MySuite.foo    0.0 1
>>> my.project.junit.MySuite.bar    0.0 1
>>> my.project.junit.MySuite.foo    0.0 1

As you can see, it becomes harder to identify the failing tests, and for example, as TestOne.foo() and TestTwo.foo() failed, I saw two MySuite.foo errors in my JUnit report. Of course I can retrieve the name of the test class by reading the logs of the failed test, but it's not really a solution...

So my question is to know if I can force Surefire (or JUnit?) to indicate the name of test class.

Thanks.

1条回答
对你真心纯属浪费
2楼-- · 2019-06-21 06:04

I get a single file per test class, so it took me a moment to figure this one out: You're using a test suite.

When using test suites, all the test results end up in a single file with the name of the suite.

There is no option to change this behavior. Try to file a feature request.

查看更多
登录 后发表回答