官方文档 : 供查阅 http://testng.org/doc/documentation-main.html#methods
1. src/main/resource -> suite : this is testng.xml .
By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false ,eg: change <
test
name
=
"Regression1"
> to <
test
name
=
"Regression1" preserve-order ="false"
>
example :
< suite name = "ProjectTest" verbose = "1" > |
< class name = "NoPackageTest" /> |
< test name = "Regression1" > |
<groups>
<run>
<exclude name = "brokenTests">
<include name = "checkinTests">
</run>
</groups>
< class name = "test.sample.ParameterSample" /> |
< class name = "test.sample.ParameterTest" /> |
<methods>
<include name= "testMethod"/>
</methods>
2.
Test Methods:
Test methods are annotated with @Test. Methods annotated with @Test that happen to return a value will be ignored, unless you set allow-return-values to true in your testng.xml:
<
suite
allow-return-values
=
"true"
> or <
test
allow-return-values
=
"true"
>
Test groups:
@Test (groups = { "functest" , "checkintest" }) |
public void testMethod1() { |
@Test (groups = { "functest" , "checkintest" } ) |
public void testMethod2() { |
@Test (groups = { "functest" }) |
public void testMethod3() { |
Invoking TestNG with (也可以用正则表达式, 比如 <include name "func.*" />)
< include name = "functest" /> |
< class name = "example1.Test1" /> |
3.Parameters
If you are using simple values for your parameters, you can specify them in your testng.xml:
@Parameters ({ "first-name" }) |
public void testSingleString(String firstName) { |
System.out.println( "Invoked testString " + firstName); |
assert "Cedric" .equals(firstName); |
In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml:
< parameter name = "first-name" value = "Cedric" /> |
< test name = "Simple example" > |
4. Dependencies
@Test(dependsOnMethods = {"serverStartOk"}) // @Test(dependsOnGroups={"init.*"})
public void method1(){}
For dependsOnMethods, if you are depending on a method which happens to have several overloaded versions, all the overloaded methods will be invoked. If you only want to invoke one of the overloaded methods, you should use dependsOnGroups.
Alternatively, you can specify your group dependencies in the testng.xml file. You use the <dependencies> tag to achieve this:
< group name = "c" depends-on = "a b" /> |
< group name = "z" depends-on = "c" /> |
5.Class level annotations
The @Test annotation can be put on a class instead of a test method:
The effect of a class level
@Test
annotation is to make all the public methods of this class to become test methods even if they are not annotated. You can still repeat the
@Test
annotation on a method if you want to add certain attributes.
6. Parallel tests,classes and methods
- parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
- parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
- parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
- parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
7. TestNG Listeners