Skip to content

Commit

Permalink
Handle parameterized unit tests. (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjust authored Sep 9, 2024
1 parent 2cce785 commit 290d8e4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
Binary file modified framework/lib/formatter.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions framework/lib/formatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Custom JUnit test formatter that outputs executed and failing tests, following
# Defects4J's naming conventions for test entries.

Run ant test to compile and test the formatter implementation.

Run ant jar to build and deploy the `formatter.jar`.
1 change: 1 addition & 0 deletions framework/lib/formatter/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
--- edu.washington.cs.mut.testrunner.junit4.EmptyTestClass
--- edu.washington.cs.mut.testrunner.junit4.InitError
--- edu.washington.cs.mut.testrunner.junit4.MethodTimeout::test2
--- edu.washington.cs.mut.testrunner.junit4.ParamTest::testWithParams
--- edu.washington.cs.mut.testrunner.junit4.NonJUnitTest
--- edu.washington.cs.mut.testrunner.junit4.NotPublicSuite
--- edu.washington.cs.mut.testrunner.junit4.SimpleTest::test2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,26 @@ private void handle(Test test, Throwable t) {

className = test.getClass().getName();
{
Pattern regexp = Pattern.compile("(.*)\\((.*)\\)\\s*");
/*
The expected format for failing tests in Defects4J is:
--- <class name>[::<method name>].
In JUnit, a test's String representation is:
<method name>(<class name>).
In JUnit, a parameterized test's String representation is:
<method name>[<params>](<class name>).
The pattern below extracts only the method name and class name,
stripping the [<params>] part if it exists.
*/
Pattern regexp = Pattern.compile("([^\\[\\(]*)(\\[.*\\])?\\((.*)\\)\\s*");
Matcher match = regexp.matcher(test.toString());
if (match.matches()) {
// Class name will equal to junit.framework.Junit4TestCaseFacade if Junit4
// style tests are ran with Junit3 style test runner.
if(className.equals("junit.framework.JUnit4TestCaseFacade"))
className = match.group(2);
className = match.group(3);
methodName = match.group(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.washington.cs.mut.testrunner.junit4;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RunWith(Parameterized.class)
public class ParamTest {

private Integer testNo;

public ParamTest(Integer n) {
this.testNo = n;
}

@Parameters
public static Collection<Object[]> params() {
final List<Object[]> list = new ArrayList<Object[]>(3);
list.add(new Integer[] {1});
list.add(new Integer[] {2});
list.add(new Integer[] {3});
return list;
}

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

0 comments on commit 290d8e4

Please sign in to comment.