TestNG-TestNG annotations

5 篇文章 0 订阅


TestNG have many annotations,like @Test,@AfterTest that we saw in the previous section.But what's the mean of each annotation?

  Configuration information for a TestNG class: 

@BeforeSuite: 
The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.

And here is the codes try to expain these annotations

<pre name="code" class="java">package test;
import org.testng.annotations.*;

public class MatchGame {

	public static void main(String[] args) {
		
		System.out.println("hello world");

	}
	@Test
	public void testcase1(){
		System.out.println("in test case1");
	}
	@Test
	public void testcase2(){
		System.out.println("in test case2");
	}
	
	@BeforeMethod
	public void beforemethod(){
		System.out.println("in beforemethod");
	}
	
	@AfterMethod
	public void aftermethod(){
		System.out.println("in aftermethod");
	}
	@BeforeClass
	public void beforeclass(){
		System.out.println("in beforclass");
	}
	
	@AfterClass
	public void afterclass(){
		System.out.println("in afterclass");
	}
	
	@BeforeTest
	public void beforetest(){
		System.out.println("in beforetest");
	}
	
	@AfterTest
	public void aftertest(){
		System.out.println("in aftertest");
	}
	
	@BeforeSuite
	public void beforesuit(){
		System.out.println("in beforesuit");
	}
	
	@AfterSuite
	public void aftersuit(){
		System.out.println("in aftersuit");
	}

}

The result is:

 
[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1016307744\testng-customsuite.xml


in beforesuit
in beforetest
in beforclass
in beforemethod
in test case1
in aftermethod
in beforemethod
in test case2
in aftermethod
in afterclass
in aftertest
in aftersuit
As you see,The BeforeSuit and AfterSuit run only once,between each BeforeMethon and AfterMethon run each test case.

the arguments:
alwaysRun:<span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; background-color: rgb(239, 239, 239);">For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method wi                  ll be run regardless of what groups it belongs to.   </span><br style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;" /><span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; background-color: rgb(239, 239, 239);">                  For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or                   was skipped.</span>
<span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; background-color: rgb(239, 239, 239);">
</span><table style="border: none; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;"><tbody><tr style="border: none;"><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"> </td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"><tt>dependsOnGroups</tt></td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;">The list of groups this method depends on.</td></tr><tr style="border: none;"><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"> </td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"><tt>dependsOnMethods</tt></td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;">The list of methods this method depends on.</td></tr><tr style="border: none;"><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"> </td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"><tt>enabled</tt></td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;">Whether methods on this class/method are enabled.</td></tr><tr style="border: none;"><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"> </td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"><tt>groups</tt></td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;">The list of groups this class/method belongs to.</td></tr><tr style="border: none;"><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"> </td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;"><tt>inheritGroups</tt></td><td style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 9pt; border: none; background-color: rgb(239, 239, 239); padding: 3px 4px; background-position: initial initial; background-repeat: initial initial;">If true, this method will belong to groups specified in the @Test annotation at the class level.</td></tr></tbody></table>

   

<span style="font-family:Lucida Grande, Tahoma, Verdana, Arial, sans-serif;"><span style="background-color: rgb(239, 239, 239);"></span></span><pre name="code" class="java"><span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; background-color: rgb(239, 239, 239);">for example:@Test(enable=disabled,groups={"aaa","bbb"},dependsOnGroups{"aaa"})</span>
</pre><pre name="code" class="java"><span style="font-family:Lucida Grande, Tahoma, Verdana, Arial, sans-serif;"><span style="background-color: rgb(239, 239, 239);"></span></span>


 
<span style="font-family:Lucida Grande, Tahoma, Verdana, Arial, sans-serif;"><span style="background-color: rgb(239, 239, 239);"><span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;">
</span></span></span>
<span style="font-family:Lucida Grande, Tahoma, Verdana, Arial, sans-serif;"><span style="background-color: rgb(239, 239, 239);"><span style="font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;">@DataProvider:      Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.</span></span></span>
  name The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
  parallel If set to true, tests generated using this data provider are run in parallel. Default value is false.
for example:

  dependsOnGroups The list of groups this method depends on.
  dependsOnMethods The list of methods this method depends on.
  enabled Whether methods on this class/method are enabled.
  groups The list of groups this class/method belongs to.
  inheritGroups If true, this method will belong to groups specified in the @Test annotation at the class level.

  name The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
  parallel If set to true, tests generated using this data provider are run in parallel. Default value is false.

//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   "Cedric"new Integer(36) },
   "Anne"new Integer(37)},
 };
}
 
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);
}




@Parameters Describes how to pass parameters to a @Test method.
     

@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
  System.out.println("Invoked testString " + firstName);
  assert "Cedric".equals(firstName);
}

     

<suite name="My suite">
  <parameter name="first-name"  value="Cedric"/>
  <test name="Simple example">
  <-- ... -->







@Test Marks a class or a method as part of the test.
  alwaysRun If set to true, this test method will always be run even if it depends on a method that failed.
  dataProvider The name of the data provider for this test method.
  dataProviderClass The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
  dependsOnGroups The list of groups this method depends on.
  dependsOnMethods The list of methods this method depends on.
  description The description for this method.
  enabled Whether methods on this class/method are enabled.
  expectedExceptions The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
  groups The list of groups this class/method belongs to.
  invocationCount The number of times this method should be invoked.
  invocationTimeOut The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
  priority The priority for this test method. Lower priorities will be scheduled first.
  successPercentage The percentage of success expected from this method
  singleThreaded If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods". This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
  timeOut The maximum number of milliseconds this test should take.
  threadPoolSize The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount. 
Note: this attribute is ignored if invocationCount is not specified

  dependsOnGroups The list of groups this method depends on.
  dependsOnMethods The list of methods this method depends on.
  enabled Whether methods on this class/method are enabled.
  groups The list of groups this class/method belongs to.
  inheritGroups If true, this method will belong to groups specified in the @Test annotation at the class level.
  name The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
  parallel If set to true, tests generated using this data provider are run in parallel. Default value is false.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]中提到了相同的错误信息,即无法访问org.testng.annotations.Test,类文件具有错误的版本55.0,应为52.0。这个错误通常是由于测试框架TestNG的版本与项目中其他依赖项的版本不兼容导致的。解决这个问题的方法是降低TestNG的版本或升级项目中其他依赖项的版本,以确保它们之间的兼容性。 引用\[3\]中提供了一个TestNG的依赖项配置示例。你可以在项目的pom.xml文件中添加这个依赖项,并指定一个与项目其他依赖项兼容的版本号。例如,你可以将版本号设置为6.14.3。然后,使用Maven或其他构建工具重新构建项目,以确保依赖项被正确下载和引入。 总结起来,要解决这个问题,你可以尝试以下几个步骤: 1. 检查项目中TestNG的版本与其他依赖项的版本是否兼容。 2. 如果不兼容,降低TestNG的版本或升级其他依赖项的版本。 3. 在项目的pom.xml文件中添加TestNG的依赖项配置,并指定一个与项目其他依赖项兼容的版本号。 4. 使用构建工具重新构建项目,以确保依赖项被正确下载和引入。 希望这些步骤能帮助你解决问题。如果还有其他疑问,请随时提问。 #### 引用[.reference_title] - *1* [java: 无法访问org.testng.annotations.Test 错误的类文件](https://blog.csdn.net/m0_66298628/article/details/128054577)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [java: 无法访问org.testng.annotations.Test](https://blog.csdn.net/weixin_46022090/article/details/127241979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值