Reputation: 2384
Have a simple math operations test suite developed with JUnit 5.
I use two tags @Tag("add")
and @Tag("minus")
.
With maven and surefire plugin I select the tag for the tests.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration combine.children="append">
<excludedGroups></excludedGroups>
<groups>add</groups>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
</plugin>
When I do the mvn test
on the terminal it will execute only the tests marked by the @Tag("add")
but for some reason will execute the @BeforeEach
and @AfterEach
tagged exclusively with @Tag("minus")
@Tag("minus")
@BeforeEach
void beforeEach(TestInfo test) { /* code */ }
@Tag("minus")
@AfterEach
void afterEach(TestInfo test) { /* code */ }
Since the expected behaviour was not working, I even forced with <excludedGroups>
, changed from empty value to <excludedGroups>minus</excludedGroups>
on pom.xml
, still not worked.
There is something wrong on pom.xml
that I'm missing?
A conflict between @Tag
and @BeforeEach
plus @AfterEach
?
Upvotes: 1
Views: 1584
Reputation: 4841
@Tag is meant for test classes and methods. It has nothing to do with setup/teardown of said tests.
You could either split those tests into separate test classes or you could leverage Nested Test. Nested tests could look something like this:
@DisplayName("A stack")
class MathTest {
@Nested
@DisplayName("when new")
@Tag("add")
class Add {
@BeforeEach
void setupAdding() {
// ...
}
@Test
@DisplayName("can add")
void canAdd() {
// ...
}
}
@Nested
@DisplayName("minus")
@Tag("minus")
class Minus {
@BeforeEach
void setupMinus() {
// ...
}
@Test
@DisplayName("can subtract")
void testMinus() {
// ...
}
}
}
Upvotes: 4
Reputation: 454
I think that the @Tag annotation was conceived just to clasify your tests following a common characteristic. If you need different setUp() and teardown() I guess the righ way to go is to separate them in different test classes, not with the tag annotation.
In the documentation it is describes it can be used in a test class or a test method:
https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Tag.html
Upvotes: 2