Skip to content

Commit

Permalink
GITHUB-2888 - don't fail test with dataProvider if skipped (#2889)
Browse files Browse the repository at this point in the history
* GITHUB-2888 - don't fail test with dataProvider if skipped
  • Loading branch information
jmoreira18 authored Mar 23, 2023
1 parent ebd9dae commit ff5843a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2888: Skipped Tests with DataProvider appear as failed (Joaquin Moreira)
Fixed: GITHUB-2884: Discrepancies with DataProvider and Retry of failed tests (Krishnan Mahadevan)
Fixed: GITHUB-2879: Test listeners specified in parent testng.xml file are not included in testng-failed.xml file (Krishnan Mahadevan)
Fixed: GITHUB-2866: TestNG.xml doesn't honour Parallel value of a clone (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ public int invoke(int invCount) {
m_configuration.isPropagateDataProviderFailureAsTestFailure()
|| bag.isBubbleUpFailures();

if (throwable instanceof TestNGException || bubbleUpFailures) {
if (!(throwable instanceof SkipException)
&& (throwable instanceof TestNGException || bubbleUpFailures)) {
tr.setStatus(ITestResult.FAILURE);
m_notifier.addFailedTest(arguments.getTestMethod(), tr);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import test.dataprovider.issue2819.TestClassSample;
import test.dataprovider.issue2819.TestClassUsingDataProviderRetrySample;
import test.dataprovider.issue2819.TestClassWithMultipleRetryImplSample;
import test.dataprovider.issue2888.SkipDataProviderSample;

public class DataProviderTest extends SimpleBaseTest {

Expand Down Expand Up @@ -513,6 +514,14 @@ public void ensureTestNGFailsDueToDataProviderFailure2() {
assertThat(testng.getStatus()).isEqualTo(1);
}

@Test(description = "GITHUB-2888")
public void ensureTestNGSkipExceptionWillSkipTestWithDataProvider() {
TestNG testng = create(SkipDataProviderSample.class);
testng.propagateDataProviderFailureAsTestFailure();
testng.run();
assertThat(testng.getStatus()).isEqualTo(2);
}

@Test(description = "GITHUB-2255")
public void ensureDataProviderValuesAreVisibleToConfigMethods() {
TestNG testNG = create(test.dataprovider.issue2255.TestClassSample.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.dataprovider.issue2888;

import java.util.Arrays;
import org.testng.IDataProviderListener;
import org.testng.IDataProviderMethod;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.SkipException;

public class SkipDataProviderListener implements ITestListener, IDataProviderListener {
@Override
public void onTestStart(ITestResult result) {
skipIfSkipMe(result.getMethod());
}

@Override
public void onTestSkipped(ITestResult result) {}

@Override
public void beforeDataProviderExecution(
IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext) {
skipIfSkipMe(method);
}

private static void skipIfSkipMe(ITestNGMethod testNGMethod) {
if (Arrays.asList(testNGMethod.getGroups()).contains("SkipMe"))
throw new SkipException("Test was skipped");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.dataprovider.issue2888;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({SkipDataProviderListener.class})
public class SkipDataProviderSample {
@Test(groups = "SkipMe")
public void testSkip() {
Assert.fail("This test should not execute, it should be skipped");
}

@DataProvider(name = "dataProvider")
private Object[][] dataProvider() {
return new Object[][] {new Object[] {"test1"}};
}

@Test(dataProvider = "dataProvider", groups = "SkipMe")
public void testSkipWithDataProvider(String a) {
Assert.fail("This test should not execute, it should be skipped");
}
}

0 comments on commit ff5843a

Please sign in to comment.