Best Testng code snippet using org.testng.xml.XmlSuite.getConfigFailurePolicy
Source:XmlSuite.java
...217 /**218 * Returns the configuration failure policy.219 * @return the configuration failure policy220 */221 public FailurePolicy getConfigFailurePolicy() {222 return m_configFailurePolicy;223 }224 /**225 * Returns the verbose.226 * @return the verbose.227 */228 public Integer getVerbose() {229 return m_verbose != null ? m_verbose : TestNG.DEFAULT_VERBOSE;230 }231 /**232 * Set the verbose.233 * @param verbose The verbose to set.234 */235 public void setVerbose(Integer verbose) {236 m_verbose = verbose;237 }238 /**239 * Returns the name.240 * @return the name.241 */242 public String getName() {243 return m_name;244 }245 /**246 * Sets the name.247 * @param name The name to set.248 */249 public void setName(String name) {250 m_name = name;251 }252 /**253 * Returns the test.254 * @return the test.255 */256 public String getTest() {257 return m_test;258 }259 /**260 * Returns the tests.261 * @return the tests.262 */263 public List<XmlTest> getTests() {264 return m_tests;265 }266 // For YAML267 public void setTests(List<XmlTest> tests) {268 m_tests = tests;269 }270 /**271 * Returns the method selectors.272 *273 * @return the method selectors.274 */275 public List<XmlMethodSelector> getMethodSelectors() {276 if (m_xmlMethodSelectors != null) {277 return m_xmlMethodSelectors.getMethodSelectors();278 } else {279 // deprecated280 return m_methodSelectors;281 }282 }283 /**284 * Sets the method selectors.285 *286 * @param methodSelectors the method selectors.287 */288 public void setMethodSelectors(List<XmlMethodSelector> methodSelectors) {289 m_methodSelectors = Lists.newArrayList(methodSelectors);290 }291 /**292 * Updates the list of parameters that apply to this XML suite. This method293 * should be invoked any time there is a change in the state of this suite that294 * would affect the parameter list.<br>295 * NOTE: Currently being invoked after a parent suite is added or if parameters296 * for this suite are updated.297 */298 private void updateParameters() {299 /*300 * Whatever parameters are set by user or via XML, should be updated301 * using parameters from parent suite, if it exists. Parameters from this302 * suite override the same named parameters from parent suite.303 */304 if (m_parentSuite != null) {305 Set<String> keySet = m_parentSuite.getParameters().keySet();306 for (String name : keySet) {307 if (!m_parameters.containsKey(name)) {308 m_parameters.put(name, m_parentSuite.getParameter(name));309 }310 }311 }312 }313 /**314 * Sets parameters.315 * @param parameters the parameters.316 */317 public void setParameters(Map<String, String> parameters) {318 m_parameters = parameters;319 updateParameters();320 }321 /**322 * Gets the parameters that apply to tests in this suite.<br>323 * Set of parameters for a suite is appended with parameters from parent suite.324 * Also, parameters from this suite override the same named parameters from325 * parent suite.326 */327 public Map<String, String> getParameters() {328 return m_parameters;329 }330 /**331 * @return The parameters defined in this suite and all its XmlTests.332 */333 public Map<String, String> getAllParameters() {334 Map<String, String> result = Maps.newHashMap();335 for (Map.Entry<String, String> entry : m_parameters.entrySet()) {336 result.put(entry.getKey(), entry.getValue());337 }338 for (XmlTest test : getTests()) {339 Map<String, String> tp = test.getLocalParameters();340 for (Map.Entry<String, String> entry : tp.entrySet()) {341 result.put(entry.getKey(), entry.getValue());342 }343 }344 return result;345 }346 /**347 * Returns the parameter defined in this suite only.348 * @param name the parameter name.349 * @return The parameter defined in this suite only.350 */351 public String getParameter(String name) {352 return m_parameters.get(name);353 }354 /**355 * @return The threadCount.356 */357 public int getThreadCount() {358 return m_threadCount;359 }360 /**361 * Set the thread count.362 * @param threadCount The thread count to set.363 */364 public void setThreadCount(int threadCount) {365 m_threadCount = threadCount;366 }367 /**368 * @return The JUnit compatibility flag.369 */370 public Boolean isJUnit() {371 return m_isJUnit;372 }373 /**374 * Sets the JUnit compatibility flag.375 *376 * @param isJUnit the JUnit compatibility flag.377 */378 public void setJUnit(Boolean isJUnit) {379 m_isJUnit = isJUnit;380 }381 // For YAML382 public void setJunit(Boolean j) {383 setJUnit(j);384 }385 public Boolean skipFailedInvocationCounts() {386 return m_skipFailedInvocationCounts;387 }388 public void setSkipFailedInvocationCounts(boolean skip) {389 m_skipFailedInvocationCounts = skip;390 }391 /**392 * Sets the XML packages.393 *394 * @param packages the XML packages.395 */396 public void setXmlPackages(List<XmlPackage> packages) {397 m_xmlPackages = Lists.newArrayList(packages);398 }399 /**400 * Returns the XML packages.401 *402 * @return the XML packages.403 */404 public List<XmlPackage> getXmlPackages() {405 return m_xmlPackages;406 }407 // For YAML408 public List<XmlPackage> getPackages() {409 return getXmlPackages();410 }411 @Tag(name = "method-selectors")412 public void setMethodSelectors(XmlMethodSelectors xms) {413 m_xmlMethodSelectors = xms;414 }415 // For YAML416 public void setPackages(List<XmlPackage> packages) {417 setXmlPackages(packages);418 }419 /**420 * @return A String representation of this XML suite.421 */422 public String toXml() {423 XMLStringBuffer xsb = new XMLStringBuffer();424 xsb.setDocType("suite SYSTEM \"" + Parser.TESTNG_DTD_URL + '\"');425 Properties p = new Properties();426 p.setProperty("name", getName());427 if (getVerbose() != null) {428 XmlUtils.setProperty(p, "verbose", getVerbose().toString(), DEFAULT_VERBOSE.toString());429 }430 final ParallelMode parallel= getParallel();431 if(parallel != null && !DEFAULT_PARALLEL.equals(parallel)) {432 p.setProperty("parallel", parallel.toString());433 }434 XmlUtils.setProperty(p, "group-by-instances", String.valueOf(getGroupByInstances()),435 DEFAULT_GROUP_BY_INSTANCES.toString());436 XmlUtils.setProperty(p, "configfailurepolicy", getConfigFailurePolicy().toString(),437 DEFAULT_CONFIG_FAILURE_POLICY.toString());438 XmlUtils.setProperty(p, "thread-count", String.valueOf(getThreadCount()),439 DEFAULT_THREAD_COUNT.toString());440 XmlUtils.setProperty(p, "data-provider-thread-count", String.valueOf(getDataProviderThreadCount()),441 DEFAULT_DATA_PROVIDER_THREAD_COUNT.toString());442 if (! DEFAULT_JUNIT.equals(m_isJUnit)) {443 p.setProperty("junit", m_isJUnit != null ? m_isJUnit.toString() : "false"); // TESTNG-141444 }445 XmlUtils.setProperty(p, "skipfailedinvocationcounts", m_skipFailedInvocationCounts.toString(),446 DEFAULT_SKIP_FAILED_INVOCATION_COUNTS.toString());447 if(null != m_objectFactory) {448 p.setProperty("object-factory", m_objectFactory.getClass().getName());449 }450 if (isStringNotEmpty(m_parentModule)) {451 p.setProperty("parent-module", getParentModule());452 }453 if (isStringNotEmpty(m_guiceStage)) {454 p.setProperty("guice-stage", getGuiceStage());455 }456 XmlUtils.setProperty(p, "allow-return-values", String.valueOf(getAllowReturnValues()),457 DEFAULT_ALLOW_RETURN_VALUES.toString());458 xsb.push("suite", p);459 XmlUtils.dumpParameters(xsb, m_parameters);460 if (hasElements(m_listeners)) {461 xsb.push("listeners");462 for (String listenerName: m_listeners) {463 Properties listenerProps = new Properties();464 listenerProps.setProperty("class-name", listenerName);465 xsb.addEmptyElement("listener", listenerProps);466 }467 xsb.pop("listeners");468 }469 if (hasElements(getXmlPackages())) {470 xsb.push("packages");471 for (XmlPackage pack : getXmlPackages()) {472 xsb.getStringBuffer().append(pack.toXml(" "));473 }474 xsb.pop("packages");475 }476 if (getXmlMethodSelectors() != null) {477 xsb.getStringBuffer().append(getXmlMethodSelectors().toXml(" "));478 } else {479 // deprecated480 if (hasElements(getMethodSelectors())) {481 xsb.push("method-selectors");482 for (XmlMethodSelector selector : getMethodSelectors()) {483 xsb.getStringBuffer().append(selector.toXml(" "));484 }485 xsb.pop("method-selectors");486 }487 }488 List<String> suiteFiles = getSuiteFiles();489 if (suiteFiles.size() > 0) {490 xsb.push("suite-files");491 for (String sf : suiteFiles) {492 Properties prop = new Properties();493 prop.setProperty("path", sf);494 xsb.addEmptyElement("suite-file", prop);495 }496 xsb.pop("suite-files");497 }498 List<String> included = getIncludedGroups();499 List<String> excluded = getExcludedGroups();500 if (hasElements(included) || hasElements(excluded)) {501 xsb.push("groups");502 xsb.push("run");503 for (String g : included) {504 xsb.addEmptyElement("include", "name", g);505 }506 for (String g : excluded) {507 xsb.addEmptyElement("exclude", "name", g);508 }509 xsb.pop("run");510 xsb.pop("groups");511 }512 if (m_xmlGroups != null) {513 xsb.getStringBuffer().append(m_xmlGroups.toXml(" "));514 }515 for (XmlTest test : getTests()) {516 xsb.getStringBuffer().append(test.toXml(" "));517 }518 xsb.pop("suite");519 return xsb.toXML();520 }521 @Tag(name = "method-selectors")522 public void setXmlMethodSelectors(XmlMethodSelectors xms) {523 m_xmlMethodSelectors = xms;524 }525 private XmlMethodSelectors getXmlMethodSelectors() {526 return m_xmlMethodSelectors;527 }528 /**529 * {@inheritDoc}530 */531 @Override532 public String toString() {533 StringBuilder result = new StringBuilder("[Suite: \"").append( m_name).append( "\" ");534 for (XmlTest t : m_tests) {535 result.append(" ").append( t.toString()).append(' ');536 }537 for (XmlMethodSelector ms : m_methodSelectors) {538 result.append(" methodSelector:").append(ms);539 }540 result.append(']');541 return result.toString();542 }543 /**544 * {@inheritDoc}545 * Note that this is not a full clone: XmlTest children are not cloned by this546 * method.547 */548 @Override549 public Object clone() {550 XmlSuite result = shallowCopy();551 result.setExcludedGroups(getExcludedGroups());552 result.setIncludedGroups(getIncludedGroups());553 result.setGroupByInstances(getGroupByInstances());554 result.setGroups(getGroups());555 result.setMethodSelectors(getXmlMethodSelectors());556 result.setPackages(getPackages());557 result.setParentSuite(getParentSuite());558 result.setPreserveOrder(getPreserveOrder());559 result.setSuiteFiles(getSuiteFiles());560 result.setTests(getTests());561 result.setXmlMethodSelectors(getXmlMethodSelectors());562 return result;563 }564 /**565 * This method returns a shallow cloned version. {@link XmlTest} are not copied by this method.566 * @return - A Shallow copied version of {@link XmlSuite}.567 */568 public XmlSuite shallowCopy() {569 XmlSuite result = new XmlSuite();570 result.setName(getName());571 result.setFileName(getFileName());572 result.setListeners(getListeners());573 result.setParallel(getParallel());574 result.setParentModule(getParentModule());575 result.setGuiceStage(getGuiceStage());576 result.setConfigFailurePolicy(getConfigFailurePolicy());577 result.setThreadCount(getThreadCount());578 result.setDataProviderThreadCount(getDataProviderThreadCount());579 result.setParameters(getParameters());580 result.setVerbose(getVerbose());581 result.setXmlPackages(getXmlPackages());582// result.setBeanShellExpression(getExpression());583 result.setMethodSelectors(getMethodSelectors());584 result.setJUnit(isJUnit()); // TESTNG-141585 result.setSkipFailedInvocationCounts(skipFailedInvocationCounts());586 result.setObjectFactory(getObjectFactory());587 result.setAllowReturnValues(getAllowReturnValues());588 result.setTimeOut(getTimeOut());589 return result;590 }...
Source:TestNGTestClassProcessor.java
...98 }99 if (options.getThreadCount() > 0) {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();173 }174 return configFailurePolicyArgValue;175 }176 private void invokeVerifiedMethod(TestNG testNg, String methodName, Class<?> paramClass, Object value, Object defaultValue) {177 try {178 JavaMethod.of(TestNG.class, Object.class, methodName, paramClass).invoke(testNg, value);179 } catch (NoSuchMethodException e) {180 if (!value.equals(defaultValue)) {181 // Should not reach this point as this is validated in the test framework implementation - just propagate the failure182 throw e;183 }184 }185 }186 private ITestListener adaptListener(ITestListener listener) {...
Source:Yaml.java
...75 maybeAdd(result, "dataProviderThreadCount", suite.getDataProviderThreadCount(),76 XmlSuite.DEFAULT_DATA_PROVIDER_THREAD_COUNT);77 maybeAdd(result, "timeOut", suite.getTimeOut(), null);78 maybeAdd(result, "parallel", suite.getParallel(), XmlSuite.DEFAULT_PARALLEL);79 maybeAdd(result, "configFailurePolicy", suite.getConfigFailurePolicy().toString(), XmlSuite.DEFAULT_CONFIG_FAILURE_POLICY);80 maybeAdd(result, "skipFailedInvocationCounts", suite.skipFailedInvocationCounts(),81 XmlSuite.DEFAULT_SKIP_FAILED_INVOCATION_COUNTS);82 toYaml(result, "parameters", "", suite.getParameters());83 toYaml(result, suite.getPackages());84 if (!suite.getListeners().isEmpty()) {85 result.append("listeners:\n");86 toYaml(result, " ", suite.getListeners());87 }88 if (!suite.getPackages().isEmpty()) {89 result.append("packages:\n");90 toYaml(result, suite.getPackages());91 }92 if (!suite.getTests().isEmpty()) {93 result.append("tests:\n");...
Source:TestNGTestFramework.java
...80 TestNGSpec spec = new TestNGSpec(options, filter);81 return new TestClassProcessorFactoryImpl(this.options.getOutputDirectory(), spec, suiteFiles);82 }83 private void verifyConfigFailurePolicy() {84 if (!options.getConfigFailurePolicy().equals(TestNGOptions.DEFAULT_CONFIG_FAILURE_POLICY)) {85 String message = String.format("The version of TestNG used does not support setting config failure policy to '%s'.", options.getConfigFailurePolicy());86 try {87 // for TestNG v6.9.12 and higher88 Class<?> failurePolicyEnum = maybeLoadFailurePolicyEnum();89 verifyMethodExists("setConfigFailurePolicy", failurePolicyEnum, message);90 } catch (ClassNotFoundException cnfe) {91 // fallback to legacy String argument92 verifyMethodExists("setConfigFailurePolicy", String.class, message);93 }94 }95 }96 private void verifyPreserveOrder() {97 if (options.getPreserveOrder()) {98 verifyMethodExists("setPreserveOrder", boolean.class, "Preserving the order of tests is not supported by this version of TestNG.");99 }...
Source:FailurePolicyTest.java
...11public class FailurePolicyTest {12 // only if this is run from an xml file that sets this on the suite13 @BeforeClass(enabled=false)14 public void setupClass( ITestContext testContext) {15 assertEquals(testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), XmlSuite.CONTINUE);16 }17 @DataProvider( name="dp" )18 public Object[][] getData() {19 Object[][] data = new Object[][] {20 // params - confFail, confSkip, skipedTests21 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },22 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleTests.class }, 2, 0, 2 },23 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleInvocations.class }, 4, 0, 4 },24 new Object[] { new Class[] { ExtendsClassWithFailedBeforeMethod.class }, 2, 2, 2 },25 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },26 new Object[] { new Class[] { ExtendsClassWithFailedBeforeClassMethod.class }, 1, 2, 2 },27 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class }, 2, 3, 3 },28 new Object[] { new Class[] { ClassWithSkippingBeforeMethod.class }, 0, 1, 1 },29 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethod.class }, 2, 0, 2 },...
getConfigFailurePolicy
Using AI Code Generation
1import org.testng.xml.XmlSuite;2public class Test {3 public static void main(String args[]) {4 XmlSuite suite = new XmlSuite();5 suite.getConfigFailurePolicy();6 }7}8Exception in thread "main" java.lang.NoSuchMethodError: org.testng.xml.XmlSuite.getConfigFailurePolicy()Ljava/lang/String;9 at Test.main(Test.java:7)
getConfigFailurePolicy
Using AI Code Generation
1package org.testng.xml;2public class XmlSuite {3 private String name;4 private String configFailurePolicy;5 public String getConfigFailurePolicy() {6 return configFailurePolicy;7 }8 public void setConfigFailurePolicy(String configFailurePolicy) {9 this.configFailurePolicy = configFailurePolicy;10 }11}12package org.testng;13public class TestNG {14 public static void main(String[] args) {15 XmlSuite xmlSuite = new XmlSuite();16 xmlSuite.setConfigFailurePolicy("continue");17 System.out.println(xmlSuite.getConfigFailurePolicy());18 }19}
getConfigFailurePolicy
Using AI Code Generation
1package com.example;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4public class Main {5 public static void main(String[] args) {6 TestNG testNG = new TestNG();7 testNG.setTestSuites(args);8 testNG.run();9 for (XmlSuite suite : testNG.getXmlSuites()) {10 System.out.println("configFailurePolicy: " + suite.getConfigFailurePolicy());11 }12 }13}14package com.example;15import org.testng.TestNG;16import org.testng.xml.XmlSuite;17public class Main {18 public static void main(String[] args) {19 TestNG testNG = new TestNG();20 testNG.setTestSuites(args);21 testNG.run();22 for (XmlSuite suite : testNG.getXmlSuites()) {23 System.out.println("configFailurePolicy: " + suite.getConfigFailurePolicy());24 }25 }26}
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!!