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

Add passing tests and test duration #80

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ public class BuildData {
private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());
public static class TestData {
private int totalCount, skipCount, failCount, passCount;
private List<FailedTest> failedTestsWithErrorDetail;
private List<ExecutedTest> failedTestsWithErrorDetail;
private List<String> failedTests;

public static class FailedTest {
private List<ExecutedTest> passedTestsWithErrorDetail;
private List<String> passedTests;

public static class ExecutedTest {
private final String fullName, errorDetails;
public FailedTest(String fullName, String errorDetails) {
private final float duration;
public ExecutedTest(String fullName, String errorDetails, float duration) {
super();
this.fullName = fullName;
this.errorDetails = errorDetails;
this.duration = duration;
}

public String getFullName()
Expand Down Expand Up @@ -103,6 +108,7 @@ public TestData(Action action) {
totalCount = skipCount = failCount = 0;
failedTests = Collections.emptyList();
failedTestsWithErrorDetail = Collections.emptyList();
passedTestsWithErrorDetail = Collections.emptyList();
return;
}

Expand All @@ -112,10 +118,18 @@ public TestData(Action action) {
passCount = totalCount - skipCount - failCount;

failedTests = new ArrayList<String>();
failedTestsWithErrorDetail = new ArrayList<FailedTest>();
for (TestResult result : testResultAction.getFailedTests()) {
failedTests.add(result.getFullName());
failedTestsWithErrorDetail.add(new FailedTest(result.getFullName(),result.getErrorDetails()));
failedTestsWithErrorDetail = new ArrayList<ExecutedTest>();
testListFill((List<TestResult>) testResultAction.getFailedTests(), failedTests, failedTestsWithErrorDetail);

passedTests = new ArrayList<String>();
passedTestsWithErrorDetail = new ArrayList<ExecutedTest>();
testListFill((List<TestResult>) testResultAction.getPassedTests(), passedTests, passedTestsWithErrorDetail);
Copy link
Author

Choose a reason for hiding this comment

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

Is there a smarter way to invoke this function than an unchecked cast?

}

private void testListFill(List<TestResult> testResults, List<String> testNames, List<ExecutedTest> testDetails) {

Choose a reason for hiding this comment

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

Make the signature of the method
List<? extends TestResult> testResults, List<String> testNames, ...
Then you don't need the type cast when you call the method

for (TestResult result : testResults) {
testNames.add(result.getFullName());
testDetails.add(new ExecutedTest(result.getFullName(),result.getErrorDetails(), result.getDuration()));
}
}

Expand All @@ -139,11 +153,21 @@ public int getPassCount()
return passCount;
}

public List<FailedTest> getFailedTestsWithErrorDetail()
public List<ExecutedTest> getFailedTestsWithDetail()
{
return failedTestsWithErrorDetail;
}

public List<ExecutedTest> getPassedTestsWithDetail()
{
return passedTestsWithErrorDetail;
}

public List<String> getPassedTests()
{
return passedTests;
}

public List<String> getFailedTests()
{
return failedTests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void before() throws Exception {
when(mockTestResultAction.getTotalCount()).thenReturn(0);
when(mockTestResultAction.getSkipCount()).thenReturn(0);
when(mockTestResultAction.getFailCount()).thenReturn(0);
when(mockTestResultAction.getPassedTests()).thenReturn(Collections.emptyList());
when(mockTestResultAction.getFailedTests()).thenReturn(Collections.emptyList());

when(mockProject.getName()).thenReturn("LogstashWriterTest");
Expand Down Expand Up @@ -182,6 +183,7 @@ public void constructorSuccess() throws Exception {
verify(mockTestResultAction).getSkipCount();
verify(mockTestResultAction).getFailCount();
verify(mockTestResultAction, times(1)).getFailedTests();
verify(mockTestResultAction, times(1)).getPassedTests();

verify(mockProject, times(2)).getName();
verify(mockProject, times(2)).getFullName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class BuildDataTest {
+ "\"rootProjectName\":\"RootBuildDataTest\",\"rootFullProjectName\":\"parent/RootBuildDataTest\","
+ "\"rootProjectDisplayName\":\"Root BuildData Test\",\"rootBuildNum\":456,\"buildVariables\":{},"
+ "\"sensitiveBuildVariables\":[],\"testResults\":{\"totalCount\":0,\"skipCount\":0,\"failCount\":0, \"passCount\":0,"
+ "\"passedTests\":[], \"passedTestsWithDetail\":[],"
+ "\"failedTests\":[], \"failedTestsWithErrorDetail\":[]}}";

@Mock AbstractBuild mockBuild;
Expand Down Expand Up @@ -99,6 +100,7 @@ public void before() throws Exception {
when(mockTestResultAction.getTotalCount()).thenReturn(0);
when(mockTestResultAction.getSkipCount()).thenReturn(0);
when(mockTestResultAction.getFailCount()).thenReturn(0);
when(mockTestResultAction.getPassedTests()).thenReturn(Collections.emptyList());
when(mockTestResultAction.getFailedTests()).thenReturn(Collections.emptyList());

when(mockProject.getName()).thenReturn("BuildDataTest");
Expand Down Expand Up @@ -166,6 +168,7 @@ private void verifyTestResultActions() {
verify(mockTestResultAction).getTotalCount();
verify(mockTestResultAction).getSkipCount();
verify(mockTestResultAction).getFailCount();
verify(mockTestResultAction, times(1)).getPassedTests();
verify(mockTestResultAction, times(1)).getFailedTests();
}

Expand Down Expand Up @@ -247,6 +250,7 @@ public void constructorSuccessTestFailures() throws Exception {
when(mockTestResultAction.getSkipCount()).thenReturn(0);
when(mockTestResultAction.getFailCount()).thenReturn(1);
when(mockTestResultAction.getFailedTests()).thenReturn(Arrays.asList(mockTestResult));
when(mockTestResultAction.getPassedTests()).thenReturn(Arrays.asList(mockTestResult));

// Unit under test
BuildData buildData = new BuildData(mockBuild, mockDate, mockListener);
Expand All @@ -256,9 +260,40 @@ public void constructorSuccessTestFailures() throws Exception {
Assert.assertEquals("Incorrect test results", 123, testResults.getTotalCount());
Assert.assertEquals("Incorrect test results", 0, testResults.getSkipCount());
Assert.assertEquals("Incorrect test results", 1, testResults.getFailCount());
Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithErrorDetail().size());
Assert.assertEquals("Incorrect failed test error details", "ErrorDetails Test", testResults.getFailedTestsWithErrorDetail().get(0).getErrorDetails());
Assert.assertEquals("Incorrect failed test fullName", "Mock Full Test", testResults.getFailedTestsWithErrorDetail().get(0).getFullName());
Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithDetail().size());
Assert.assertEquals("Incorrect failed test error details", "ErrorDetails Test", testResults.getFailedTestsWithDetail().get(0).getErrorDetails());
Assert.assertEquals("Incorrect failed test fullName", "Mock Full Test", testResults.getFailedTestsWithDetail().get(0).getFullName());


verifyMocks();
verifyTestResultActions();
}

@Test
public void constructorSuccessTestPasses() throws Exception {
TestResult mockTestResult = Mockito.mock(hudson.tasks.test.TestResult.class);
when(mockTestResult.getFullName()).thenReturn("Mock Full Test");
when(mockTestResult.getErrorDetails()).thenReturn("");

when(mockTestResultAction.getTotalCount()).thenReturn(123);
when(mockTestResultAction.getSkipCount()).thenReturn(0);
when(mockTestResultAction.getFailCount()).thenReturn(0);
when(mockTestResultAction.getFailedTests()).thenReturn(Arrays.asList(mockTestResult));
when(mockTestResultAction.getPassedTests()).thenReturn(Arrays.asList(mockTestResult));

// Unit under test
BuildData buildData = new BuildData(mockBuild, mockDate, mockListener);

TestData testResults = buildData.getTestResults();

Assert.assertEquals("Incorrect test results", 123, testResults.getTotalCount());
Assert.assertEquals("Incorrect test results", 0, testResults.getSkipCount());
Assert.assertEquals("Incorrect test results", 0, testResults.getFailCount());
Assert.assertEquals("Incorrect test results", 123, testResults.getPassCount());
Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithDetail().size());
Assert.assertEquals("Incorrect failed test error details", "", testResults.getPassedTestsWithDetail().get(0).getErrorDetails());
Assert.assertEquals("Incorrect failed test fullName", "Mock Full Test", testResults.getPassedTestsWithDetail().get(0).getFullName());


verifyMocks();
verifyTestResultActions();
Expand Down