22
votes

I have a TestNG suite with large amount of methods. I execute this suite using wrapper built on top of TestNG runner. All tests in the suite fail except one. What should I write in testng.xml to execute just that one failed test?

Obvious solution is to assign unique group names to all of the methods and then specify name in testng.xml. This can work in case of 2-3 methods, but it gets harder as number of tests grow.

6

6 Answers

29
votes

Instead of exclude, you may use include. It'll exactly what you want. Only this test will be executed.

  <classes>
    <class name="test.IndividualMethodsTest">
      <methods>
        <include name="testMethod" />
      </methods>
    </class>
  </classes>
8
votes

Try this:

  <classes>
    <class name="test.IndividualMethodsTest">
      <methods>
        <exclude name="testMethod" />
      </methods>
    </class>
  </classes>
7
votes

After each run, TestNG creates a filed called testng-failed.xml that contains only the tests that failed. Just invoke TestNG again on that file:

java org.testng.TestNG testng.xml java org.testng.TestNG testng-failed.xml

(replace org.testng.TestNG with your own runner since you seem to use a customized one).

1
votes

You could also create your own ITestListener (since you've got your own wrapper anyhow) that keeps track of the failures and then from that generate your own failures suite file that contains only the failed test. TestNG's listener/interceptor hooks are pretty good. At work we've extended TestNG using them several ways:

  • capture/playback of generated data sets
  • result logging to a database
  • customized test output (logs)
  • meta data such as IDs, descriptions, for data sets provided by a @DataProvider
  • runtime checks of environment dependent restrictions on test cases
1
votes

There are several methods to do this.

Are you using Eclipse for development? There is an Eclipse plugin for TestNG and I think it would be by far the easiest way for you to run specific tests. The plugin allows you to run suite, group, class or method of available test.

If not, I believe you can setup an ant task for launching the test(http://testng.org/doc/ant.html) and use attributes like "classfilesetref" to provide a list of test to run. You can specify the test in a separate file so you don't have to update the build.xml every time your run the test.

For installing testng Plugin.Just Follow the steps: 1-Go to "Help" Menu in Eclipse. 2-Select "Install New Software"". 3-Add"http://beust.com/eclipse."

It works in case of the error you specified I think you do not have the plugin installed within the Eclippse IDE

0
votes

Select the test method in the program and go to the top menu "Run" -> "Run" , or do CTRL + F11 , this will start the test independently of the XML test suite.