Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,29 @@
package com.baeldung.testng;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.Arrays;

import static org.testng.Assert.assertEquals;

public class DataDrivenNameTest {

@BeforeMethod
public void capture(Method method, Object[] params) {
String testName = method.getName() + Arrays.toString(params);
Comment thread
LeoColman marked this conversation as resolved.
System.out.println("→ " + testName);
}

@Test(dataProvider = "numbers")
public void shouldSquare(int input, int expected) {
Comment thread
LeoColman marked this conversation as resolved.
Outdated
assertEquals(Math.pow(input, 2), expected);
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@DataProvider
public Object[][] numbers() {
return new Object[][]{{2, 4}, {3, 9}};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.testng;

import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class FactoryNameTest {
private final String instanceLabel;

public FactoryNameTest(String label) {
Comment thread
LeoColman marked this conversation as resolved.
Outdated
this.instanceLabel = label;
}

@Factory
public static Object[] build() {
return new Object[]{new FactoryNameTest("fast-path"), new FactoryNameTest("slow-path")};
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@BeforeMethod
public void capture(Method method, ITestResult result) {
String fullName = method.getName() + "[" + instanceLabel + "]";
result.setAttribute("displayName", fullName);
System.out.println("→ " + fullName);
}

@Test
public void executeScenario() { /* … */ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.testng;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class NameByReflectionTest {

@BeforeMethod
public void capture(Method method) {
String testName = method.getName();
System.out.printf("→ About to run %s%n", testName);
}

@Test
public void shouldPersistEntity() { /* … */ }
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.testng;

import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;

public class NameByResultTest {

@BeforeMethod
public void capture(ITestResult result) {
String testName = result.getMethod().getMethodName();
long startTime = result.getStartMillis();
log(testName, startTime);
}

public void log(String testName, long startTime) {

}
Comment thread
LeoColman marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.testng;

import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NameByXmlSuite {

@BeforeTest
public void xmlName(ITestContext ctx) {
System.out.println("XML test: " + ctx.getName());
}

@Test
public void foo() {

}
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}