Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed test status of optional in dataprovider test #256

Merged
merged 3 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void onMethodEnd(MethodEndEvent event) {
ITestResult testResult = event.getTestResult();

// Handle assertions and exceptions in dataprovider methods
if (testResult.getMethod().isDataDriven() && methodContext.readErrors().findFirst().isPresent()) {
if (testResult.getMethod().isDataDriven()
&& methodContext.readErrors().anyMatch(ErrorContext::isNotOptional)) {
Zsar marked this conversation as resolved.
Show resolved Hide resolved
testResult.setStatus(ITestResult.SKIP);
methodContext.setStatus(Status.SKIPPED);
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Arrays;
Expand Down Expand Up @@ -140,10 +139,14 @@ public String filterLogForFollowingEntry(final String searchString) {
public void assertTestStatusPerMethod(final String classNameSlug, final String methodNameSlug, final Status expectedStatus) {
final List<String> foundEntries = filterLogForTestMethod(classNameSlug, methodNameSlug);

for (final String entry : foundEntries) {
log().debug("Asserting '{}' for '{}'", expectedStatus, methodNameSlug);
Assert.assertTrue(entry.contains(expectedStatus.title), String.format("'%s' has status '%s'", methodNameSlug, expectedStatus));
}
log().debug("Asserting '{}' for '{}'", expectedStatus, methodNameSlug);
foundEntries.stream()
.filter(line -> line.contains(expectedStatus.title))
.findFirst()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not anyMatch work just as well here? Or must we discard results after the first?
(value is unused, so we do not actually need a find operation.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work without value: () -> log().info(...) --> IntelliJ: Cannot infer functional interface type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's just a helper class for tests. ;-)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean like this:

if (foundEntries.stream().anyMatch(line -> line.contains(expectedStatus.title))) {
  log().info("Found status {}", expectedStatus.title);
}
else {
  Assert.fail(String.format("'%s' should have status '%s'", methodNameSlug, expectedStatus));
}

?
value is unused in ifPresentOrElse, so we do not actually need a result item.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or alternatively: Did you forget to output value in the ifPresent part?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of unique combination of class and method I have one result or nothing.

And value is not needed in output, I dont want to log the whole line. But value must be declared.

.ifPresentOrElse(
value -> log().info("Found status {}", expectedStatus.title),
() -> Assert.fail(String.format("'%s' should have status '%s'", methodNameSlug, expectedStatus))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

package io.testerra.test.pretest_status;

import eu.tsystems.mms.tic.testframework.testing.AssertProvider;
import eu.tsystems.mms.tic.testframework.testing.TesterraTest;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Optional;
import org.testng.annotations.Test;

public class DataProviderTest extends TesterraTest {
public class DataProviderTest extends TesterraTest implements AssertProvider {

/**
* This test uses an external data provider throwing an exception
Expand Down Expand Up @@ -61,4 +63,29 @@ public Object[][] dataProviderThrowingAssertion() {
public void testT03_AssertFailedDataProvider(String dp) throws Exception {
}

@DataProvider
public Object[][] dataProviderSimple() {
String[][] objects = {{"passed"}, {"failed"}};
return objects;
}

/**
* This tests uses a default dataprovider with 2 elements, 1 passed, 1 failed.
*/
@Test(dataProvider = "dataProviderSimple")
public void testT04_DataProviderWithFailedTests(String dp) {
Assert.assertEquals(dp, "passed");
}

/**
* This tests uses a default dataprovider with 2 elements.
* Failed assertion is only optional (also passed)
*/
@Test(dataProvider = "dataProviderSimple")
public void testT05_DataProviderWithFailedTestsOptional(String dp) {
CONTROL.optionalAssertions(() -> {
ASSERT.assertEquals(dp, "passed");
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public static Object[][] provideExpectedStatusPerMethod() {
{"test_validExpectedFailed_withMethod", Status.FAILED_EXPECTED},
{"test_invalidExpectedFailed_withMethod", Status.FAILED},
{"test_validExpectedFailed_withClass", Status.FAILED_EXPECTED},
{"test_invalidExpectedFailed_withClass", Status.FAILED}
{"test_invalidExpectedFailed_withClass", Status.FAILED},
{"testT04_DataProviderWithFailedTests", Status.FAILED},
{"testT04_DataProviderWithFailedTests", Status.PASSED},
{"testT05_DataProviderWithFailedTestsOptional", Status.PASSED}
};
}

Expand Down Expand Up @@ -95,13 +98,13 @@ public static Object[][] provideFinalTestResult() {
{"*** Stats: SuiteContexts: 2", "SuiteContext"},
{"*** Stats: TestContexts: 2", "TestContext"},
{"*** Stats: ClassContexts: 5", "ClassContext"},
{"*** Stats: MethodContexts: 43", "MethodContexts"},
{"*** Stats: Test Methods Count: 40 (30 relevant)", "Test methods"},
{"*** Stats: Failed: 7", "Failed tests"},
{"*** Stats: MethodContexts: 47", "MethodContexts"},
{"*** Stats: Test Methods Count: 44 (34 relevant)", "Test methods"},
{"*** Stats: Failed: 8", "Failed tests"},
Zsar marked this conversation as resolved.
Show resolved Hide resolved
{"*** Stats: Retried: 10", "Retried tests"},
{"*** Stats: Expected Failed: 6", "Expected failed tests"},
{"*** Stats: Skipped: 5", "Skipped tests"},
{"*** Stats: Passed: 12 ⊃ Recovered: 4 ⊃ Repaired: 1", "Passed tests"}
{"*** Stats: Passed: 15 ⊃ Recovered: 4 ⊃ Repaired: 1", "Passed tests"}
};
}

Expand Down