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

normalize test listeners across junit4,5 and groovy cli #457

Merged
merged 1 commit into from Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
com.twosigma.webtau.browser.reporter.ScreenshotStepReporter
@@ -0,0 +1,40 @@
package com.twosigma.webtau.reporter;

import com.twosigma.webtau.console.ConsoleOutputs;
import com.twosigma.webtau.console.ansi.Color;

public class ConsoleTestListener implements TestListener {
@Override
public void beforeFirstTest() {
}

@Override
public void afterAllTests(WebTauReport report) {
}

@Override
public void beforeTestRun(WebTauTest test) {
ConsoleOutputs.out(Color.BLUE, test.getScenario().trim(), " ",
Color.PURPLE, "(" + test.getShortContainerId() + ")");
}

@Override
public void afterTestRun(WebTauTest test) {
if (test.isFailed()) {
outAfter(Color.RED, "[x]", test);
} else if (test.hasError()) {
outAfter(Color.RED, "[~]", test);
} else if (test.isSkipped()) {
outAfter(Color.YELLOW, "[o]", test);
} else {
outAfter(Color.GREEN, "[.]", test);
}

ConsoleOutputs.out();
}

private static void outAfter(Color color, String icon, WebTauTest test) {
ConsoleOutputs.out(color, icon, ' ', Color.BLUE, test.getScenario().trim(),
' ', Color.PURPLE, '(' + test.getShortContainerId() + ')');
}
}
@@ -0,0 +1,15 @@
package com.twosigma.webtau.reporter;

/**
* Lowest common denominator for Groovy Standalone Tests, JUnit4, JUnit5, etc
* Core usage is to print current test name
*/
public interface TestListener {
void beforeFirstTest();

void beforeTestRun(WebTauTest test);

void afterTestRun(WebTauTest test);

void afterAllTests(WebTauReport report);
}
@@ -0,0 +1,36 @@
package com.twosigma.webtau.reporter;

import com.twosigma.webtau.utils.ServiceLoaderUtils;

import java.util.List;

public class TestListeners {
private static List<TestListener> listeners = ServiceLoaderUtils.load(TestListener.class);

private TestListeners() {
}

public static void beforeTestRun(WebTauTest test) {
listeners.forEach(listener -> listener.beforeTestRun(test));
}

public static void afterTestRun(WebTauTest test) {
listeners.forEach(listener -> listener.afterTestRun(test));
}

public static void beforeFirstTest() {
listeners.forEach(TestListener::beforeFirstTest);
}

public static void afterAllTests(WebTauReport report) {
listeners.forEach(listeners -> listeners.afterAllTests(report));
}

public static void add(TestListener listener) {
listeners.add(listener);
}

public static void remove(TestListener listener) {
listeners.remove(listener);
}
}
@@ -0,0 +1,23 @@
package com.twosigma.webtau.reporter;

/**
* Extracts {@link TestStep} payloads from all test steps at the end of the test and re-attaches it as a test payload.
*/
public class TestResultPayloadExtractorTestListener implements TestListener {
@Override
public void beforeFirstTest() {
}

@Override
public void beforeTestRun(WebTauTest test) {
}

@Override
public void afterTestRun(WebTauTest test) {
TestResultPayloadExtractors.extract(test.getSteps().stream()).forEach(test::addTestResultPayload);
}

@Override
public void afterAllTests(WebTauReport report) {
}
}
Expand Up @@ -20,7 +20,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down
Expand Up @@ -14,34 +14,48 @@
* limitations under the License.
*/

package com.twosigma.webtau.report;
package com.twosigma.webtau.reporter;

import com.twosigma.webtau.reporter.TestStatus;

import java.util.LinkedHashMap;
import java.util.Map;
public class WebTauReport {
private final long startTime;
private final long stopTime;
private final WebTauTestList tests;

public class ReportSummary {
private final long total;
private final long passed;
private final long failed;
private final long skipped;
private final long errored;

private final long startTime;
private final long stopTime;
private final long duration;

ReportSummary(ReportTestEntries testEntries, long startTime, long stopTime) {
total = testEntries.size();
passed = testEntries.countWithStatus(TestStatus.Passed);
failed = testEntries.countWithStatus(TestStatus.Failed);
skipped = testEntries.countWithStatus(TestStatus.Skipped);
errored = testEntries.countWithStatus(TestStatus.Errored);

public WebTauReport(WebTauTestList tests, long startTime, long stopTime) {
this.startTime = startTime;
this.stopTime = stopTime;
this.duration = stopTime - startTime;
this.tests = tests;

duration = stopTime - startTime;

total = tests.size();
passed = tests.countWithStatus(TestStatus.Passed);
failed = tests.countWithStatus(TestStatus.Failed);
skipped = tests.countWithStatus(TestStatus.Skipped);
errored = tests.countWithStatus(TestStatus.Errored);
}

public WebTauTestList getTests() {
return tests;
}

public long getStartTime() {
return startTime;
}

public long getStopTime() {
return stopTime;
}

public long getDuration() {
return duration;
}

public long getTotal() {
Expand All @@ -63,30 +77,4 @@ public long getSkipped() {
public long getErrored() {
return errored;
}

public long getStartTime() {
return startTime;
}

public long getStopTime() {
return stopTime;
}

public long getDuration() {
return duration;
}

public Map<String, ?> toMap() {
Map<String, Object> result = new LinkedHashMap<>();
result.put("total", total);
result.put("passed", passed);
result.put("failed", failed);
result.put("skipped", skipped);
result.put("errored", errored);
result.put("startTime", startTime);
result.put("stopTime", stopTime);
result.put("duration", duration);

return result;
}
}
Expand Up @@ -14,11 +14,8 @@
* limitations under the License.
*/

package com.twosigma.webtau.report;
package com.twosigma.webtau.reporter;

import com.twosigma.webtau.reporter.TestResultPayload;
import com.twosigma.webtau.reporter.TestStatus;
import com.twosigma.webtau.reporter.TestStep;
import com.twosigma.webtau.reporter.stacktrace.StackTraceCodeEntry;
import com.twosigma.webtau.reporter.stacktrace.StackTraceUtils;
import com.twosigma.webtau.time.Time;
Expand All @@ -32,13 +29,9 @@
import java.util.Map;
import java.util.stream.Collectors;

import static com.twosigma.webtau.reporter.TestStatus.Errored;
import static com.twosigma.webtau.reporter.TestStatus.Failed;
import static com.twosigma.webtau.reporter.TestStatus.Passed;
import static com.twosigma.webtau.reporter.TestStatus.Skipped;
import static com.twosigma.webtau.reporter.TestStatus.*;


public class ReportTestEntry {
public class WebTauTest {
private String id;
private String scenario;

Expand All @@ -60,7 +53,7 @@ public class ReportTestEntry {
private long startTime;
private long elapsedTime;

public ReportTestEntry(Path workingDir) {
public WebTauTest(Path workingDir) {
this.workingDir = workingDir;
payloads = new ArrayList<>();
steps = new ArrayList<>();
Expand Down
Expand Up @@ -14,49 +14,47 @@
* limitations under the License.
*/

package com.twosigma.webtau.report;

import com.twosigma.webtau.reporter.TestStatus;
package com.twosigma.webtau.reporter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class ReportTestEntries {
private List<ReportTestEntry> entries;
public class WebTauTestList {
private List<WebTauTest> tests;

public ReportTestEntries() {
entries = Collections.synchronizedList(new ArrayList<>());
public WebTauTestList() {
tests = Collections.synchronizedList(new ArrayList<>());
}

public ReportTestEntries(List<ReportTestEntry> entries) {
this.entries = Collections.synchronizedList(entries);
public WebTauTestList(List<WebTauTest> tests) {
this.tests = Collections.synchronizedList(tests);
}

public void add(ReportTestEntry entry) {
entries.add(entry);
public void add(WebTauTest test) {
tests.add(test);
}

public void forEach(Consumer<ReportTestEntry> action) {
entries.forEach(action);
public void forEach(Consumer<WebTauTest> action) {
tests.forEach(action);
}

public Stream<ReportTestEntry> stream() {
return entries.stream();
public Stream<WebTauTest> stream() {
return tests.stream();
}

public int size() {
return entries.size();
return tests.size();
}

public boolean isEmpty() {
return entries.isEmpty();
return tests.isEmpty();
}

public Stream<ReportTestEntry> withStatus(TestStatus status) {
return entries.stream().filter(e -> e.getTestStatus() == status);
public Stream<WebTauTest> withStatus(TestStatus status) {
return tests.stream().filter(e -> e.getTestStatus() == status);
}

public long countWithStatus(TestStatus status) {
Expand Down
@@ -0,0 +1 @@
com.twosigma.webtau.reporter.TestResultPayloadExtractorTestListener
Expand Up @@ -2,15 +2,15 @@ package scenarios.rest.report

import com.twosigma.webtau.console.ConsoleOutputs
import com.twosigma.webtau.console.ansi.Color
import com.twosigma.webtau.report.ReportTestEntries
import com.twosigma.webtau.reporter.WebTauReport

import static com.twosigma.webtau.WebTauDsl.cfg

class Report {
static void generateReport(ReportTestEntries entries) {
static void generateReport(WebTauReport report) {
def reportPath = cfg.workingDir.resolve('report.txt')

ConsoleOutputs.out('generating report: ', Color.PURPLE, reportPath)
reportPath.toFile().text = entries.size()
reportPath.toFile().text = report.tests.size()
}
}