Best Testng code snippet using org.testng.junit.JUnitTest.getTestName
getTestName
Using AI Code Generation
1public class TestName {2 public void test1() {3 System.out.println("Test name: " + getTestName());4 }5 public void test2() {6 System.out.println("Test name: " + getTestName());7 }8 public static String getTestName() {9 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();10 for (StackTraceElement stackTraceElement : stackTrace) {11 if (stackTraceElement.getClassName().equals(TestName.class.getName())) {12 return stackTraceElement.getMethodName();13 }14 }15 return "Test name not found";16 }17}
getTestName
Using AI Code Generation
1import org.testng.ITestContext;2import org.testng.ITestResult;3import org.testng.Reporter;4import org.testng.TestListenerAdapter;5import org.testng.junit.JUnitTest;6public class TestNGReporter extends TestListenerAdapter {7 public void onTestStart(ITestResult tr) {8 super.onTestStart(tr);9 Reporter.log("##teamcity[testStarted name='" + getTestName(tr) + "']");10 }11 public void onTestSuccess(ITestResult tr) {12 super.onTestSuccess(tr);13 Reporter.log("##teamcity[testFinished name='" + getTestName(tr) + "']");14 }15 public void onTestFailure(ITestResult tr) {16 super.onTestFailure(tr);17 Reporter.log("##teamcity[testFailed name='" + getTestName(tr) + "' message='"18 + tr.getThrowable().getMessage() + "' details='"19 + tr.getThrowable().getMessage() + "']");20 Reporter.log("##teamcity[testFinished name='" + getTestName(tr) + "']");21 }22 public void onTestSkipped(ITestResult tr) {23 super.onTestSkipped(tr);24 Reporter.log("##teamcity[testIgnored name='" + getTestName(tr) + "' message='"25 + tr.getThrowable().getMessage() + "']");26 }27 public void onFinish(ITestContext testContext) {28 super.onFinish(testContext);29 Reporter.log("##teamcity[testSuiteFinished name='" + testContext.getName() + "']");30 }31 public void onStart(ITestContext testContext) {32 super.onStart(testContext);33 Reporter.log("##teamcity[testSuiteStarted name='" + testContext.getName() + "']");34 }35 private String getTestName(ITestResult tr) {36 JUnitTest test = (JUnitTest) tr.getInstance();37 String testName = test.getTestName();38 return testName;39 }40}
getTestName
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.IHookCallBack;3import org.testng.IHookable;4import org.testng.ITestResult;5import org.testng.annotations.AfterMethod;6import org.testng.annotations.BeforeMethod;7import org.testng.annotations.BeforeSuite;8import org.testng.annotations.BeforeTest;9import org.testng.ITestContext;10import org.testng.ITestListener;11import org.testng.ITestNGMethod;12import org.testng.ITestResult;13import org.testng.annotations.AfterMethod;14import org.testng.annotations.AfterSuite;15import org.testng.annotations.AfterTest;16import org.testng.annotations.BeforeClass;17import org.testng.annotations.AfterClass;18import org.testng.annotations.BeforeMethod;19import org.testng.annotations.DataProvider;20import org.testng.annotations.Factory;21import org.testng.annotations.Listeners;22import org.testng.annotations.Parameters;23import org.testng.annotations.Test;24import org.testng.internal.annotations.ITest;25import org.testng.junit.JUnitTest;26import org.testng.xml.XmlTest;27import java.io.File;28import java.io.FileNotFoundException;29import java.io.FileOutputStream;30import java.io.IOException;31import java.io.PrintStream;32import java.lang.reflect.Method;33import java.nio.file.Files;34import java.nio.file.Paths;35import java.util.Arrays;36import java.util.Collection;37import java.util.List;38import java.util.Map;39import java.util.Set;40import java.util.logging.Level;41import java.util.logging.Logger;42import java.util.stream.Collectors;43import java.util.stream.Stream;44import org.testng.Assert;45import org.testng.ITestContext;46import org.testng.ITestListener;47import org.testng.ITestResult;48import org.testng.annotations.AfterMethod;49import org.testng.annotations.BeforeMethod;50import org.testng.annotations.BeforeSuite;51import org.testng.annotations.BeforeTest;52import org.testng.annotations.DataProvider;53import org.testng.annotations.Listeners;54import org.testng.annotations.Parameters;55import org.testng.annotations.Test;56import com.google.common.collect.Lists;57public class TestNGTest implements ITestListener, IHookable {58 private String testMethodName;59 private PrintStream out;60 private PrintStream err;61 private String testOutputDir;62 private String testOutputFilename;63 private String testOutputFilePath;64 private String testOutput;65 private String testOutputFilenameTxt;66 private String testOutputFilePathTxt;67 public void beforeSuite(ITestContext context) {
getTestName
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.Assert;3import org.testng.annotations.DataProvider;4import org.testng.annotations.Parameters;5import java.io.*;6import org.testng.ITestResult;7import org.testng.Reporter;8import org.testng.annotations.AfterMethod;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.BeforeTest;11import org.testng.annotations.AfterTest;12import org.testng.annotations.BeforeSuite;13import org.testng.annotations.AfterSuite;14public class NewTest {15 public void f() {16 System.out.println("test name is " + getTestName());17 Assert.assertEquals(1, 2);18 }19 public String getTestName()20 {21 String testName = "";22 {23 File file = new File("TestName.txt");24 file.createNewFile();25 FileWriter writer = new FileWriter(file);26 writer.write(TestName());27 writer.close();28 FileReader reader = new FileReader(file);29 BufferedReader bufferedReader = new BufferedReader(reader);30 testName = bufferedReader.readLine();31 reader.close();32 file.delete();33 }34 catch(Exception e)35 {36 System.out.println(e.getMessage());37 }38 return testName;39 }40 public String TestName()41 {42 String testName = "";43 {44 String className = this.getClass().getName();45 String testngXml = System.getProperty("testngXml");46 File file = new File(testngXml);47 FileReader reader = new FileReader(file);48 BufferedReader bufferedReader = new BufferedReader(reader);49 String line;50 while((line = bufferedReader.readLine()) != null)51 {52 if(line.contains("class-name=") && line.contains(className))53 {54 int index = line.indexOf("name=");55 testName = line.substring(index+6, line.length()-2);56 break;57 }58 }59 reader.close();60 }61 catch(Exception e)62 {63 System.out.println(e.getMessage());64 }65 return testName;
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.