TestNG:重复一个方法

TestNG:重复一个方法,testng,Testng,我有一个测试用例,它要求我多次注销。我希望注销成为它自己的测试/方法。除了每次创建注销方法外,还有其他方法可以调用该方法吗 @Test(priority = 1, groups = {"Regression"}) public void createaccount() throws Exception {} @Test(priority = 2, groups = {"Regression"}) public void addpayment() throws Exception {} @Test

我有一个测试用例,它要求我多次注销。我希望注销成为它自己的测试/方法。除了每次创建注销方法外,还有其他方法可以调用该方法吗

@Test(priority = 1, groups = {"Regression"})
public void createaccount() throws Exception {}
@Test(priority = 2, groups = {"Regression"})
public void addpayment() throws Exception {}
@Test(priority = 3, groups = {"Regression"})
public void logout() throws Exception {}
@Test(priority = 4, groups = {"Regression"})
public void login() throws Exception {}
@Test(priority = 5, groups = {"Regression"})
public void logout() throws Exception {}

可以通过使用注释来实现这一点。然后在每个测试方法之后调用此方法。如果只想对某些测试方法运行它,可以传入以获取测试方法名称。您可能还希望包括
alwaysRun=true
,这样即使测试失败,注销方法也会运行

大概是这样的:

@AfterMethod(alwaysRun=true)
public void logout(ITestResult result) {
    if (result.getName().equalsIgnoreCase("addpayment")) {
            // do logout
    }
}

希望这有帮助

使用
@AfterMethod
有一个问题。您需要将它添加到每个测试类中。因此,如果您的所有测试类都需要有选择地执行某些方法,那么您可以通过实现testng listener
org.testng.IInvokedMethodListener
并将其连接到[使用
@Listener
注释或使用
标记或使用服务加载程序。有关更多详细信息,请参阅]

检查
ITestResult
对象并依赖方法名称会出现一个问题,即如果明天将
@Test
方法重构为其他方法,则需要记住在选择的执行位置对其进行更改。您可以改为创建自定义注释,例如
@Logout
,检查侦听器中是否存在此注释,如果存在,则让它调用该方法

下面是我所指的示例实现

以下是标记注释的外观:

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
/**
 * Marker annotation that signifies that a {@link ILogout#signout()} operation has to be performed
 * for the current method.
 */
public @interface Logout {
}
下面是一个新的接口,它定义了注销机制应该是什么样子。我们稍后将利用这个接口从实际的测试类触发注销

/**
 * Defines the logging out capabilities that any test class may possess.
 */
public interface ILogout {
    /**
     * Perform the actual sign out.
     */
    void signout();
}
test类将实现上面的接口
ILogout
,因为它希望对一些
@test
方法执行选择性注销

import org.testng.annotations.Test;

public class MyTestClass implements ILogout {

    @Test(priority = 1, groups = {"Regression"})
    @Logout //We need a selective logout to be performed for this method.
    public void createaccount() throws Exception {
    }

    @Test(priority = 2, groups = {"Regression"})
    public void addpayment() throws Exception {
    }

    @Test(priority = 3, groups = {"Regression"})
    public void logout() throws Exception {
    }

    @Test(priority = 4, groups = {"Regression"})
    public void login() throws Exception {
    }

    @Override
    public void signout() {

    }
}
最后,TestNG侦听器如下所示:

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class MyListener implements IInvokedMethodListener {
    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        Logout logout = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Logout.class);
        if (logout == null) {
            return;
        }
        //Try and process only if the method that was just now executed by TestNG has the
        //marker annotation.
        Object instance = method.getTestMethod().getInstance();
        //We were asked to logout, but check if the corresponding test class has an implementation
        //for performing the actual logout. If yes, then invoke that directly.
        if (instance instanceof ILogout) {
            ((ILogout) instance).signout();
        }
    }
}

此解决方案将确保即使方法名称经过重构,您的代码也不会受到影响。

如果您需要确保它仅在某些方法之后运行,您可以使用此方法检查是否需要注销@Aarjav是的,我需要在某些方法之后运行它。我查看了文档,但我不太明白。您能给我举个例子吗e?谢谢!@HaC谢谢,但我不想在每次测试方法之后调用注销方法。它必须在某些方法之后运行。我明白了,我误解了这个问题。然后是的,@Aarjav使用ITestResult是正确的。我更新了我的答案。