Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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 java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

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

public class DataDrivenNameTest {

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

private String testName;
Comment thread
LeoColman marked this conversation as resolved.

@BeforeMethod
public void capture(Method method, Object[] params) {
String testName = method.getName() + Arrays.toString(params);
Comment thread
LeoColman marked this conversation as resolved.
this.testName = testName;
logger.info("Executing test {}", testName);
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@Test(dataProvider = "numbers")
public void givenInputParameters_WhenFetchingTestName_ThenShouldReturnCorrectTestName(int input, int expected) {
Comment thread
LeoColman marked this conversation as resolved.
Outdated
logger.info("Executing scenario from {}", testName);
Assert.assertListContainsObject(List.of("givenInputParameters_WhenFetchingTestName_ThenShouldReturnCorrectTestName[2, 4]", "givenInputParameters_WhenFetchingTestName_ThenShouldReturnCorrectTestName[3, 9]"), testName, "Test name is not as 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,40 @@
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;
import java.util.List;

public class FactoryNameTest {

private static final Logger logger = LoggerFactory.getLogger(FactoryNameTest.class);
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
theangrydev marked this conversation as resolved.
}

@BeforeMethod
public void capture(Method method, ITestResult result) {
String fullName = method.getName() + "[" + instanceLabel + "]";
result.setAttribute("displayName", fullName);
logger.info("capturing {}", fullName);
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@Test
public void givenTestNameSetup_WhenTestNameIsRequested_ThenShouldReturnTestName() {
logger.info("Executing scenario {}", instanceLabel);
Assert.assertListContainsObject(List.of("fast-path", "slow-path"), instanceLabel, "instance label is not");
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}
}
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);
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@Test
public void givenTestNameSetup_WhenTestNameIsRequested_ThenShouldReturnTestName() {
logger.info("Executing scenario {}", testName);
Assert.assertEquals(testName, "givenTestNameSetup_WhenTestNameIsRequested_ThenShouldReturnTestName");
}
}
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.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();
this.testName = testName;
logger.info("Starting test {}", testName);
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@Test
public void givenTestNameSetup_WhenTestNameIsRequested_ThenShouldReturnTestName() {
logger.info("Executing scenario {}", testName);
Assert.assertEquals(testName, "givenTestNameSetup_WhenTestNameIsRequested_ThenShouldReturnTestName");
}
Comment thread
LeoColman marked this conversation as resolved.
}
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;
Comment thread
LeoColman marked this conversation as resolved.

@BeforeTest
public void xmlName(ITestContext ctx) {
Comment thread
LeoColman marked this conversation as resolved.
Outdated
iTestContextSuiteName = ctx.getName();
logger.info("Starting test suite: {}", ctx.getName());
Comment thread
LeoColman marked this conversation as resolved.
Outdated
}

@Test
public void givenTestContextSetup_WhenRequestingSuiteName_ThenShouldReturnSuiteName() {
logger.info("Executing scenario from {}", iTestContextSuiteName);
Assert.assertEquals(iTestContextSuiteName, "All Tests");
}
}