通过三种方式调用TestNG
- testing.xml
- ant
- command line
- Eclipse
- IntelliJ's IDEA
testing.xml sample:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Nopackage" >
<classes>
<class name="NoPackageTest" />
</classes>
</test>
<test name="Regression1">
<classes>
<class name="test.sample.ParameterSample"/>
<class name="test.sample.ParameterTest"/>
</classes>
</test>
</suite>
或者指定包名代替类名
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1" >
<packages>
<package name="test.sample" />
</packages>
</test>
</suite>
或者运行指定的组和方法included excluded
<test name="Regression1">
<groups>
<run>
<exclude name="brokenTests" />
<include name="checkinTests" />
</run>
</groups>
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<include name="testMethod" />
</methods>
</class>
</classes>
</test>
或者不按照指定顺序,需要更改preserve-order属性为false.
<test name="Regression1" preserve-order="false">
<classes>
<class name="test.Test1">
<methods>
<include name="m1" />
<include name="m2" />
</methods>
</class>
<class name="test.Test2" />
</classes>
</test>
command line sample:
假设环境变量中有TestNG。
java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
Option | Argument | Documentation |
---|---|---|
-configfailurepolicy | skip|continue | Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @Before* method fails. Default behavior is skip. |
-d | A directory | The directory where the reports will be generated (defaults to test-output). |
-dataproviderthreadcount | The default number of threads to use for data providers when running tests in parallel. | This sets the default maximum number of threads to use for data providers when running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition. |
-excludegroups | A comma-separated list of groups. | The list of groups you want to be excluded from this run. |
-groups | A comma-separated list of groups. | The list of groups you want to run (e.g. "windows,linux,regression"). |
-listener | A comma-separated list of Java classes that can be found on your classpath. | Lets you specify your own test listeners. The classes need to implement org.testng.ITestListener |
-usedefaultlisteners | true|false | Whether to use the default listeners |
-methods | A comma separated list of fully qualified class name and method. For example com.example.Foo.f1,com.example.Bar.f2. | Lets you specify individual methods to run. |
-methodselectors | A comma-separated list of Java classes and method priorities that define method selectors. | Lets you specify method selectors on the command line. For example: com.example.Selector1:3,com.example.Selector2:2 |
-parallel | methods|tests|classes | If specified, sets the default mechanism used to determine how to use parallel threads when running tests. If not set, default mechanism is not to use parallel threads at all. This can be overridden in the suite definition. |
-reporter | The extended configuration for a custom report listener. | Similar to the -listener option, except that it allows the configuration of JavaBeans-style properties on the reporter instance. Example: -reporter com.test.MyReporter:methodFilter=*insert*,enableFiltering=true You can have as many occurrences of this option, one for each reporter that needs to be added. |
-sourcedir | A semi-colon separated list of directories. | The directories where your javadoc annotated test sources are. This option is only necessary if you are using javadoc type annotations. (e.g. "src/test"or "src/test/org/testng/eclipse-plugin;src/test/org/testng/testng"). |
-suitename | The default name to use for a test suite. | This specifies the suite name for a test suite defined on the command line. This option is ignored if the suite.xml file or the source code specifies a different suite name. It is possible to create a suite name with spaces in it if you surround it with double-quotes "like this". |
-testclass | A comma-separated list of classes that can be found in your classpath. | A list of class files separated by commas (e.g. "org.foo.Test1,org.foo.test2"). |
-testjar | A jar file. | Specifies a jar file that contains test classes. If a testng.xml file is found at the root of that jar file, it will be used, otherwise, all the test classes found in this jar file will be considered test classes. |
-testname | The default name to use for a test. | This specifies the name for a test defined on the command line. This option is ignored if the suite.xml file or the source code specifies a different test name. It is possible to create a test name with spaces in it if you surround it with double-quotes "like this". |
-testnames | A comma separated list of test names. | Only tests defined in a <test> tag matching one of these names will be run. |
-testrunfactory | A Java classes that can be found on your classpath. | Lets you specify your own test runners. The class needs to implement org.testng.ITestRunnerFactory. |
-threadcount | The default number of threads to use when running tests in parallel. | This sets the default maximum number of threads to use for running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition. |
-xmlpathinjar | The path of the XML file inside the jar file. | This attribute should contain the path to a valid XML file inside the test jar (e.g. "resources/testng.xml"). The default is "testng.xml", which means a file called "testng.xml" at the root of the jar file. This option will be ignored unless -testjar is specified. |
或者在text文件中写入命令 c:\command.txt,然后让TestNG检索参数
C:> more c:\command.txt
-d test-output testng.xml
C:> java org.testng.TestNG @c:\command.txt
TestNG可以在Java虚拟机的命令行上传递属性
java -Dtestng.test.classpath="c:/build;c:/java/classes;" org.testng.TestNG testng.xml
Property | Type | Documentation |
---|---|---|
testng.test.classpath | 用分号分隔的包含测试类的一系列目录。 | TestNG将使用它来查找您的测试类,而不是类路径。如果您在XML文件中使用包标记,并且类路径中有很多类,其中大多数不是测试类,那么这将非常方便。 |
java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
如果您还指定一个testng.xml文件,那么指定应该运行哪些测试的命令行标志将被忽略,但-includedgroups和-exclude groups除外,它们将覆盖testng.xml中发现的所有组包含/排除。
Eclipse
Maven 插件安装好后
创建一个新的 TestNG 类
如果你现在有一个Java文件在编辑器中打开或如果你有在导航器中选择一个Java文件,向导的第一个页面将向您展示所有公共方法的类的列表,它会给你选择选择你想要测试的。您在此页上选择的每个方法都将包含在新TestNG类中,并带有一个引发异常的默认实现,因此您要记住实现它。
一旦创建了包含TestNG注释和/或一个或多个TestNG .xml文件的类,就可以创建TestNG启动配置。选择Run / Run…(或运行/调试…)菜单,并创建一个新的TestNG配置: