Skip to content

Commit

Permalink
[OX-851] JUnit5: support running individual test method
Browse files Browse the repository at this point in the history
The PR adds support of test selector for JUnit5 tests. 
Note: The failure count is still incorrect when there's failures, hope to address that in the future.

#### Before
```
$ ./buck test domain/service/accountreceivedate: -f AccountReceiveDateServiceTest#test2
Building: finished in 0.9 sec (100%) 2471/2471 jobs, 2 updated
  Total time: 0.9 sec
Testing: finished in 6.2 sec (2 PASS/0 FAIL)
RESULTS FOR SELECTED TESTS
PASS     113ms  2 Passed   0 Skipped   0 Failed   
                ^
com.addepar.domain.service.accountreceivedate.AccountReceiveDateServiceTest
TESTS PASSED
```

#### After
```
$ ./buck test domain/service/accountreceivedate: -f AccountReceiveDateServiceTest#test2
Building: finished in 0.8 sec (100%) 2471/2471 jobs, 0 updated
  Total time: 0.9 sec
Testing: finished in 8.9 sec (1 PASS/0 FAIL)
RESULTS FOR SELECTED TESTS
PASS     139ms  1 Passed   0 Skipped   0 Failed   
                ^
com.addepar.domain.service.accountreceivedate.AccountReceiveDateServiceTest
TESTS PASSED
```
  • Loading branch information
ztai-add authored Jun 7, 2024
2 parents 00ea1f3 + 7712d1a commit 9a2dc3f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/com/facebook/buck/test/selectors/PatternTestSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ public String getExplanation() {
isMatchAnyMethod() ? "<any>" : methodPattern);
}

@Nullable
@Override
public String getMethod() {
if (methodPattern == null) {
return null;
}
String methodPatternString = methodPattern.toString();
return methodPatternString.substring(0, methodPatternString.length() - 1);
}

@Override
public boolean isInclusive() {
return inclusive;
Expand Down
6 changes: 6 additions & 0 deletions src/com/facebook/buck/test/selectors/SimpleTestSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public String getExplanation() {
isMatchAnyClass() ? "<any>" : className, isMatchAnyMethod() ? "<any>" : methodName);
}

@Nullable
@Override
public String getMethod() {
return methodName;
}

@Override
public boolean isInclusive() {
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/com/facebook/buck/test/selectors/TestSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public interface TestSelector {

String getExplanation();

@Nullable
String getMethod();

boolean isInclusive();

boolean isMatchAnyClass();
Expand Down
4 changes: 4 additions & 0 deletions src/com/facebook/buck/test/selectors/TestSelectorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public List<String> getRawSelectors() {
return rawSelectors;
}

public List<TestSelector> getSelectors() {
return testSelectors;
}

public boolean isEmpty() {
return testSelectors.isEmpty();
}
Expand Down
22 changes: 17 additions & 5 deletions src/com/facebook/buck/testrunner/JUnitRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.facebook.buck.testrunner;

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;

import com.facebook.buck.test.selectors.TestDescription;
import com.facebook.buck.test.selectors.TestSelector;
Expand Down Expand Up @@ -90,13 +91,24 @@ public void run() throws Throwable {
jUnitCore.addListener(new Junit4TestListener(results, stdOutLogLevel, stdErrLogLevel, isDryRun));
jUnitCore.run(request);
} else if (mightBeJunit5TestClass(testClass)) {
LauncherDiscoveryRequest request =
LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(testClass))
.build();
LauncherDiscoveryRequestBuilder requestBuilder = LauncherDiscoveryRequestBuilder.request();
if (testSelectorList.isEmpty()) {
requestBuilder = requestBuilder.selectors(selectClass(testClass));
} else {
for (TestSelector selector : testSelectorList.getSelectors()) {
if (selector.matchesClassName(testClass.getSimpleName())) {
if (selector.isMatchAnyMethod()) {
requestBuilder = requestBuilder.selectors(selectClass(testClass));
} else {
requestBuilder = requestBuilder.selectors(selectMethod(testClass, selector.getMethod()));
}
break;
}
}
}
Launcher launcher = LauncherFactory.create();
Junit5TestListener listener = new Junit5TestListener(results, stdOutLogLevel, stdErrLogLevel, testClass);
launcher.execute(request, listener);
launcher.execute(requestBuilder.build(), listener);
}
// Combine the results with the tests we filtered out
List<TestResult> actualResults = combineResults(results, filter.filteredOut);
Expand Down

0 comments on commit 9a2dc3f

Please sign in to comment.