Best Testng code snippet using org.testng.TestNG.setOutputDirectory
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}...
Source:ConfigurationFailure.java
...14 @Test15 public void beforeTestClassFails() {16 TestListenerAdapter tla = new TestListenerAdapter();17 TestNG testng = new TestNG();18 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());19 testng.setTestClasses(new Class[] {20 ClassWithFailedBeforeTestClass.class,21 ClassWithFailedBeforeTestClassVerification.class22 });23 testng.addListener(tla);24 testng.setVerbose(0);25 testng.run();26 assertTrue(ClassWithFailedBeforeTestClassVerification.success(),27 "Not all the @Configuration methods of Run2 were run");28 }29 @Test30 public void beforeTestSuiteFails() {31 TestListenerAdapter tla = new TestListenerAdapter();32 TestNG testng = new TestNG();33 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());34 testng.setTestClasses(new Class[] { ClassWithFailedBeforeSuite.class, ClassWithFailedBeforeSuiteVerification.class });35 testng.addListener(tla);36 testng.setVerbose(0);37 testng.run();38 assertTrue(ClassWithFailedBeforeSuiteVerification.success(),39 "No @Configuration methods should have run");40 }41 private static void ppp(String s) {42 System.out.println("[AlwaysRunTest] " + s);43 }44}...
setOutputDirectory
Using AI Code Generation
1import org.testng.TestNG;2import java.util.ArrayList;3import java.util.List;4public class SetOutputDirectory {5 public static void main(String[] args) {6 TestNG testNG = new TestNG();7 List<String> suites = new ArrayList<>();8 suites.add("C:\\Users\\username\\IdeaProjects\\setOutputDirectory\\testng.xml");9 testNG.setTestSuites(suites);10 testNG.setOutputDirectory("C:\\Users\\username\\IdeaProjects\\setOutputDirectory\\test-output");11 testNG.run();12 }13}
setOutputDirectory
Using AI Code Generation
1package com.codoid.products.exception;2import java.io.File;3import java.io.IOException;4import java.util.Date;5import org.testng.annotations.Test;6public class TestNGExceptionExample {7 public void test1() throws IOException {8 File file = new File("C:\\test.txt");9 file.createNewFile();10 }11 public void test2() {12 System.out.println("test2");13 }14 public void test3() {15 System.out.println("test3");16 }17 public void test4() {18 System.out.println("test4");19 }20 public void test5() {21 System.out.println("test5");22 }23}24[INFO] --- maven-surefire-plugin:2.18:test (default-test) @ TestNGExceptionExample ---25test1(com.codoid.products.exception.TestNGExceptionExample) Time elapsed: 0.001 sec <<< FAILURE!26 at com.codoid.products.exception.TestNGExceptionExample.test1(TestNGExceptionExample.java:18)
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!!