maven使用TestNG
来源:互联网 发布:乐清人民法院淘宝拍卖 编辑:程序博客网 时间:2024/05/21 05:21
maven配置TestNG
如需要使用TestNG,需要在工程里添加依赖(可以替换你想使用的版本)
<dependencies> [...] <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.8</version> <scope>test</scope> </dependency> [...]</dependencies>
如果使用的TestNG的版本过低(<=5.11),依赖的书写形式如下:
<dependencies> [...] <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.11</version> <scope>test</scope> <classifier>jdk15</classifier> </dependency> [...]</dependencies>
注意:如果使用JDK1.4 JavaDoc注释进行TestNG测试,需要用jdk14替换上面的classifier属性中的jdk15
在pom中添加依赖是必须的步骤
XML文件使用套件
另一种替代方法是使用TestNG套件XML文件。这允许灵活配置的测试运行。以正常的方式创建这些文件,然后添加到成功的插件配置:
<plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> [...]</plugins>
配置将覆盖包括和不包括模式和运行所有测试套件的文件。
指定测试参数
TestNG测试可以接受参数@Parameters 注释。你也可以从Maven将参数传递到你TestNG测试,通过指定系统属性,eg:
<plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <systemPropertyVariables> <propertyName>firefox</propertyName> </systemPropertyVariables> </configuration> </plugin> [...]</plugins>
使用分组
TestNG允许将测试分组,分组后你可以选择执行一个多个组,用surefire带分组参数eg
<plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <groups>functest,perftest</groups> </configuration> </plugin> [...]</plugins>
同样,excludedGroups参数可以在执行的时候排除一些组,使之不执行
并行执行测试
TestNG允许并行执行测试用例,并行执行必须配合parallel参数,默认是5个线程,可以通过配置threadCount属性修改默认值。
eg:
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <parallel>methods</parallel> <threadCount>10</threadCount> </configuration> </plugin> [...]</plugins>
对于高并发,或需要快速评估线程和代码的测试是十分有用的。
TestNG 版本5.10+、插件版本2.19+并行测试时允许使用data provider
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <properties> <property> <name>parallel</name> <value>methods</value> </property> <property> <name>dataproviderthreadcount</name> <value>30</value> </property> </properties> </configuration> </plugin> [...]</plugins>
eg:
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <suiteXmlFiles> <file>src/test/resources/testng1.xml</file> <file>src/test/resources/testng2.xml</file> </suiteXmlFiles> <properties> <property> <name>suitethreadpoolsize</name> <value>2</value> </property> </properties> </configuration> </plugin> [...]</plugins>
使用自定义侦听器和报告
TestNG支持附加自定义侦听器,报告,注解和方法拦截器。默认情况下,TestNG附带一些基本的监听生成HTML和XML报告。
你可以配置多个自定义侦听器是这样的:
<dependencies>[...] <dependency> <groupId>your-testng-listener-artifact-groupid</groupId> <artifactId>your-testng-listener-artifact-artifactid</artifactId> <version>your-testng-listener-artifact-version</version> <scope>test</scope> </dependency>[...]</dependencies>[...]</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name><value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value> </property> <property> <name>reporter</name> <value>listenReport.Reporter</value> </property> </properties> </configuration> </plugin> [...]</plugins>
更多信息请参考testng官网
定制TestNG对象工厂
自从插件版本2.19和TestNG 5.7或更高版本你可以定制通过实现org.testng.IObjectFactory TestNG的对象工厂。并绑定类名。
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> [...] <properties> <property> <name>objectfactory</name><value>testng.objectfactory.TestNGCustomObjectFactory</value> </property> </properties> [...] </configuration> </plugin> [...]</plugins>
定制TestNG 运行工厂
从插件v2.19+和TestNG v5.9+,可以通过实现org.testng.ITestRunnerFactory实现定制TestNG runner factory,并绑定类名
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> [...] <properties> <property> <name>testrunfactory</name><value>testng.testrunnerfactory.TestNGCustomTestRunnerFactory</value> </property> </properties> [...] </configuration> </plugin> [...]</plugins>
根据”标签名称”运行测
测试定义的标签匹配配置中的标签将会被执行,否则不会被执行。在以下的7个用例中只会被执行两个:即InstallTest和ATest。测试标签a-t3在suite.xml不能够匹配,不执行任何测试。
</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> [...] <suiteXmlFiles> <file>src/test/resources/suite.xml</file> </suiteXmlFiles> <properties> <property> <name>testnames</name> <value>a-t1,a-t3</value> </property> </properties> [...] </configuration> </plugin> [...]</plugins>
The test suite ‘suite.xml’ :
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Component Tests" verbose="2" annotations="JDK"> <test name="a-t1" preserve-order="true" > <classes> <class name="server.InstallTest" /> <class name="server.ATest" /> </classes> </test> <test name="a-t2" preserve-order="true" > <classes> <class name="server.SCHTest" /> <class name="server.PRGTest" /> <class name="server.SIBBTest" /> <class name="server.EDNTest" /> <class name="server.PPVTest" /> </classes> </test></suite>
同时运行TestNG和JUnit
TestNG 6.5.1 以上的版本支持同时运行TestNG 和 JUnit 4.x在同一个maven项目中 .
<dependencies> [...] <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.4</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> [...]</dependencies>
Testing和junit同时运行需要注意版本
备忘(还未完全整理,可能会有不恰当之处)
- maven使用TestNG
- TestNG 命令行使用 Maven项目结合TestNG
- allure report+maven+testng集成使用
- 使用Maven + testNG跑测试代码
- maven中使用testng需要关联
- 使用Maven搭建Eclipse+Selenium_Webdriver+Testng+Junit自动化测试环境
- 在Eclipse中使用Maven配置WebDriver+Testng(一)
- 在Eclipse中使用Maven配置WebDriver+Testng(二)
- Maven与Ant使用reportNG代替testng生成的报表
- 接口自动化测试(使用testng,reportng,maven,jenkins)
- 使用ReportNg生成测试报告(Maven+Appium+TestNg+ReportNg)
- 使用maven执行testng特定组时,报错Nullpointer
- TestNG使用
- testng使用
- maven运行testNG
- maven执行testng测试
- Maven运行TestNG
- selenium+testNG+maven
- Java基础补习Day6
- Error: Expected resource of type raw [ResourceType]
- CSS 面包屑导航栏
- matlab小程序:求矩阵的行列式
- 如何取消选择easyui-combogrid的下拉数据
- maven使用TestNG
- 错排列
- map()和reduce()的细节
- CodeForces 299 C.Weird Game(博弈论)
- PAT B1009.说反话
- SQL Server监控
- java菜鸟之路
- 《TP5.0学习笔记---URL和路由篇》
- 大数据--一致性哈希算法