Skip to content
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
@@ -0,0 +1,37 @@
package com.baeldung.testng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {

private static final Logger logger = LoggerFactory.getLogger(DataDrivenNameTest.class);

private String testName;

@BeforeMethod
public void capture(Method method, Object[] params) {
String testName = method.getName() + Arrays.toString(params);
this.testName = testName;
logger.info("Executing test {}", testName);
}

@Test(dataProvider = "numbers")
public void shouldSquare(int input, int expected) {
logger.info("Executing scenario from {}", testName);
assertEquals(Math.pow(input, 2), expected);
}

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
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 static final Logger logger = LoggerFactory.getLogger(FactoryNameTest.class);
private final String instanceLabel;

public FactoryNameTest(String label) {
this.instanceLabel = label;
}

@Factory
public static Object[] build() {
return new Object[]{new FactoryNameTest("fast-path"), new FactoryNameTest("slow-path")};
}

@BeforeMethod
public void capture(Method method, ITestResult result) {
String fullName = method.getName() + "[" + instanceLabel + "]";
result.setAttribute("displayName", fullName);
logger.info("capturing {}", fullName);
}

@Test
public void givenSimpleAssertionThenShouldPass() {
logger.info("Executing scenario {}", instanceLabel);
Assert.assertEquals(4, 2 + 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.testng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class NameByReflectionTest {

private static final Logger logger = LoggerFactory.getLogger(NameByReflectionTest.class);
private String testName;

@BeforeMethod
public void capture(Method method) {
String testName = method.getName();
this.testName = testName;
logger.info("Method name: {}", testName);
}

@Test
public void givenSimpleAssertionThenShouldPass() {
logger.info("Executing scenario {}", testName);
Assert.assertEquals(4, 2 + 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.testng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class NameByResultTest {

private static final Logger logger = LoggerFactory.getLogger(NameByResultTest.class);
private String testName;

@BeforeMethod
public void capture(ITestResult result) {
String testName = result.getMethod().getMethodName();
long startTime = result.getStartMillis();
this.testName = testName;
logger.info("Starting test {} at {}", testName, startTime);
Comment on lines +18 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the purpose of capturing the test start time, is it relevant to this article?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point is showing off more attributes that can be obtained from the Result, such as the start time. It's not completely necessary

}

@Test
public void givenSimpleAssertionThenShouldPass() {
logger.info("Executing scenario {}", testName);
Assert.assertEquals(4, 2 + 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.testng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NameByXmlSuiteTest {

private static final Logger logger = LoggerFactory.getLogger(NameByXmlSuiteTest.class);

private String iTestContextSuiteName;

@BeforeTest
public void xmlName(ITestContext ctx) {
iTestContextSuiteName = ctx.getName();
logger.info("Starting test suite: {}", ctx.getName());
}

@Test
public void givenSimpleAssertionThenShouldPass() {
logger.info("Executing scenario from {}", iTestContextSuiteName);
Assert.assertEquals(4, 2 + 2);
}
}