Test for @RunWith(Parametrized.class).
Summary:
Wanted to try how Parametrized.class works with buck junit runner. It seems like
it already works pretty good so Im just adding a test for it.
Test Plan:
Run ant test
You may also try:
* cd test/com/facebook/buck/junit/testdata/runwith/
* buck test :ParametrizedTest
* Check buck-out/gen/__java_test_ParametrizedTest_output__/com.example.ParametrizedTest.xml
* Remember to rm -rf buck-out after that
diff --git a/test/com/facebook/buck/junit/BUCK b/test/com/facebook/buck/junit/BUCK
index 821ab0b..872cfe3 100644
--- a/test/com/facebook/buck/junit/BUCK
+++ b/test/com/facebook/buck/junit/BUCK
@@ -11,6 +11,7 @@
'//lib:hamcrest-library',
'//lib:junit',
'//src/com/facebook/buck/junit:junit',
+ '//src/com/facebook/buck/util:util',
'//test/com/facebook/buck/testutil/integration:integration',
],
)
diff --git a/test/com/facebook/buck/junit/RunWithAnnotationIntegrationTest.java b/test/com/facebook/buck/junit/RunWithAnnotationIntegrationTest.java
index b6a03a9..6be35c9 100644
--- a/test/com/facebook/buck/junit/RunWithAnnotationIntegrationTest.java
+++ b/test/com/facebook/buck/junit/RunWithAnnotationIntegrationTest.java
@@ -23,11 +23,18 @@
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult;
import com.facebook.buck.testutil.integration.TestDataHelper;
+import com.facebook.buck.util.XmlDomParser;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import java.io.FileReader;
import java.io.IOException;
public class RunWithAnnotationIntegrationTest {
@@ -41,7 +48,6 @@
this, "runwith", temporaryFolder);
workspace.setUp();
- // ExceedsAnnotationTimeoutTest should fail.
ProcessResult suiteTestResult = workspace.runBuckCommand("test", "//:SimpleSuiteTest");
assertEquals("Test should pass", 0, suiteTestResult.getExitCode());
assertThat(suiteTestResult.getStderr(), containsString("2 Passed"));
@@ -53,7 +59,6 @@
this, "runwith", temporaryFolder);
workspace.setUp();
- // ExceedsAnnotationTimeoutTest should fail.
ProcessResult suiteTestResult = workspace.runBuckCommand("test", "//:FailingSuiteTest");
assertEquals("Test should fail because of one of subtests failure",
1,
@@ -62,4 +67,35 @@
assertThat(suiteTestResult.getStderr(), containsString("1 Failed"));
}
+ @Test
+ public void testParametrizedTestRun4Cases() throws IOException {
+ ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(
+ this, "runwith", temporaryFolder);
+ workspace.setUp();
+
+ ProcessResult suiteTestResult = workspace.runBuckCommand("test", "//:ParametrizedTest");
+ suiteTestResult.assertExitCode("Test should pass", 0);
+ assertThat(suiteTestResult.getStderr(), containsString("4 Passed"));
+
+ Document doc = XmlDomParser.parse(new InputSource(new FileReader(workspace.getFile(
+ "buck-out/gen/__java_test_ParametrizedTest_output__/com.example.ParametrizedTest.xml"))),
+ false);
+
+ NodeList testNodes = doc.getElementsByTagName("test");
+ assertEquals(4, testNodes.getLength());
+
+ for (int i = 0; i < testNodes.getLength(); i++) {
+ Node testNode = testNodes.item(i);
+
+ String expectedName = String.format("parametrizedTest[%d]", i);
+ assertEquals(expectedName, testNode.getAttributes().getNamedItem("name").getTextContent());
+
+ String expectedStdout = String.format("Parameter: %d\n", i);
+ assertEquals(
+ expectedStdout,
+ ((Element) testNode).getElementsByTagName("stdout").item(0).getTextContent());
+ }
+
+ }
+
}
diff --git a/test/com/facebook/buck/junit/testdata/runwith/BUCK b/test/com/facebook/buck/junit/testdata/runwith/BUCK
index 4a8d2a6..4dab91a 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/BUCK
+++ b/test/com/facebook/buck/junit/testdata/runwith/BUCK
@@ -24,6 +24,14 @@
],
)
+java_test(
+ name = 'ParametrizedTest',
+ srcs = glob(['ParametrizedTest.java']),
+ deps = [
+ ':junit',
+ ],
+)
+
prebuilt_jar(
name = 'junit',
binary_jar = 'junit-4.11.jar',
diff --git a/test/com/facebook/buck/junit/testdata/runwith/FailingSubtest.java b/test/com/facebook/buck/junit/testdata/runwith/FailingSubtest.java
index ce410f0..da46332 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/FailingSubtest.java
+++ b/test/com/facebook/buck/junit/testdata/runwith/FailingSubtest.java
@@ -14,7 +14,7 @@
* under the License.
*/
-package com.facebook.buck.junit.testdata.runwith;
+package com.example;
import static org.junit.Assert.fail;
diff --git a/test/com/facebook/buck/junit/testdata/runwith/FailingSuiteTest.java b/test/com/facebook/buck/junit/testdata/runwith/FailingSuiteTest.java
index 8276deb..3d0c123 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/FailingSuiteTest.java
+++ b/test/com/facebook/buck/junit/testdata/runwith/FailingSuiteTest.java
@@ -14,7 +14,7 @@
* under the License.
*/
-package com.facebook.buck.junit.testdata.runwith;
+package com.example;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
diff --git a/test/com/facebook/buck/junit/testdata/runwith/ParametrizedTest.java b/test/com/facebook/buck/junit/testdata/runwith/ParametrizedTest.java
new file mode 100644
index 0000000..3b0250e
--- /dev/null
+++ b/test/com/facebook/buck/junit/testdata/runwith/ParametrizedTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2013-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.example;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+@RunWith(Parameterized.class)
+public class ParametrizedTest {
+
+ private int number;
+
+ public ParametrizedTest(int number) {
+ this.number = number;
+ }
+
+ @Parameterized.Parameters
+ public static Collection<Object[]> data() {
+ Object[][] data = new Object[][] { { 0 }, { 1 }, { 2 }, { 3 } };
+ return Arrays.asList(data);
+ }
+
+ @Test
+ public void parametrizedTest() {
+ System.out.println("Parameter: " + number);
+ }
+
+}
diff --git a/test/com/facebook/buck/junit/testdata/runwith/SimpleSuiteTest.java b/test/com/facebook/buck/junit/testdata/runwith/SimpleSuiteTest.java
index 037c3a3..c4ac9e1 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/SimpleSuiteTest.java
+++ b/test/com/facebook/buck/junit/testdata/runwith/SimpleSuiteTest.java
@@ -14,7 +14,7 @@
* under the License.
*/
-package com.facebook.buck.junit.testdata.runwith;
+package com.example;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
diff --git a/test/com/facebook/buck/junit/testdata/runwith/Subtest1.java b/test/com/facebook/buck/junit/testdata/runwith/Subtest1.java
index 2113913..e0d4b92 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/Subtest1.java
+++ b/test/com/facebook/buck/junit/testdata/runwith/Subtest1.java
@@ -14,7 +14,7 @@
* under the License.
*/
-package com.facebook.buck.junit.testdata.runwith;
+package com.example;
import org.junit.Test;
diff --git a/test/com/facebook/buck/junit/testdata/runwith/Subtest2.java b/test/com/facebook/buck/junit/testdata/runwith/Subtest2.java
index 132c58b..5ffa3ac 100644
--- a/test/com/facebook/buck/junit/testdata/runwith/Subtest2.java
+++ b/test/com/facebook/buck/junit/testdata/runwith/Subtest2.java
@@ -14,7 +14,7 @@
* under the License.
*/
-package com.facebook.buck.junit.testdata.runwith;
+package com.example;
import org.junit.Test;