Best Testng code snippet using org.testng.TestNG.setConfigFailurePolicy
Source:TestNgSuiteRunner.java
...130 }131 132 XmlSuite suite = new XmlSuite();133 suite.setName("LocalSuite");134 suite.setConfigFailurePolicy( FailurePolicy.SKIP );135 136 suite.addListener(br.com.safra.automation.selenium.listeners137 .TestListener.class.getCanonicalName());138 suite.addListener(br.com.safra.automation.selenium.listeners139 .SuiteListener.class.getCanonicalName());140141 XmlTest test = new XmlTest(suite, 0);142 test.setName( "Test-Local-" + IdTest );143 Map<String, String> parameters = new HashMap<String, String>();144 test.setParameters(parameters);145 test.setXmlClasses( classes );146 147 return suite;148 }149 150 /**Prepara suite de teste para o MobileCenter, com as classes executadas151 * em seqüência, e sobre os dispositivos em paralelo.152 * 153 * @param classes Testar quais classes.154 * @param groups Testar somente as classes de quais grupos, se houver.155 * @param devices Executar em quais dispositivos.156 * @param threadCount Quantos testes em paralelo (em quantos dispositivos157 * ao mesmo tempo).158 * @return159 */160 protected XmlSuite setUpMobileCenterSuite(161 List<String> testClassNames,162 XmlGroups groups,163 List<String> devices,164 int threadCount )165 {166 LOG.info( "Gerando suÃte para execução sobre o MobileCenter em 'COLUNA' ..." );167 168 if ( threadCount <= 0 )169 {170 String message = "à necessário definir o número de threads.";171 throw new AutomationException( message );172 }173 174 ArrayList<XmlClass> classes = new ArrayList<XmlClass>();175 for ( String className : testClassNames )176 {177 classes.add( new XmlClass(className) );178 }179 180 XmlSuite suite = new XmlSuite();181 suite.setName( "MobileCenterSuite" );182 suite.setParallel( ParallelMode.TESTS );183 suite.setConfigFailurePolicy( FailurePolicy.SKIP );184 185 suite.addListener( br.com.safra.automation.selenium.listeners.186 TestListener.class.getCanonicalName() );187 188 suite.addListener( br.com.safra.automation.selenium.listeners.189 SuiteListener.class.getCanonicalName() );190 191 int i = 0;192 for (String deviceInfo : devices) {193 LOG.info("Gerando um <test> para o dispositivo '" + deviceInfo + "' ...");194195 XmlTest test = new XmlTest(suite, i);196 test.setName( "Test-MobileCenter-" + deviceInfo );197 Map<String, String> parameters = new HashMap<String, String>();198 parameters.put( "udid", deviceInfo );199 test.setParameters(parameters);200 if ( groups != null )201 {202 test.setGroups( groups );203 }204 test.setXmlClasses( classes );205 206 i++;207 }208 209 LOG.info("Número de threads em paralelo na suÃte: " + threadCount);210 suite.setThreadCount(threadCount);211 return suite;212 }213 214 /**Prepara suite de teste para o MobileCenter, com classes alternadas,215 * de forma que a execução sempre use o máximo de licenças disponÃveis.216 * Isto é, a continuação da execução das próximas classes nos próximos217 * dispositivos não aguarda o término da execução das classes anteriores218 * nos dispositivos anteriores.219 * 220 * @param classes Testar quais classes.221 * @param groups Testar somente as classes de quais grupos, se houver.222 * @param devices Executar em quais dispositivos.223 * @param threadCount Quantos testes em paralelo (em quantos dispositivos224 * ao mesmo tempo).225 * @return226 */227 protected XmlSuite setUpMobileCenterAlternatingSuite(228 List<String> testClassNames,229 XmlGroups groups,230 List<String> devices,231 int threadCount )232 {233 LOG.info( "Gerando suÃte para execução sobre o MobileCenter em 'LINHA' ..." );234 235 if ( threadCount <= 0 )236 {237 String message = "à necessário definir o número de threads.";238 throw new AutomationException( message );239 }240 241 ArrayList<ArrayList<XmlClass>> classes = new ArrayList<ArrayList<XmlClass>>();242 for ( String className : testClassNames )243 {244 ArrayList<XmlClass> singleList = new ArrayList<XmlClass>();245 singleList.add( new XmlClass(className) );246 classes.add( singleList );247 }248 249 XmlSuite suite = new XmlSuite();250 suite.setName( "MobileCenterSuite" );251 suite.setParallel( ParallelMode.TESTS );252 suite.setConfigFailurePolicy( FailurePolicy.SKIP );253 254 suite.addListener( br.com.safra.automation.selenium.listeners.255 TestListener.class.getCanonicalName() );256 257 suite.addListener( br.com.safra.automation.selenium.listeners.258 SuiteListener.class.getCanonicalName() );259 260 int i = 0;261 for ( ArrayList<XmlClass> singleList : classes )262 {263 String className = singleList.get( 0 ).getName();264 LOG.info( "Gerando os <test> para a classe :" + className );265 for ( String deviceInfo : devices )266 {267 LOG.info( "Gerando um <test> para o dispositivo '" + deviceInfo + "' ..." );268269 XmlTest test = new XmlTest(suite, i);270 test.setName( "Test-MobileCenter-" + className + "-" + deviceInfo );271 Map<String, String> parameters = new HashMap<String, String>();272 parameters.put( "udid", deviceInfo );273 test.setParameters(parameters);274 if ( groups != null )275 {276 test.setGroups( groups );277 }278 test.setXmlClasses( singleList );279 280 i++;281 }282 }283 284 LOG.info( "Número de threads em paralelo na suÃte: " + threadCount );285 suite.setThreadCount( threadCount );286 return suite;287 }288 289 /**Prepara uma suÃte de testng para abastecimento de massa de teste.290 * 291 * @param testClassNames os nomes canônicos das classes de teste.292 * @param groups os grupos a ser incluÃdos, ou null se nenhum.293 * @param devices os udids dos dispositivos onde será feita a execução.294 * @param threadCount a quantidade de threads simultâneas.295 * @param dispositivosDeMassa os udids dos dispositivos que296 * se quer abastecer de massa, cujos usuários serão usados para logar.297 * @return suÃte de Testng298 */299 protected XmlSuite setUpMassa(300 List<String> testClassNames,301 XmlGroups groups,302 List<String> devices,303 int threadCount,304 List<String> dispositivosDeMassa )305 {306 if ( threadCount <= 0 )307 {308 String message = "à necessário definir o número de threads.";309 throw new AutomationException( message );310 }311 312 ArrayList<ArrayList<XmlClass>> classes = new ArrayList<ArrayList<XmlClass>>();313 for ( String className : testClassNames )314 {315 ArrayList<XmlClass> singleList = new ArrayList<XmlClass>();316 singleList.add( new XmlClass(className) );317 classes.add( singleList );318 }319 320 XmlSuite suite = new XmlSuite();321 suite.setName( "MobileCenterSuite" );322 suite.setParallel( ParallelMode.TESTS );323 suite.setConfigFailurePolicy( FailurePolicy.SKIP );324 325 suite.addListener( br.com.safra.automation.selenium.listeners.326 TestListener.class.getCanonicalName() );327 328 suite.addListener( br.com.safra.automation.selenium.listeners.329 SuiteListener.class.getCanonicalName() );330 331 int i = 0;332 for ( ArrayList<XmlClass> singleList : classes )333 {334 String className = singleList.get( 0 ).getName();335 LOG.info( "Gerando os <test> para a classe :" + className );336 for ( String deviceInfo : dispositivosDeMassa )337 {
...
Source:TestNGTestClassProcessor.java
...100 testNg.setThreadCount(options.getThreadCount());101 }102 Class<?> configFailurePolicyArgType = getConfigFailurePolicyArgType(testNg);103 Object configFailurePolicyArgValue = getConfigFailurePolicyArgValue(testNg);104 invokeVerifiedMethod(testNg, "setConfigFailurePolicy", configFailurePolicyArgType, configFailurePolicyArgValue, DEFAULT_CONFIG_FAILURE_POLICY);105 invokeVerifiedMethod(testNg, "setPreserveOrder", boolean.class, options.getPreserveOrder(), false);106 invokeVerifiedMethod(testNg, "setGroupByInstances", boolean.class, options.getGroupByInstances(), false);107 testNg.setUseDefaultListeners(options.getUseDefaultListeners());108 testNg.setVerbose(0);109 testNg.setGroups(CollectionUtils.join(",", options.getIncludeGroups()));110 testNg.setExcludedGroups(CollectionUtils.join(",", options.getExcludeGroups()));111 //adding custom test listeners before Gradle's listeners.112 //this way, custom listeners are more powerful and, for example, they can change test status.113 for (String listenerClass : options.getListeners()) {114 try {115 testNg.addListener(JavaReflectionUtil.newInstance(applicationClassLoader.loadClass(listenerClass)));116 } catch (Throwable e) {117 throw new GradleException(String.format("Could not add a test listener with class '%s'.", listenerClass), e);118 }119 }120 if (!options.getIncludedTests().isEmpty() || !options.getIncludedTestsCommandLine().isEmpty() || !options.getExcludedTests().isEmpty()) {121 testNg.addListener(new SelectedTestsFilter(options.getIncludedTests(),122 options.getExcludedTests(), options.getIncludedTestsCommandLine()));123 }124 if (!suiteFiles.isEmpty()) {125 testNg.setTestSuites(GFileUtils.toPaths(suiteFiles));126 } else {127 testNg.setTestClasses(testClasses.toArray(new Class<?>[0]));128 }129 testNg.addListener((Object) adaptListener(new TestNGTestResultProcessorAdapter(resultProcessor, idGenerator, clock)));130 testNg.run();131 }132 /**133 * The setter for configFailurePolicy has a different signature depending on TestNG version. This method uses reflection to134 * detect the API and return a reference to the correct argument type.135 * <ul>136 * <li>When TestNG >= 6.9.12, {@link TestNG#setConfigFailurePolicy(org.testng.xml.XmlSuite$FailurePolicy)}</li>137 * <li>When TestNG < 6.9.12, {@link TestNG#setConfigFailurePolicy(String)}</li>138 * </ul></li>139 *140 * @param testNg the TestNG instance141 * @return String.class or org.testng.xml.XmlSuite$FailurePolicy.class142 */143 private Class<?> getConfigFailurePolicyArgType(TestNG testNg) {144 Class<?> failurePolicy;145 try {146 failurePolicy = Class.forName("org.testng.xml.XmlSuite$FailurePolicy", false, testNg.getClass().getClassLoader());147 } catch (ClassNotFoundException e) {148 // new API not found; fallback to legacy String argument149 failurePolicy = String.class;150 }151 return failurePolicy;152 }153 /**154 * The setter for configFailurePolicy has a different signature depending on TestNG version. This method uses reflection to155 * detect the API. If not {@link String}, coerce the spec's string value to the expected enum value using {@link XmlSuite$FailurePolicy#getValidPolicy(String)}156 * <ul>157 * <li>When TestNG >= 6.9.12, {@link TestNG#setConfigFailurePolicy(org.testng.xml.XmlSuite$FailurePolicy)}</li>158 * <li>When TestNG < 6.9.12, {@link TestNG#setConfigFailurePolicy(String)}</li>159 * </ul></li>160 *161 * @param testNg the TestNG instance162 * @return Arg value; might be a String or an enum value of org.testng.xml.XmlSuite$FailurePolicy.class163 */164 private Object getConfigFailurePolicyArgValue(TestNG testNg) {165 Object configFailurePolicyArgValue;166 try {167 Class<?> failurePolicy = Class.forName("org.testng.xml.XmlSuite$FailurePolicy", false, testNg.getClass().getClassLoader());168 Method getValidPolicy = failurePolicy.getMethod("getValidPolicy", String.class);169 configFailurePolicyArgValue = getValidPolicy.invoke(null, options.getConfigFailurePolicy());170 } catch (Exception e) {171 // unable to invoke new API method; fallback to legacy String value172 configFailurePolicyArgValue = options.getConfigFailurePolicy();...
Source:TestRunner.java
...67 testng.setDataProviderThreadCount(suites.getNumDataProviderThreads());68 testng.setXmlSuites(xmlSuites);69 testng.setUseDefaultListeners(false);70 testng.setParallel("methods");71 testng.setConfigFailurePolicy("continue");72 testng.setVerbose(100);73 testng.run();74 }75 private static XmlSuite createXmlSuite(String singleSuite) {76 XmlSuite suite = new XmlSuite();77 suite.setName("Selgp QA - " + singleSuite + " tests");78 suite.setParallel("methods");79 suite.setDataProviderThreadCount(suites.getNumDataProviderThreads());80 suite.setThreadCount(suites.getNumThreads());81 suite.setTimeOut(String.valueOf(suites.getTimeout()));82 suite.setConfigFailurePolicy("continue");83 suite.setVerbose(100);84 if (!TestRunner.getInstance().PARAMETER.equals("unset")) {85 String[] keyAndValue = TestRunner.getInstance().PARAMETER.split("=");86 Map<String, String> parameterMap = new HashMap<String, String>();87 parameterMap.put(keyAndValue[0], keyAndValue[1]);88 suite.setParameters(parameterMap);89 }90 List<XmlClass> xmlClasses = null;91 if (!TestRunner.getInstance().CLASS_NAME.equals("unset")) {92 xmlClasses = getClassesFromArguments();93 } else {94 xmlClasses = getClassesForSuite();95 }96 createSuiteXmlTest(suite, TestRunner.getInstance().suites, xmlClasses);...
Source:FailurePolicyTest.java
...60 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());61 testng.setTestClasses(classesUnderTest);62 testng.addListener(tla);63 testng.setVerbose(0);64 testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);65 testng.run();66 verify(tla, configurationFailures, configurationSkips, skippedTests);67 }68 @Test69 public void confFailureTestInvolvingGroups() {70 Class[] classesUnderTest = new Class[]{71 ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class72 };73 TestListenerAdapter tla = new TestListenerAdapter();74 TestNG testng = new TestNG();75 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());76 testng.setTestClasses(classesUnderTest);77 testng.addListener(tla);78 testng.setVerbose(0);79 testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);80 testng.setGroups("group1");81 testng.run();82 verify(tla, 1, 3, 1);83 }84 @Test85 public void commandLineTest_policyAsSkip() {86 String[] argv =87 new String[] {88 "-log",89 "0",90 "-d",91 OutputDirectoryPatch.getOutputDirectory(),92 "-configfailurepolicy",93 "skip",...
Source:IssueTest.java
...20 }21 @Test(description = GITHUB_1777)22 public void testOnStartInvokedForSkippedTests() {23 TestNG testNG = create(TestClassSample.class);24 testNG.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);25 MyListener listener = new MyListener();26 testNG.addListener(listener);27 testNG.run();28 List<String> expectedTestMessages =29 Arrays.asList(30 "testStart_test_method: test1", "testSkipped_test_method: test1",31 "before_test_method: test1", "after_test_method: test1",32 "testStart_test_method: test2", "before_test_method: test2",33 "after_test_method: test2", "testSuccess_test_method: test2");34 assertThat(listener.tstMsgs).containsExactlyElementsOf(expectedTestMessages);35 List<String> expectedConfigMessages =36 Arrays.asList(37 "before_configuration_method: beforeMethod[test1]",38 "after_configuration_method: beforeMethod[test1]",...
Source:ConfigurationFailure.java
...41 }42 @Test(description = "GITHUB-990")43 public void ensureConfigurationRunsFromBaseClass() {44 TestNG testng = create(ChildClassSample.class);45 testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);46 testng.run();47 assertThat(AbstractBaseSample.messages).containsExactly("cleanup");48 }49}...
Source:AfterGroupsBehaviorTest.java
...30 XmlSuite xmlsuite = createXmlSuite("sample_suite", "sample_test", clazz);31 xmlsuite.addIncludedGroup(groups);32 TestNG testng = create(xmlsuite);33 if (shouldContinue) {34 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);35 }36 LocalConfigListener listener = new LocalConfigListener();37 testng.addListener(listener);38 testng.run();39 assertThat(listener.getMessages()).containsExactly(expected);40 }41}...
setConfigFailurePolicy
Using AI Code Generation
1public void testSetConfigFailurePolicy() {2 TestNG testng = new TestNG();3 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);4 Assert.assertEquals(testng.getConfigFailurePolicy(), FailurePolicy.CONTINUE);5}6org.testng.TestNG.setConfigFailurePolicy(org.testng.internal.annotations.IConfigurationAnnotation.FailurePolicy)7public void setConfigFailurePolicy(FailurePolicy configFailurePolicy)8org.testng.TestNG.setConfigFailurePolicy(org.testng.internal.annotations.IConfigurationAnnotation.FailurePolicy)9public void setConfigFailurePolicy(FailurePolicy configFailurePolicy)10org.testng.TestNG.getConfigFailurePolicy()11public FailurePolicy getConfigFailurePolicy()12org.testng.TestNG.setConfigFailurePolicy(org.testng.internal.annotations.IConfigurationAnnotation.FailurePolicy)13public void setConfigFailurePolicy(FailurePolicy configFailurePolicy)14org.testng.TestNG.getConfigFailurePolicy()15public FailurePolicy getConfigFailurePolicy()16org.testng.TestNG.setConfigFailurePolicy(org.testng.internal.annotations.IConfigurationAnnotation.FailurePolicy)17public void setConfigFailurePolicy(FailurePolicy configFailurePolicy)
setConfigFailurePolicy
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.XmlSuite;3import org.testng.xml.XmlTest;4import java.util.ArrayList;5import java.util.List;6public class TestNGSetConfigFailurePolicyExample {7 public static void main(String[] args) {8 TestNG testNG = new TestNG();9 testNG.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);10 XmlSuite suite = new XmlSuite();11 suite.setName("TestNGSetConfigFailurePolicyExample");12 XmlTest test = new XmlTest(suite);13 test.setName("TestNGSetConfigFailurePolicyExample");14 List<String> files = new ArrayList<>();15 files.add("src/test/resources/testng-xmlsuite.xml");16 test.setSuiteFiles(files);17 List<XmlSuite> suites = new ArrayList<>();18 suites.add(suite);19 testNG.setXmlSuites(suites);20 testNG.run();21 }22}
setConfigFailurePolicy
Using AI Code Generation
1TestNG testng = new TestNG();2testng.setConfigFailurePolicy("continue");3testng.setXmlSuites(suites);4testng.run();5XmlSuite suite = new XmlSuite();6suite.setConfigFailurePolicy(XmlSuite.CONTINUE);7suite.setTests(suites.get(0).getTests());8TestNG testng = new TestNG();9testng.setXmlSuites(Arrays.asList(suite));10testng.run();
setConfigFailurePolicy
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.XmlSuite;3public class TestNGTest {4 public static void main(String[] args) {5 TestNG testNG = new TestNG();6 testNG.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);7 testNG.setTestClasses(new Class[] { TestClass.class });8 testNG.run();9 }10}11setTestSuites() method12public void setTestSuites(String[] suites)13import org.testng.TestNG;14public class TestNGTest {15 public static void main(String[] args) {16 TestNG testNG = new TestNG();17 testNG.setTestSuites(new String[] { "testng.xml" });18 testNG.run();19 }20}21setTestClasses() method22public void setTestClasses(Class[] classes)23import org.testng.TestNG;24public class TestNGTest {25 public static void main(String[] args) {26 TestNG testNG = new TestNG();27 testNG.setTestClasses(new Class[] { TestClass.class });28 testNG.run();29 }30}31setOutputDirectory() method32public void setOutputDirectory(String outputDirectory)33import org.testng.TestNG;34public class TestNGTest {35 public static void main(String[] args) {
setConfigFailurePolicy
Using AI Code Generation
1public void testSetConfigFailurePolicy() {2 TestNG testng = new TestNG();3 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);4}5org.testng.TestNG.setConfigFailurePolicy(FailurePolicy)6public void setConfigFailurePolicy(FailurePolicy configFailurePolicy)7org.testng.TestNG.getConfigFailurePolicy()8public FailurePolicy getConfigFailurePolicy()9org.testng.TestNG.setVerbose(int)10public void setVerbose(int level)11org.testng.TestNG.getVerbose()12public int getVerbose()13org.testng.TestNG.setPreserveOrder(boolean)14public void setPreserveOrder(boolean preserveOrder)15org.testng.TestNG.setParallel(ParallelMode)16public void setParallel(ParallelMode mode)17org.testng.TestNG.getParallel()18public ParallelMode getParallel()19org.testng.TestNG.setThreadCount(int)20public void setThreadCount(int threadCount)21org.testng.TestNG.getThreadCount()22public int getThreadCount()23org.testng.TestNG.setUseDefaultListeners(boolean)24public void setUseDefaultListeners(boolean useDefaultListeners)25org.testng.TestNG.setUseDefaultListeners(boolean)26public void setUseDefaultListeners(boolean useDefaultListeners)27org.testng.TestNG.getUseDefaultListeners()28public boolean getUseDefaultListeners()
setConfigFailurePolicy
Using AI Code Generation
1public void testSetConfigFailurePolicy() {2 TestNG testng = new TestNG();3 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);4}5What is the difference between @Test and @Test(enabled=false)?6@Test(enabled=false)7public void testSetConfigFailurePolicy() {8 TestNG testng = new TestNG();9 testng.setConfigFailurePolicy(FailurePolicy.CONTINUE);10}11What is the difference between @Test(expectedExceptions = ArithmeticException.class) and @Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = "divide by zero")?12@Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = "divide by zero")13public void testDivide() {14 int result = 1/0;15}16What is the difference between @Test(timeOut = 1000) and @Test(invocationTimeOut = 1000)?17@Test(timeOut = 1000)18public void testDivide() {19 int result = 1/0;20}21@Test(invocationTimeOut = 1000)22public void testDivide() {23 int result = 1/0;24}25What is the difference between @Test(priority=0) and @Test(priority=1)?26@Test(priority=0)27public void testDivide() {28 int result = 1/0;29}
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!!