Best Testng code snippet using org.testng.xml.Xml.getName
Source:JarFileUtils.java
...62 file.deleteOnExit();63 String suitePath = null;64 while (entries.hasMoreElements()) {65 JarEntry je = entries.nextElement();66 String jeName = je.getName();67 if (Parser.canParse(jeName.toLowerCase())) {68 InputStream inputStream = jf.getInputStream(je);69 File copyFile = new File(file, jeName);70 Files.copyFile(inputStream, copyFile);71 if (matchesXmlPathInJar(je)) {72 suitePath = copyFile.toString();73 }74 } else if (isJavaClass(je)) {75 classes.add(constructClassName(je));76 }77 }78 if (Strings.isNullOrEmpty(suitePath)) {79 return false;80 }81 Collection<XmlSuite> parsedSuites = Parser.parse(suitePath, processor);82 for (XmlSuite suite : parsedSuites) {83 // If test names were specified, only run these test names84 if (testNames != null) {85 TestNamesMatcher testNamesMatcher = new TestNamesMatcher(suite, testNames);86 List<String> missMatchedTestname = testNamesMatcher.getMissMatchedTestNames();87 if (!missMatchedTestname.isEmpty()) {88 throw new TestNGException("The test(s) <" + missMatchedTestname + "> cannot be found.");89 }90 suites.addAll(testNamesMatcher.getSuitesMatchingTestNames());91 } else {92 suites.add(suite);93 }94 return true;95 }96 }97 return false;98 }99 private boolean matchesXmlPathInJar(JarEntry je) {100 return je.getName().equals(xmlPathInJar);101 }102 private static boolean isJavaClass(JarEntry je) {103 return je.getName().endsWith(".class");104 }105 private static String constructClassName(JarEntry je) {106 int n = je.getName().length() - ".class".length();107 return je.getName().replace("/", ".").substring(0, n);108 }109}...
Source:SimpleBaseTest.java
...35 protected TestNG createTests(String suiteName, Class<?>... testClasses) {36 XmlSuite suite = createXmlSuite(suiteName);37 int i=0;38 for (Class<?> testClass : testClasses) {39 createXmlTest(suite, testClass.getName() + i, testClass);40 i++;41 }42 return create(suite);43 }44 protected XmlSuite createXmlSuite(String name) {45 XmlSuite result = new XmlSuite();46 result.setName(name);47 return result;48 }49 protected XmlTest createXmlTest(XmlSuite suite, String name, Class clazz, Class... classes) {50 XmlTest result = new XmlTest(suite);51 int index = 0;52 result.setName(name);53 XmlClass xc = new XmlClass(clazz.getName(), index++, true /* load classes */);54 result.getXmlClasses().add(xc);55 for (Class c : classes) {56 xc = new XmlClass(c.getName(), index++, true /* load classes */);57 result.getXmlClasses().add(xc);58 }59 return result;60 }61 protected XmlTest createXmlTest(XmlSuite suite, String name, String... classes) {62 XmlTest result = new XmlTest(suite);63 int index = 0;64 result.setName(name);65 for (String c : classes) {66 XmlClass xc = new XmlClass(c, index++, true /* load classes */);67 result.getXmlClasses().add(xc);68 }69 return result;70 }71 protected void addMethods(XmlClass cls, String... methods) {72 int index = 0;73 for (String m : methods) {74 XmlInclude include = new XmlInclude(m, index++);75 cls.getIncludedMethods().add(include);76 }77 }78 public static String getPathToResource(String fileName) {79 String result = System.getProperty(TEST_RESOURCES_DIR);80 if (result == null) {81 throw new IllegalArgumentException("System property " + TEST_RESOURCES_DIR + " was not defined.");82 }83 return result + File.separatorChar + fileName;84 }85 protected void verifyPassedTests(TestListenerAdapter tla, String... methodNames) {86 Iterator<ITestResult> it = tla.getPassedTests().iterator();87 Assert.assertEquals(tla.getPassedTests().size(), methodNames.length);88 int i = 0;89 while (it.hasNext()) {90 Assert.assertEquals(it.next().getName(), methodNames[i++]);91 }92 }93 /**94 * Compare a list of ITestResult with a list of String method names,95 */96 public static void assertTestResultsEqual(List<ITestResult> results, List<String> methods) {97 List<String> resultMethods = Lists.newArrayList();98 for (ITestResult r : results) {99 resultMethods.add(r.getMethod().getMethodName());100 }101 Assert.assertEquals(resultMethods, methods);102 }103}...
Source:MethodInstance.java
...49 XmlTest test1 = o1.getMethod().getTestClass().getXmlTest();50 XmlTest test2 = o2.getMethod().getTestClass().getXmlTest();5152 // If the two methods are not in the same <test>, we can't compare them53 if (! test1.getName().equals(test2.getName())) {54 return 0;55 }5657 int result = 0;5859 // If the two methods are in the same <class>, compare them by their method60 // index, otherwise compare them with their class index.61 XmlClass class1 = o1.getMethod().getTestClass().getXmlClass();62 XmlClass class2 = o2.getMethod().getTestClass().getXmlClass();6364 // This can happen if these classes came from a @Factory, in which case, they65 // don't have an associated XmlClass66 if (class1 == null || class2 == null) {67 if (class1 != null) return -1;68 if (class2 != null) return 1;69 return 0;70 }7172 if (! class1.getName().equals(class2.getName())) {73 int index1 = class1.getIndex();74 int index2 = class2.getIndex();75 result = index1 - index2;76 }77 else {78 XmlInclude include1 =79 findXmlInclude(class1.getIncludedMethods(), o1.getMethod().getMethodName());80 XmlInclude include2 =81 findXmlInclude(class2.getIncludedMethods(), o2.getMethod().getMethodName());82 if (include1 != null && include2 != null) {83 result = include1.getIndex() - include2.getIndex();84 }85 }8687 return result;88 }8990 private XmlInclude findXmlInclude(List<XmlInclude> includedMethods, String methodName) {91 for (XmlInclude xi : includedMethods) {92 if (xi.getName().equals(methodName)) {93 return xi;94 }95 }96 return null;97 }98 };99100// public static final Comparator<IMethodInstance> SORT_BY_CLASS101// = new Comparator<IMethodInstance>() {102// public int compare(IMethodInstance o1, IMethodInstance o2) {103// int result= o1.getMethod().getTestClass().getName()104// .compareTo(o2.getMethod().getTestClass().getName());105// return result;106// }107// };108109}
...
Source:CheckSuiteNamesTest.java
...36 String testngXmlPath = getPathToResource("sanitycheck/test-s-a.xml");37 tng.setTestSuites(Arrays.asList(testngXmlPath));38 tng.addListener(tla);39 tng.run();40 Assert.assertEquals(tla.getTestContexts().get(0).getSuite().getName(), "SanityCheck suites");41 Assert.assertEquals(tla.getTestContexts().get(1).getSuite().getName(), "SanityCheck suites");42 Assert.assertEquals(tla.getTestContexts().get(2).getSuite().getName(), "SanityCheck suites (0)");43 Assert.assertEquals(tla.getTestContexts().get(3).getSuite().getName(), "SanityCheck suites (0)");44 }45 /**46 * Checks that suites created programmatically also works as expected47 */48 @Test49 public void checkProgrammaticSuitesFails() {50 XmlSuite xmlSuite1 = new XmlSuite();51 xmlSuite1.setName("SanityCheckSuite");52 {53 XmlTest result = new XmlTest(xmlSuite1);54 result.getXmlClasses().add(new XmlClass(SampleTest1.class.getCanonicalName()));55 }56 XmlSuite xmlSuite2 = new XmlSuite();57 xmlSuite2.setName("SanityCheckSuite");58 {59 XmlTest result = new XmlTest(xmlSuite2);60 result.getXmlClasses().add(new XmlClass(SampleTest2.class.getCanonicalName()));61 }62 TestNG tng = create();63 tng.setXmlSuites(Arrays.asList(xmlSuite1, xmlSuite2));64 tng.run();65 Assert.assertEquals(xmlSuite1.getName(), "SanityCheckSuite");66 Assert.assertEquals(xmlSuite2.getName(), "SanityCheckSuite (0)");67 }68 69 @Test70 public void checkXmlSuiteAddition() throws ParserConfigurationException, SAXException, IOException {71 TestNG tng = create();72 String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");73 Parser parser = new Parser(testngXmlPath); 74 tng.setXmlSuites(parser.parseToList());75 tng.initializeSuitesAndJarFile(); 76 }77}...
Source:TckRunner.java
...42 // Alternatively e.g. use this for running single tests43 // List<XmlClass> classes = Collections.singletonList( new XmlClass( ValidateTest.class ) );44 // test.setXmlClasses( classes );45 XmlMethodSelector selector = new XmlMethodSelector();46 selector.setClassName( IntegrationTestsMethodSelector.class.getName() );47 test.setMethodSelectors( Collections.singletonList( selector ) );48 TestListenerAdapter tla = new TestListenerAdapter();49 TestNG testng = new TestNG();50 testng.setXmlSuites( Collections.singletonList( suite ) );51 testng.addListener( tla );52 testng.run();53 for ( ITestResult failure: tla.getConfigurationFailures() ) {54 System.out.println( "Failure: " + failure.getName() );55 failure.getThrowable().printStackTrace();56 }57 for ( ITestResult result : tla.getFailedTests() ) {58 System.out.println( "Failed:" + result.getName() );59 result.getThrowable().printStackTrace();60 }61 }62}...
Source:ShadowTest.java
...13 public void parametersShouldNotBeShadowed() {14 XmlSuite s = createXmlSuite("s");15 XmlTest t = createXmlTest(s, "t");16 {17 XmlClass c1 = new XmlClass(Shadow1SampleTest.class.getName());18 XmlInclude include1 = new XmlInclude("test1");19 include1.setXmlClass(c1);20 c1.getLocalParameters().put("a", "First");21 c1.getIncludedMethods().add(include1);22 t.getXmlClasses().add(c1);23 }24 {25 XmlClass c2 = new XmlClass(Shadow2SampleTest.class.getName());26 XmlInclude include2 = new XmlInclude("test2");27 include2.setXmlClass(c2);28 c2.getLocalParameters().put("a", "Second");29 c2.getIncludedMethods().add(include2);30 t.getXmlClasses().add(c2);31 }32 TestNG tng = create();33 tng.setXmlSuites(Arrays.asList(s));34 TestListenerAdapter tla = new TestListenerAdapter();35 tng.addListener(tla);36 tng.run();37// System.out.println(s.toXml());38 assertTestResultsEqual(tla.getPassedTests(), Arrays.asList("test1", "test2"));39 }...
Source:Bug90Test.java
...11public class Bug90Test extends SimpleBaseTest {12 @Test(description = "Fix for https://github.com/cbeust/testng/issues/90")13 public void afterClassShouldRun() {14 XmlSuite s = createXmlSuite("Bug90");15 XmlTest t = createXmlTest(s, "Bug90 test", Sample.class.getName());16 XmlClass c = t.getClasses().get(0);17 c.setIncludedMethods(Arrays.asList(new XmlInclude("test1")));18 TestNG tng = create();19 tng.setXmlSuites(Arrays.asList(s));20 Sample.m_afterClassWasRun = false;21 tng.run();22 Assert.assertTrue(Sample.m_afterClassWasRun);23 }24}...
Source:MainTest.java
...11public class MainTest extends SimpleBaseTest {12 @Test13 public void afterClassShouldRun() {14 XmlSuite s = createXmlSuite("S");15 XmlTest t = createXmlTest(s, "T", Main.class.getName());16 XmlClass c = t.getXmlClasses().get(0);17 c.getIncludedMethods().add(new XmlInclude("test1"));18 t.setPreserveOrder("true");19 TestNG tng = create();20 tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s }));21 Main.m_passed = false;22 tng.run();23 Assert.assertTrue(Main.m_passed);24 }25}...
getName
Using AI Code Generation
1XmlSuite suite = new XmlSuite();2suite.setName("Test Suite");3List<XmlSuite> suites = new ArrayList<XmlSuite>();4suites.add(suite);5TestNG tng = new TestNG();6tng.setXmlSuites(suites);7tng.run();8XmlSuite suite = new XmlSuite();9suite.setName("Test Suite");10List<XmlSuite> suites = new ArrayList<XmlSuite>();11suites.add(suite);12TestNG tng = new TestNG();13tng.setXmlSuites(suites);14tng.run();15package com.javatpoint; 16import java.util.*; 17import org.testng.*; 18import org.testng.xml.*; 19public class TestngDemo { 20public static void main(String[] args) { 21 XmlSuite suite=new XmlSuite(); 22 suite.setName("Test Suite"); 23 List<XmlSuite> suites=new ArrayList<XmlSuite>(); 24 suites.add(suite); 25 TestNG tng=new TestNG(); 26 tng.setXmlSuites(suites); 27 tng.run(); 28 XmlSuite suite1=new XmlSuite(); 29 suite1.setName("Test Suite"); 30 List<XmlSuite> suites1=new ArrayList<XmlSuite>(); 31 suites1.add(suite1); 32 TestNG tng1=new TestNG(); 33 tng1.setXmlSuites(suites1); 34 tng1.run(); 35 String suiteName=suite1.getName(); 36 System.out.println(suiteName); 37} 38}
getName
Using AI Code Generation
1public void testGetName() {2 XmlSuite suite = new XmlSuite();3 suite.setName("MySuite");4 String suiteName = suite.getName();5 Assert.assertEquals(suiteName, "MySuite");6}7public void testSetName() {8 XmlSuite suite = new XmlSuite();9 suite.setName("MySuite");10 String suiteName = suite.getName();11 Assert.assertEquals(suiteName, "MySuite");12}13public void testGetTests() {14 XmlSuite suite = new XmlSuite();15 suite.setName("MySuite");16 List<XmlTest> tests = suite.getTests();17 Assert.assertEquals(tests.size(), 0);18}19public void testSetTests() {20 XmlSuite suite = new XmlSuite();21 suite.setName("MySuite");22 List<XmlTest> tests = suite.getTests();23 Assert.assertEquals(tests.size(), 0);24}25public void testGetParameters() {26 XmlSuite suite = new XmlSuite();27 suite.setName("MySuite");28 List<XmlSuite.Parameter> parameters = suite.getParameters();29 Assert.assertEquals(parameters.size(), 0);30}31public void testSetParameters() {32 XmlSuite suite = new XmlSuite();33 suite.setName("MySuite");34 List<XmlSuite.Parameter> parameters = suite.getParameters();35 Assert.assertEquals(parameters.size(), 0);36}37public void testGetGroups() {38 XmlSuite suite = new XmlSuite();39 suite.setName("MySuite");40 List<XmlSuite.Group> groups = suite.getGroups();41 Assert.assertEquals(groups.size(), 0);42}
getName
Using AI Code Generation
1String xmlFileName = testngXml.getName();2List<XmlClass> xmlClassList = testngXml.getClasses();3List<String> includedGroups = testngXml.getIncludedGroups();4List<String> excludedGroups = testngXml.getExcludedGroups();5Map<String, String> parameters = testngXml.getParameters();6String parallel = testngXml.getParallel();7List<String> suiteFiles = testngXml.getSuiteFiles();8List<XmlTest> xmlTestList = testngXml.getTests();9List<XmlPackage> xmlPackageList = testngXml.getXmlPackages();10XmlSuite xmlSuite = testngXml.getXmlSuite();11testngXml.setClasses(xmlClassList);12testngXml.setIncludedGroups(includedGroups);13testngXml.setExcludedGroups(excludedGroups);14testngXml.setParameters(parameters);
getName
Using AI Code Generation
1package com.suiteName;2import org.testng.ITestContext;3import org.testng.ITestListener;4import org.testng.ITestResult;5public class SuiteName implements ITestListener {6 public void onTestStart(ITestResult result) {7 }8 public void onTestSuccess(ITestResult result) {9 }10 public void onTestFailure(ITestResult result) {11 }12 public void onTestSkipped(ITestResult result) {13 }14 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {15 }16 public void onStart(ITestContext context) {17 String suiteName = context.getSuite().getName();18 System.out.println("Suite Name: " + suiteName);19 System.setProperty("suiteName", suiteName);20 }21 public void onFinish(ITestContext context) {22 }23}24package com.suiteName;25import org.testng.ITestContext;26import org.testng.ITestListener;27import org.testng.ITestResult;28public class SuiteName implements ITestListener {29 public void onTestStart(ITestResult result) {30 }31 public void onTestSuccess(ITestResult result) {32 }33 public void onTestFailure(ITestResult result) {34 }35 public void onTestSkipped(ITestResult result) {36 }37 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {38 }39 public void onStart(ITestContext context) {40 String suiteName = context.getSuite().getName();41 System.out.println("Suite Name: " + suiteName);42 System.setProperty("suiteName", suiteName);43 }44 public void onFinish(ITestContext context) {45 }46}
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.
Get 100 minutes of automation test minutes FREE!!