简体   繁体   English

在TestNG中重写@BeforeMethod

[英]Override @BeforeMethod in TestNG

I have a base class for all my tests with defined @BeforeMethod which fits to most of the tests (I am testing admin UI so I want to usually log in as an administrator before test and log out after it) - but sometimes I would like to either skip this method or override it with different one in the class which is extending my base class. 我的所有测试都有一个基类,其中定义的@BeforeMethod适合大多数测试(我正在测试管理员UI,因此我通常希望在测试之前以管理员身份登录,然后在测试之后注销)-但有时我想跳过此方法或在扩展我的基类的类中使用其他方法覆盖它。

public class AbstractWebDriverTest {

    @BeforeMethod
    public void preparePage() {
        driver.get("somePage");
        signInAsAdmin();
    }

}

public class TestConfigWithOtherUser extends AbstractWebDriverTest {

    //now I want to skip before method and sign as a different user instead

    @Test
    public void someTestWithDifferentUser() { }

}

So I guess I could solve this with using groups somehow but is there a way how to override the init method? 所以我想我可以通过某种方式使用组来解决此问题,但是有一种方法可以覆盖init方法吗?

I would go either by grouping tests as you stated in your question or by exception. 我可以按照您在问题中所说的对测试进行分组,也可以例外。 If just few cases are the exception, your test in that cases must expect, that you start as admin. 如果只有少数几种情况例外,则在这种情况下的测试必须期望您以管理员身份开始。 My idea in Java Pseudocode: 我在Java伪代码中的想法:

 @Test
 public void someTestWithDifferentUser() {
  logout();
  logInAsDifferentUser();
  doSomeStuff();
 }

My other Idea is to introduce User class which could look like this: 我的另一个想法是介绍类似于下面的User类:

public class User{
  private String username;
  private String password;

  public User(String username, String password){
    this.username = username;
    this.password = password;
  }

  public String getUsername(){
    return username;
  }   

  public String getPassword(){
    return password;
  }

}

Then, in your tests you would have one global variable: 然后,在测试中,您将拥有一个全局变量:

public static final User DEFAULT_USER = new User("admin","AdminSecretPassword");

and your tests would look like this: 您的测试将如下所示:

@Test
public void doAdminStuff(){
 loginAsUser(DEFAULT_USER);
 doAdminStuff;
}

@Test
public void doUserStuff(){
 loginAsUser(new User("testytester","testytest"));
 doUserStuff();
}

Where the @BeforeMethod would avoid the login completly. @BeforeMethod将完全避免登录。

You can use Listeners interface to override default behaviour. 您可以使用Listeners界面来覆盖默认行为。 Refer this post which had a similar requirement to yours. 请参阅帖子里面也有类似的要求,你的。

public class SkipLoginMethodListener implements IInvokedMethodListener {

private static final String SKIP_GROUP = "loginMethod";

@Override
public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
    ITestNGMethod method = invokedMethod.getTestMethod();
    if (method.isAfterMethodConfiguration() || method.isBeforeMethodConfiguration()) {
        for (String group : method.getGroups()) {
            if (group.equals(SKIP_GROUP)) {
                System.out.println("skipped " + method.getMethodName());
                throw new SkipException("Configuration of the method " + method.getMethodName() + " skipped");
            }
        }
    }
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM