问题
Is it possible in JUnit to add a brief description of the test for the future reader (e.g. what's being tested, some short explanation, expected result, ...)? I mean something like in ScalaTest, where I can write:
test("Testing if true holds") {
assert(true)
}
Ideal approach would be using some annotation, e.g.
@Test
@TestDescription("Testing if true holds")
public void testTrue() {
assert(true);
}
Therefore, if I run such annotated tests using Maven (or some similar tool), I could have similar output to the one I have in SBT when using ScalaTest:
- Testing if entity gets saved correctly
- Testing if saving fails when field Name is not specified
- ...
Currently I can either use terribly long method names or write javadoc comments, which are not present in the build output.
Thank you.
回答1:
In JUnit 5, there is @DisplayName annotation:
@DisplayName is used to declare a custom display name for the annotated test class or test method. Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.
Example:
@Test
@DisplayName("Test if true holds")
public void checkTrue() {
assertEquals(true, true);
}
回答2:
TestNG does it like this, which to me is the neatest solution:
@Test(description="My funky test")
public void testFunk() {
...
}
See http://testng.org/javadocs/org/testng/annotations/Test.html for more information.
回答3:
Not exactly what you are looking for, but you can provide a description on any assert
methods.
Something like:
@Test
public void testTrue() {
assertTrue("Testing if true holds", true);
}
回答4:
I prefer to follow a standard format when testing in JUnit. The name of the test would be
test[method name]_[condition]_[outcome]
for Example:
@Test
public void testCreateObject_nullField_errorMessage(){}
@Test
public void testCreateObject_validObject_objectCreated(){}
I think this approach is helpful when doing TDD, because you can just start writing all the test names, so you know what you need to test / develop.
Still I would welcome a test description functionality from JUnit.
And this is certainly better than other tests I have seen in the past like:
@Test public void testCreateObject1(){}
@Test public void testCreateObject2(){}
@Test public void testCreateObject3(){}
or
@Test public void testCreateObjectWithNullFirstNameAndSecondNameTooLong(){}
回答5:
You can name the test method after the test:
public void testThatOnePlusOneEqualsTwo() {
assertEquals(2, 1 + 1);
}
This will show up in Eclipse, Surefire, and most other runners.
回答6:
The detailed solution would be: You could add a Logger
to your test, to log the results to a File. See log4j, for example. Then you can read the results in the File and also print successfull statements, what assert
statemens cannot.
The simple solution: You can add a JDoc
description to every test method, this will be outlined, if you generate the JavaDoc.
Also every assert
statement can provide a Message, that will be printed, whenever the assert
fails.
/**
* test the List#size() increasement after adding an Object to a List.
*/
public void testAdd(){
List<Object> list = new LinkedList<>();
list.add(new Object());
assertEquals("size should be 1, because of adding an Object", 1, list.size());
}
Do NOT use System.out.println("your message");
because you don't know how the tests will be executed and if the environment does not provide a console, your messages will not be displayed.
来源:https://stackoverflow.com/questions/13159293/junit-test-description