Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Test for @RunWith(Parametrized.class).
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kmagiera authored and bolinfest committed Aug 13, 2013
1 parent e112264 commit 04d334d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 7 deletions.
1 change: 1 addition & 0 deletions test/com/facebook/buck/junit/BUCK
Expand Up @@ -11,6 +11,7 @@ java_test(
'//lib:hamcrest-library',
'//lib:junit',
'//src/com/facebook/buck/junit:junit',
'//src/com/facebook/buck/util:util',
'//test/com/facebook/buck/testutil/integration:integration',
],
)
40 changes: 38 additions & 2 deletions test/com/facebook/buck/junit/RunWithAnnotationIntegrationTest.java
Expand Up @@ -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 {
Expand All @@ -41,7 +48,6 @@ public void testSimpleSuiteRun2TestCases() throws IOException {
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"));
Expand All @@ -53,7 +59,6 @@ public void testFailingSuiteRun3TestCasesWith1Failure() throws IOException {
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,
Expand All @@ -62,4 +67,35 @@ public void testFailingSuiteRun3TestCasesWith1Failure() throws IOException {
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());
}

}

}
8 changes: 8 additions & 0 deletions test/com/facebook/buck/junit/testdata/runwith/BUCK
Expand Up @@ -24,6 +24,14 @@ java_test(
],
)

java_test(
name = 'ParametrizedTest',
srcs = glob(['ParametrizedTest.java']),
deps = [
':junit',
],
)

prebuilt_jar(
name = 'junit',
binary_jar = 'junit-4.11.jar',
Expand Down
Expand Up @@ -14,7 +14,7 @@
* under the License.
*/

package com.facebook.buck.junit.testdata.runwith;
package com.example;

import static org.junit.Assert.fail;

Expand Down
Expand Up @@ -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;
Expand Down
@@ -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);
}

}
Expand Up @@ -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;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* under the License.
*/

package com.facebook.buck.junit.testdata.runwith;
package com.example;

import org.junit.Test;

Expand Down
Expand Up @@ -14,7 +14,7 @@
* under the License.
*/

package com.facebook.buck.junit.testdata.runwith;
package com.example;

import org.junit.Test;

Expand Down

0 comments on commit 04d334d

Please sign in to comment.