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

TestNG binder - added ability to provide "pretty" test names #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -65,7 +65,7 @@ public abstract class AbstractDifidoReporter implements Reporter {
private int totalPlannedTests = 0;

private DifidoConfig config;

public AbstractDifidoReporter() {
config = new DifidoConfig();
}
Expand Down Expand Up @@ -149,24 +149,37 @@ private static String getMachineName() {

protected abstract void writeExecution(Execution execution);



@Override
public void onTestStart(ITestResult result) {

if (!result.getTestClass().getName().equals(testClassName)) {
testClassName = result.getTestClass().getName();
startClassScenario(result);
}
String testName = result.getName();

String prettyTestName = extractPrettyTestNameFromTestParameters(result);
List<String> testParameters = getTestParameters(result);
if (!testParameters.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append(testName);
sb.append("(");
String tempString = testParameters.toString().replaceFirst("\\[", "");
sb.append(tempString.substring(0, tempString.length() - 1));
sb.append(")");
testName = sb.toString();

String testName;
if (prettyTestName != null) {
testName = prettyTestName;
}

else {
testName = result.getName();

if (!testParameters.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append(testName);
sb.append("(");
String tempString = testParameters.toString().replaceFirst("\\[", "");
sb.append(tempString.substring(0, tempString.length() - 1));
sb.append(")");
testName = sb.toString();
}
}

currentTest = new TestNode(index++, testName, executionUid + "-" + index);
currentTest.setClassName(testClassName);
final Date date = new Date(result.getStartMillis());
Expand Down Expand Up @@ -206,6 +219,21 @@ private List<String> getTestParameters(ITestResult result) {
return testParameters;
}

private String extractPrettyTestNameFromTestParameters(ITestResult result) {

Object[] params = result.getParameters();

if (params != null) {
for (Object param : params) {
if (param.toString().contains("prettyTestName=")) {
return param.toString().replaceAll("\"", "").replaceAll("prettyTestName=", "");
}
}
}

return null;
}

private void startClassScenario(ITestResult result) {
ScenarioNode scenario = new ScenarioNode(testClassName);
currentTestScenario.addChild(scenario);
Expand Down Expand Up @@ -313,6 +341,7 @@ public void onStart(ITestContext context) {
*/
@Override
public void onStart(ISuite suite) {

execution = null;
totalPlannedTests = getAllPlannedTestsCount(suite);
updateIndex();
Expand Down Expand Up @@ -455,5 +484,4 @@ protected TestDetails getTestDetails() {
protected Execution getExecution() {
return execution;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ public class ConsoleReporter implements Reporter {

private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss:");
private boolean testStarted = false;

private String testName;

@Override
public void onTestStart(ITestResult result) {
if (!testStarted) {

String prettyTestName = extractPrettyTestNameFromTestParameters(result);
if (prettyTestName != null) {
testName = prettyTestName + " (" + result.getName() + ")";
}
else {
testName = result.getName();
}

print("------------------------------------------------------------------------");
print("[TEST START]: " + result.getName());
print("[TEST START]: " + testName);
print("------------------------------------------------------------------------");
testStarted = true;
}
Expand All @@ -34,9 +44,9 @@ public void onTestSuccess(ITestResult result) {
if (testStarted) {
long testDuration = result.getEndMillis() - result.getStartMillis();
print("------------------------------------------------------------------------");
print("[TEST END]: " + result.getName());
print("duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
print("status: success");
print("[TEST END]: " + testName);
print("Duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
print("Status: SUCCESS");
print("------------------------------------------------------------------------");
testStarted = false;
}
Expand All @@ -47,9 +57,9 @@ public void onTestFailure(ITestResult result) {
if (testStarted) {
long testDuration = result.getEndMillis() - result.getStartMillis();
print("------------------------------------------------------------------------");
print("[TEST END]: " + result.getName());
print("duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
print("status: failure");
print("[TEST END]: " + testName);
print("Duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
print("Status: FAILURE");
print("------------------------------------------------------------------------");
testStarted = false;
}
Expand All @@ -58,7 +68,7 @@ public void onTestFailure(ITestResult result) {
@Override
public void onTestSkipped(ITestResult result) {
print("------------------------------------------------------------------------");
print("[TEST SKIPPED]: " + result.getName());
print("[TEST SKIPPED]: " + testName);
print("------------------------------------------------------------------------");
}

Expand Down Expand Up @@ -157,6 +167,21 @@ public File getCurrentTestFolder() {
return null;
}

private String extractPrettyTestNameFromTestParameters(ITestResult result) {

Object[] params = result.getParameters();

if (params != null) {
for (Object param : params) {
if (param.toString().contains("prettyTestName=")) {
return param.toString().replaceAll("\"", "").replaceAll("prettyTestName=", "");
}
}
}

return null;
}

private void print(String message) {
System.out.println(TIME_FORMAT.format(System.currentTimeMillis()) + " " + message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package il.co.topq.difido;

import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestsWithPrettyNames extends AbstractDifidoTestCase {

@Test
public void testWithNoPrettyNameAndNoParams() {

report.log("This is printed from a test with no pretty name");

}

@Test
@Parameters({"someParam"})
public void testWithParamAndNoPrettyName(@Optional("some_param_value") String someParam) {

report.log("This is printed from a test with no pretty name and with some param = " + someParam);

}

@Test
@Parameters("a") // parameter names for different tests in the same class must be different because otherwise the parameter value is not being updated between tests
public void testWithPrettyName1(@Optional("prettyTestName=Test With Pretty Name - No. 1") String prettyTestName) {

report.log("This is printed from test No. 1 with a pretty name");

}

@Test
@Parameters("b") // parameter names for different tests in the same class must be different because otherwise the parameter value is not being updated between tests
public void testWithPrettyName2(@Optional("prettyTestName=Test With Pretty Name - No. 2") String prettyTestName) {

report.log("This is printed from test No. 2 with a pretty name");

}
}