TestNG教程

TestNG @AfterMethod

TestNG @AfterMethod 注释

@AfterMethod 注释特定于类而不是 XML 文件。 @AfterMethod 注释的方法将在每个测试方法执行后调用。假设有四个测试方法,意味着@AfterMethod注解的方法会被执行四次。
我们通过一个例子来理解@AfterMethod注解
第 1 步: 打开 Eclipse。
第 2 步: 我们创建一个简单的 Java 项目,其中包含 @AfterMethod 注释方法。
After_Method.java
package com.lidihuo;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class After_Method 
{
@AfterMethod
public void after_method()
{
System.out.println("this method will be invoked after the execution of each test method");
}
@Test
public void c_programmers()
{
System.out.println("I am a C programmer");
}
@Test
public void java_programmers()
{
System.out.println("I am a java programmer");
}
@Test
public void dotnet_developer()
{
System.out.println("I am a .Net Developer");
}
}
    
第 3 步: 现在,我们创建一个 testng.xml 文件来配置 After_Method 类。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test_suite">
<test name="Before Methods">
<classes>
<class name="com.lidihuo.After_Method"/>
</classes>
</test> <!--Test-->
</suite> <!--Suite-->
    
第 4 步: 运行 testng.xml 文件。右键单击 testng.xml 文件并将光标向下移动到 Run As,然后单击 1 TestNG Suite。
输出
TestNG @AfterMethod Annotation
注意: TestNG 按字母顺序执行测试方法。
在上面的例子中,首先运行c_programmers()方法,然后执行@AfterMethod注解方法,然后运行dotnet_developer()方法,然后执行@AfterMethod注解方法,最后java_programmers()方法运行,然后@AfterMethod注解的方法会被执行。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4