-
Notifications
You must be signed in to change notification settings - Fork 53.8k
[Bael-7734] Add Examples #19143
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
base: master
Are you sure you want to change the base?
[Bael-7734] Add Examples #19143
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
LeoColman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.info("Executing scenario from {}", testName); | ||
| assertEquals(Math.pow(input, 2), expected); | ||
LeoColman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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")}; | ||
LeoColman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
LeoColman marked this conversation as resolved.
Show resolved
Hide 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; | ||
|
|
||
| @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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.