Best Testng code snippet using org.testng.TestNG.getOutputDirectory
Source: FailurePolicyTest.java
...35 @Test( dataProvider = "dp" )36 public void confFailureTest(Class[] classesUnderTest, int configurationFailures, int configurationSkips, int skippedTests) {37 TestListenerAdapter tla = new TestListenerAdapter();38 TestNG testng = new TestNG();39 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());40 testng.setTestClasses(classesUnderTest);41 testng.addListener(tla);42 testng.setVerbose(0);43 testng.setConfigFailurePolicy(XmlSuite.CONTINUE);44 testng.run();45 verify(tla, configurationFailures, configurationSkips, skippedTests);46 }47 @Test48 public void commandLineTest_policyAsSkip() {49 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),50 "-configfailurepolicy", "skip",51 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };52 TestListenerAdapter tla = new TestListenerAdapter();53 TestNG.privateMain(argv, tla);54 verify(tla, 1, 1, 2);55 }56 @Test57 public void commandLineTest_policyAsContinue() {58 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),59 "-configfailurepolicy", "continue",60 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };61 TestListenerAdapter tla = new TestListenerAdapter();62 TestNG.privateMain(argv, tla);63 verify(tla, 2, 0, 2);64 }65 @Test66 public void commandLineTestWithXMLFile_policyAsSkip() {67 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),68 "-configfailurepolicy", "skip", "src/test/resources/testng-configfailure.xml" };69 TestListenerAdapter tla = new TestListenerAdapter();70 TestNG.privateMain(argv, tla);71 verify(tla, 1, 1, 2);72 }73 @Test74 public void commandLineTestWithXMLFile_policyAsContinue() {75 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),76 "-configfailurepolicy", "continue", "src/test/resources/testng-configfailure.xml" };77 TestListenerAdapter tla = new TestListenerAdapter();78 TestNG.privateMain(argv, tla);79 verify(tla, 2, 0, 2);80 }81 private void verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests ) {82 assertEquals(tla.getConfigurationFailures().size(), configurationFailures, "wrong number of configuration failures");83 assertEquals(tla.getConfigurationSkips().size(), configurationSkips, "wrong number of configuration skips");84 assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests");85 }86}...
Source: CommandLineTest.java
...17 @Test(groups = { "current" } )18 public void junitParsing() {19 String[] argv = {20 "-log", "0",21 "-d", OutputDirectoryPatch.getOutputDirectory(),22 "-junit",23 "-testclass", "test.sample.JUnitSample1"24 };25 TestListenerAdapter tla = new TestListenerAdapter();26 TestNG.privateMain(argv, tla);27 List<ITestResult> passed = tla.getPassedTests();28 assertEquals(passed.size(), 2);29 String test1 = passed.get(0).getMethod().getMethodName();30 String test2 = passed.get(1).getMethod().getMethodName();31 assertTrue(JUnitSample1.EXPECTED1.equals(test1) && JUnitSample1.EXPECTED2.equals(test2) ||32 JUnitSample1.EXPECTED1.equals(test2) && JUnitSample1.EXPECTED2.equals(test1));33 }34 /**35 * Test the absence of -junit36 */37 @Test(groups = { "current" } )38 public void junitParsing2() {39 String[] argv = {40 "-log", "0",41 "-d", OutputDirectoryPatch.getOutputDirectory(),42 "-testclass", "test.sample.JUnitSample1"43 };44 TestListenerAdapter tla = new TestListenerAdapter();45 TestNG.privateMain(argv, tla);46 List<ITestResult> passed = tla.getPassedTests();47 assertEquals(passed.size(), 0);48 }49 /**50 * Test the ability to override the default command line Suite name51 */52 @Test(groups = { "current" } )53 public void suiteNameOverride() {54 String suiteName="MySuiteName";55 String[] argv = {56 "-log", "0",57 "-d", OutputDirectoryPatch.getOutputDirectory(),58 "-junit",59 "-testclass", "test.sample.JUnitSample1",60 "-suitename", "\""+suiteName+"\""61 };62 TestListenerAdapter tla = new TestListenerAdapter();63 TestNG.privateMain(argv, tla);64 List<ITestContext> contexts = tla.getTestContexts();65 assertTrue(contexts.size()>0);66 for (ITestContext context:contexts) {67 assertEquals(context.getSuite().getName(),suiteName);68 }69 }70 /**71 * Test the ability to override the default command line test name72 */73 @Test(groups = { "current" } )74 public void testNameOverride() {75 String testName="My Test Name";76 String[] argv = {77 "-log", "0",78 "-d", OutputDirectoryPatch.getOutputDirectory(),79 "-junit",80 "-testclass", "test.sample.JUnitSample1",81 "-testname", "\""+testName+"\""82 };83 TestListenerAdapter tla = new TestListenerAdapter();84 TestNG.privateMain(argv, tla);85 List<ITestContext> contexts = tla.getTestContexts();86 assertTrue(contexts.size()>0);87 for (ITestContext context:contexts) {88 assertEquals(context.getName(),testName);89 }90 }91 @Test92 public void testUseDefaultListenersArgument() {93 TestNG.privateMain(new String[] {94 "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1"95 }, null);96 }97 @Test98 public void testMethodParameter() {99 String[] argv = {100 "-log", "0",101 "-d", OutputDirectoryPatch.getOutputDirectory(),102 "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3",103 };104 TestListenerAdapter tla = new TestListenerAdapter();105 TestNG.privateMain(argv, tla);106 List<ITestResult> passed = tla.getPassedTests();107 Assert.assertEquals(passed.size(), 2);108 Assert.assertTrue((passed.get(0).getName().equals("method1") &&109 passed.get(1).getName().equals("method3"))110 ||111 (passed.get(1).getName().equals("method1") &&112 passed.get(0).getName().equals("method3")));113 }114 private static void ppp(String s) {115 System.out.println("[CommandLineTest] " + s);...
Source: AlwaysRunTest.java
...9 @Test10 public void withAlwaysRunAfter() {11 TestListenerAdapter tla = new TestListenerAdapter();12 TestNG testng = create();13 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());14 testng.setTestClasses(new Class[] { AlwaysRunAfter1.class });15 testng.addListener(tla);16 testng.run();17 assertTrue(AlwaysRunAfter1.success(), "afterTestMethod should have run");18 }19 @Test20 public void withoutAlwaysRunAfter() {21 TestListenerAdapter tla = new TestListenerAdapter();22 TestNG testng = create();23 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());24 testng.setTestClasses(new Class[] { AlwaysRunAfter2.class });25 testng.addListener(tla);26 testng.run();27 assertTrue(AlwaysRunAfter2.success(), "afterTestMethod should not have run");28 }29 @Test30 public void withoutAlwaysRunBefore() {31 TestListenerAdapter tla = new TestListenerAdapter();32 TestNG testng = create();33 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());34 testng.setTestClasses(new Class[] { AlwaysRunBefore1.class });35 testng.setGroups("A");36 testng.addListener(tla);37 testng.run();38 assertTrue(AlwaysRunBefore1.success(), "before alwaysRun methods should have been run");39 }40 private static void ppp(String s) {41 System.out.println("[AlwaysRunTest] " + s);42 }43}...
getOutputDirectory
Using AI Code Generation
1import org.testng.TestNG;2import java.util.List;3import java.util.ArrayList;4public class TestNgTest {5 public static void main(String[] args) {6 TestNG testng = new TestNG();7 List<String> suites = new ArrayList<String>();8 suites.add("testng.xml");9 testng.setTestSuites(suites);10 testng.setOutputDirectory("test-output");11 testng.run();12 }13}14C:\Users\user\IdeaProjects\testng>java -cp .;C:\Users\user\.m2\repository\org\testng\testng\7.1.0\testng-7.1.0.jar TestNgTest
getOutputDirectory
Using AI Code Generation
1import org.testng.TestNG;2import java.io.File;3import java.util.Arrays;4import java.util.List;5import java.util.stream.Collectors;6public class TestNGGetOutputDirectory {7 public static void main(String[] args) {8 TestNG testng = new TestNG();9 List<String> suitefiles = Arrays.asList("testng1.xml", "testng2.xml");10 testng.setTestSuites(suitefiles);11 testng.run();12 File outputdir = testng.getOutputDirectory();13 System.out.println("Output directory: " + outputdir);14 }15}16package com.zetcode;17import org.testng.TestNG;18import java.io.File;19import java.util.Arrays;20import java.util.List;21import java.util.stream.Collectors;22public class TestNGGetOutputDirectory2 {23 public static void main(String[] args) {24 TestNG testng = new TestNG();25 List<String> suitefiles = Arrays.asList("testng1.xml", "testng2.xml");26 testng.setTestSuites(suitefiles);27 testng.run();28 File outputdir = testng.getOutputDirectory();29 System.out.println("Output directory: " + outputdir);30 System.out.println("Output directory exists: " + outputdir.exists());31 }32}33package com.zetcode;34import org.testng.TestNG;35import java.io.File;36import java.util.Arrays;37import java.util.List;38import java.util.stream.Collectors;39public class TestNGGetOutputDirectory3 {40 public static void main(String[] args) {41 TestNG testng = new TestNG();42 List<String> suitefiles = Arrays.asList("testng1.xml", "testng2.xml");43 testng.setTestSuites(suitefiles);44 testng.run();45 File outputdir = testng.getOutputDirectory();46 System.out.println("Output directory: " + outputdir);47 System.out.println("Output directory exists: " + outputdir.exists());
getOutputDirectory
Using AI Code Generation
1public void testOutputDirectory() {2 TestNG testng = new TestNG();3 testng.setOutputDirectory("C:/test-output");4 testng.setTestClasses(new Class[] { TestClass.class });5 testng.run();6}7public void testOutputDirectory() {8 TestNG testng = new TestNG()9 testng.setOutputDirectory("C:/test-output")10 testng.setTestClasses(new Class[] { TestClass.class })11 testng.run()12}13fun testOutputDirectory() {14 val testng = TestNG()15 testng.setOutputDirectory("C:/test-output")16 testng.setTestClasses(arrayOf(TestClass::class.java))17 testng.run()18}19def testOutputDirectory() {20 val testng = new TestNG()21 testng.setOutputDirectory("C:/test-output")22 testng.setTestClasses(Array(TestClass))23 testng.run()24}25 testng.setOutputDirectory("C:/test-output")26 testng.setTestClasses([TestClass])27def testOutputDirectory(self):28 testng = TestNG()29 testng.setOutputDirectory("C:/test-output")30 testng.setTestClasses([TestClass])31 testng.run()32public function testOutputDirectory()33{34 $testng = new TestNG();35 $testng->setOutputDirectory("C:/test-output");
getOutputDirectory
Using AI Code Generation
1public class TestNGOutputDirectory {2 public static void main(String[] args) {3 TestNG testng = new TestNG();4 testng.setOutputDirectory("/Users/username/Documents/testng-output");5 testng.setTestClasses(new Class[] { TestClass.class });6 testng.run();7 }8}9public class TestNGOutputDirectory {10 public static void main(String[] args) {11 TestNG testng = new TestNG();12 testng.setOutputDirectory("/Users/username/Documents/testng-output");13 testng.setTestClasses(new Class[] { TestClass.class });14 testng.run();15 }16}17public class TestNGOutputDirectory {18 public static void main(String[] args) {19 TestNG testng = new TestNG();20 testng.setOutputDirectory("/Users/username/Documents/testng-output");21 testng.setTestClasses(new Class[] { TestClass.class });22 testng.run();23 }24}25public class TestNGOutputDirectory {26 public static void main(String[] args) {27 TestNG testng = new TestNG();28 testng.setOutputDirectory("/Users/username/Documents/testng-output");29 testng.setTestClasses(new Class[] { TestClass.class });30 testng.run();31 }32}33public class TestNGOutputDirectory {34 public static void main(String[] args) {35 TestNG testng = new TestNG();36 testng.setOutputDirectory("/Users/username/Documents/testng-output");37 testng.setTestClasses(new Class[] { TestClass.class });38 testng.run();39 }40}
getOutputDirectory
Using AI Code Generation
1import org.testng.TestNG;2import java.io.File;3import java.io.IOException;4import java.nio.file.Files;5import java.nio.file.Paths;6import java.nio.file.StandardCopyOption;7public class TestNGResults {8 public static void main(String[] args) throws IOException {9 TestNG testng = new TestNG();10 testng.setTestClasses(new Class[] { TestNGResults.class });11 testng.run();12 String outputDirectory = testng.getOutputDirectory();13 String testngResultsFile = outputDirectory + File.separator + "testng-results.xml";14 String newTestngResultsFile = outputDirectory + File.separator + "testng-results.xml";15 Files.copy(Paths.get(testngResultsFile), Paths.get(newTestngResultsFile), StandardCopyOption.REPLACE_EXISTING);16 }17}
How to find how many testcase are there in TestNG class from another java class
Turn Citrus variable into Java variable
How to run JUnit tests with Gradle?
Tests pass when run individually but not when the whole test class run
Execute TestNG.xml from Jenkins (Maven Project)
Can a Java HashMap's size() be out of sync with its actual entries' size?
TestNG by default disables loading DTD from unsecure Urls
How to combine two object arrays in Java
Execute TestNG tests sequentially with different parameters?
TestNG ERROR Cannot find class in classpath
You can use reflection technique to find out the matching methods in the supplied class like:
public int TotalTescase(String pattern, Class<?> testNGclass) throws ClassNotFoundException
{
int count = 0;
testNGclass.getClass();
Class<?> className = Class.forName(testNGclass.getName());
Method[] methods = className.getMethods();
for(int i=0; i<methods.length; i++)
{
String methodName = methods[i].getName();
System.out.println("Method Name: "+methodName);
if(methodName.contains(pattern))
{
count++;
}
}
return count;
}
Check out the latest blogs from LambdaTest on this topic:
Galen Framework is a test automation framework which was originally introduced to perform cross browser layout testing of a web application in a browser. Nowadays, it has become a fully functional testing framework with rich reporting and test management system. This framework supports both Java and Javascript.
There are different interfaces provided by Java that allows you to modify TestNG behaviour. These interfaces are further known as TestNG Listeners in Selenium WebDriver. TestNG Listeners also allows you to customize the tests logs or report according to your project requirements.
According to netmarketshare, Google Chrome accounts for 67% of the browser market share. It is the choice of the majority of users and it’s popularity continues to rise. This is why, as an automation tester, it is important that you perform automated browser testing on Chrome browser.
Have you noticed the ubiquity of web forms while surfing the internet? Almost every website or web-application you visit, leverages web-forms to gain relevant information about yourself. From creating an account over a web-application to filling a brief survey, web forms are everywhere! A form comprises web elements such as checkbox, radio button, password, drop down to collect user data.
After being voted as the best programming language in the year 2018, Python still continues rising up the charts and currently ranks as the 3rd best programming language just after Java and C, as per the index published by Tiobe. With the increasing use of this language, the popularity of test automation frameworks based on Python is increasing as well. Obviously, developers and testers will get a little bit confused when it comes to choosing the best framework for their project. While choosing one, you should judge a lot of things, the script quality of the framework, test case simplicity and the technique to run the modules and find out their weaknesses. This is my attempt to help you compare the top 5 Python frameworks for test automation in 2019, and their advantages over the other as well as disadvantages. So you could choose the ideal Python framework for test automation according to your needs.
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!!