Skip to content

Commit 99bb8d8

Browse files
committed
Improve detection of ignored and disabled tests
1 parent d99dd21 commit 99bb8d8

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/main/java/de/donnerbart/split/TestSplit.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.javaparser.JavaParser;
55
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
66
import com.github.javaparser.ast.expr.AnnotationExpr;
7+
import com.github.javaparser.ast.nodeTypes.NodeWithName;
78
import de.donnerbart.split.model.Split;
89
import de.donnerbart.split.model.TestCase;
910
import de.donnerbart.split.model.TestSuite;
@@ -30,8 +31,9 @@
3031

3132
public class TestSplit {
3233

33-
private static final @NotNull Set<String> SKIP_TEST_ANNOTATIONS =
34-
Set.of("org.junit.jupiter.api.Disabled", "Disabled", "org.junit.Ignore", "Ignore");
34+
private static final @NotNull Set<String> SKIP_TEST_IMPORTS =
35+
Set.of("org.junit.jupiter.api.Disabled", "org.junit.Ignore");
36+
private static final @NotNull Set<String> SKIP_TEST_ANNOTATIONS = Set.of("Disabled", "Ignore");
3537

3638
private static final @NotNull Logger LOG = LoggerFactory.getLogger(TestSplit.class);
3739

@@ -195,18 +197,26 @@ public void run() throws Exception {
195197
final var className = declaration.getFullyQualifiedName().orElseThrow();
196198
if (declaration.isInterface()) {
197199
LOG.info("Skipping test interface {}", className);
200+
continue;
198201
} else if (declaration.isAbstract()) {
199202
LOG.info("Skipping abstract test class {}", className);
200-
} else if (declaration.getAnnotations()
203+
continue;
204+
}
205+
final var hasSkipTestImport = compilationUnit.getImports()
206+
.stream()
207+
.map(NodeWithName::getNameAsString)
208+
.anyMatch(SKIP_TEST_IMPORTS::contains);
209+
final var hasSkipTestAnnotation = declaration.getAnnotations()
201210
.stream()
202211
.map(AnnotationExpr::getNameAsString)
203-
.anyMatch(SKIP_TEST_ANNOTATIONS::contains)) {
212+
.anyMatch(SKIP_TEST_ANNOTATIONS::contains);
213+
if (hasSkipTestImport && hasSkipTestAnnotation) {
204214
LOG.info("Skipping disabled test class {}", className);
205-
} else {
206-
classNames.add(className);
215+
continue;
207216
}
217+
classNames.add(className);
208218
} catch (final Exception e) {
209-
LOG.error("Failed to parse test class: {}", testPath, e);
219+
LOG.error("Failed to parse test class {}", testPath, e);
210220
exitCodeConsumer.accept(1);
211221
}
212222
}

src/test/java/de/donnerbart/split/TestSplitTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ void setUp() throws Exception {
4343
.resolve("donnerbart")
4444
.resolve("example");
4545
Files.createDirectories(projectFolder);
46+
// ignored tests
4647
copyResourceToTarget(projectFolder, "tests/BaseTest.java", "BaseTest.java", PERMISSIONS);
4748
copyResourceToTarget(projectFolder, "tests/DisabledTest.java", "DisabledTest.java", PERMISSIONS);
49+
copyResourceToTarget(projectFolder, "tests/IgnoreTest.java", "IgnoreTest.java", PERMISSIONS);
4850
copyResourceToTarget(projectFolder, "tests/InterfaceTest.java", "InterfaceTest.java", PERMISSIONS);
51+
// valid tests
4952
copyResourceToTarget(projectFolder, "tests/FastTest.java", "FastTest.java", PERMISSIONS);
5053
copyResourceToTarget(projectFolder, "tests/NoTimingOneTest.java", "NoTimingOneTest.java", PERMISSIONS);
5154
copyResourceToTarget(projectFolder, "tests/NoTimingTwoTest.java", "NoTimingTwoTest.java", PERMISSIONS);

src/test/resources/tests/DisabledTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.junit.jupiter.api.Disabled;
44
import org.junit.jupiter.api.Test;
55

6-
@Disabled("This test should not be split")
6+
@Disabled("This test should be skipped")
77
class DisabledTest {
88

99
@Test
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.donnerbart.example;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
6+
@Ignore("This test should be skipped")
7+
class IgnoreTest {
8+
9+
@Test
10+
void testIgnored() {
11+
}
12+
}

0 commit comments

Comments
 (0)