-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specifying dataProvider & successPercentage causes test to always pass
Closes #3170
- Loading branch information
1 parent
c9ec222
commit f55ca8e
Showing
8 changed files
with
257 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 115 additions & 9 deletions
124
testng-core/src/test/java/test/invocationcount/issue1719/IssueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,128 @@ | ||
package test.invocationcount.issue1719; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
import org.assertj.core.api.SoftAssertions; | ||
import org.testng.ITestResult; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import test.SimpleBaseTest; | ||
|
||
public class IssueTest extends SimpleBaseTest { | ||
|
||
@Test | ||
public void testSuccessPercentageCalculation() { | ||
TestNG testng = create(TestclassSample.class); | ||
@Test(dataProvider = "dp") | ||
public void testSuccessPercentageCalculation(Class<?> clazz, Expected expected) { | ||
TestNG testng = create(clazz); | ||
DummyReporter listener = new DummyReporter(); | ||
testng.addListener(listener); | ||
testng.run(); | ||
assertThat(listener.getFailures()).isEmpty(); | ||
assertThat(listener.getSkip()).isEmpty(); | ||
assertThat(listener.getSuccess()).isEmpty(); | ||
assertThat(listener.getFailedWithinSuccessPercentage()).hasSize(5); | ||
SoftAssertions assertions = new SoftAssertions(); | ||
String msg = | ||
pretty( | ||
"[failedWithinSuccessPercentage]", | ||
expected.failedWithinSuccessPercentage, | ||
listener.getFailedWithinSuccessPercentage()); | ||
assertions | ||
.assertThat(listener.getFailedWithinSuccessPercentage()) | ||
.withFailMessage(msg) | ||
.hasSize(expected.failedWithinSuccessPercentage); | ||
|
||
msg = pretty("[skip]", expected.skip, listener.getSkip()); | ||
assertions.assertThat(listener.getSkip()).withFailMessage(msg).hasSize(expected.skip); | ||
|
||
msg = pretty("[success]", expected.success, listener.getSuccess()); | ||
assertions.assertThat(listener.getSuccess()).withFailMessage(msg).hasSize(expected.success); | ||
|
||
msg = pretty("[failures]", expected.failures, listener.getFailures()); | ||
assertions.assertThat(listener.getFailures()).withFailMessage(msg).hasSize(expected.failures); | ||
|
||
assertions.assertAll(); | ||
} | ||
|
||
private static String pretty(String prefix, int expected, Set<ITestResult> actual) { | ||
return prefix + " test. Expected: " + expected + ", Actual: " + actual.size(); | ||
} | ||
|
||
@DataProvider(name = "dp") | ||
public Object[][] dp() { | ||
return new Object[][] { | ||
{ | ||
TestclassSample.DataDrivenTestHavingZeroSuccessPercentageAndNoInvocationCount.class, | ||
new Expected().failures(2) | ||
}, | ||
{ | ||
TestclassSample.DataDrivenTestHavingValidSuccessPercentageAndInvocationCount.class, | ||
// Parameter - Invocation Count - Expected Test Result | ||
// . 1 1 Failed Within success percentage (30% expected) | ||
// 1 2 Passed (Remember this is a flaky test simulation) | ||
// 2 1 Failed Within success percentage (30% expected) | ||
// 2 2 Failed | ||
new Expected().failures(1).failedWithinSuccessPercentage(2).success(1) | ||
}, | ||
{ | ||
TestclassSample.RegularTestWithZeroSuccessPercentage.class, | ||
new Expected().failedWithinSuccessPercentage(1) | ||
}, | ||
{ | ||
TestclassSample.RegularTestWithZeroSuccessPercentageAndInvocationCount.class, | ||
new Expected().failedWithinSuccessPercentage(2) | ||
}, | ||
}; | ||
} | ||
|
||
public static class Expected { | ||
private int failures = 0; | ||
private int success = 0; | ||
private int skip = 0; | ||
private int failedWithinSuccessPercentage = 0; | ||
|
||
public int failures() { | ||
return failures; | ||
} | ||
|
||
public Expected failures(int failures) { | ||
this.failures = failures; | ||
return this; | ||
} | ||
|
||
public int success() { | ||
return success; | ||
} | ||
|
||
public Expected success(int success) { | ||
this.success = success; | ||
return this; | ||
} | ||
|
||
public int skip() { | ||
return skip; | ||
} | ||
|
||
public Expected skip(int skip) { | ||
this.skip = skip; | ||
return this; | ||
} | ||
|
||
public int failedWithinSuccessPercentage() { | ||
return failedWithinSuccessPercentage; | ||
} | ||
|
||
public Expected failedWithinSuccessPercentage(int failedWithinSuccessPercentage) { | ||
this.failedWithinSuccessPercentage = failedWithinSuccessPercentage; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{failures=" | ||
+ failures | ||
+ ", success=" | ||
+ success | ||
+ ", skip=" | ||
+ skip | ||
+ ", failedWithinSuccessPercentage=" | ||
+ failedWithinSuccessPercentage | ||
+ '}'; | ||
} | ||
} | ||
} |
56 changes: 44 additions & 12 deletions
56
testng-core/src/test/java/test/invocationcount/issue1719/TestclassSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,60 @@ | ||
package test.invocationcount.issue1719; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.testng.Assert; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestclassSample { | ||
|
||
@Test(successPercentage = 0, dataProvider = "dp") | ||
public void dataDrivenTestMethod(int i) { | ||
Assert.fail("Failing iteration:" + i); | ||
} | ||
|
||
@DataProvider(name = "dp") | ||
public Object[][] getData() { | ||
public static Object[][] getData() { | ||
return new Object[][] {{1}, {2}}; | ||
} | ||
|
||
@Test(successPercentage = 0) | ||
public void simpleTestMethod() { | ||
Assert.fail(); | ||
public static class DataDrivenTestHavingZeroSuccessPercentageAndNoInvocationCount { | ||
@Test(successPercentage = 0, dataProvider = "dp", dataProviderClass = TestclassSample.class) | ||
public void dataDrivenTestMethod(int i) { | ||
Assert.fail("Failing iteration:" + i); | ||
} | ||
} | ||
|
||
public static class DataDrivenTestHavingValidSuccessPercentageAndInvocationCount { | ||
|
||
private boolean shouldFail = true; | ||
private Map<Integer, AtomicInteger> tracker = new HashMap<>(); | ||
|
||
@Test( | ||
successPercentage = 30, | ||
dataProvider = "dp", | ||
invocationCount = 2, | ||
dataProviderClass = TestclassSample.class) | ||
public void dataDrivenTestMethodWithInvocationCount(int i) { | ||
int current = tracker.computeIfAbsent(i, k -> new AtomicInteger()).incrementAndGet(); | ||
String msg = String.format("Parameter [%d], Invocation [%d]", i, current); | ||
if (i != 1) { // If the parameter is NOT 1, then just fail | ||
Assert.fail("Failing test " + msg); | ||
} | ||
if (shouldFail) { // If the parameter is 1, then simulate a flaky test that passes and fails | ||
shouldFail = false; | ||
Assert.fail("Failing test " + msg); | ||
} | ||
} | ||
} | ||
|
||
public static class RegularTestWithZeroSuccessPercentage { | ||
@Test(successPercentage = 0) | ||
public void simpleTestMethod() { | ||
Assert.fail(); | ||
} | ||
} | ||
|
||
@Test(successPercentage = 0, invocationCount = 2) | ||
public void testMethodWithMultipleInvocations() { | ||
Assert.fail(); | ||
public static class RegularTestWithZeroSuccessPercentageAndInvocationCount { | ||
@Test(successPercentage = 0, invocationCount = 2) | ||
public void testMethodWithMultipleInvocations() { | ||
Assert.fail(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...cationcount/issue3170/DataDrivenWithSuccessPercentageAndInvocationCountDefinedSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package test.invocationcount.issue3170; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class DataDrivenWithSuccessPercentageAndInvocationCountDefinedSample { | ||
@Test(dataProvider = "test", invocationCount = 10, successPercentage = 90) | ||
public void sampleTestCase(String string) { | ||
assertEquals(string, "1"); | ||
} | ||
|
||
@DataProvider(name = "test") | ||
public Object[][] testProvider() { | ||
return new Object[][] {{"2"}}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...est/java/test/invocationcount/issue3170/DataDrivenWithSuccessPercentageDefinedSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package test.invocationcount.issue3170; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class DataDrivenWithSuccessPercentageDefinedSample { | ||
@Test(dataProvider = "test", successPercentage = 99) | ||
public void sampleTestCase(String string) { | ||
assertEquals(string, "1"); | ||
} | ||
|
||
@DataProvider(name = "test") | ||
public Object[][] testProvider() { | ||
return new Object[][] {{"1"}, {"2"}, {"3"}, {"4"}}; | ||
} | ||
} |