TestNG pass test parameter between different test

2019-09-02 17:31发布

Is there a way to pass a dynamically generated data and pass them to different test classes/suites?

What I have is the following: A username/password pair is created by TestUtil.signUpNewAccount(); and I would like to pass this account object to other test classes so that their test methods can use it.

  • Test1Class.test1(){// use newUserAccount .... };
  • Test1Class.test2(){// use newUserAccount .... };
  • Test2Class.test1(){// use newUserAccount .... };
  • Test2Class.test2(){// use newUserAccount .... };

标签: testng
2条回答
爷的心禁止访问
2楼-- · 2019-09-02 18:04

I would recommend to use @Factory Factory allows you to create different test instances with different parameters http://testng.org/doc/documentation-main.html#factories

查看更多
做自己的国王
3楼-- · 2019-09-02 18:11

I ended up using TestNG's ITestContext. For test classes that requires the shared dynamically generated data, I have an abstract class that initializes needed data with @BeforeClass

public abstract class GenericWebTest{
    protected UserAccount ua;
    @BeforeClass
    public void BeforeClass(ITestContext ctx){
        if(ctx.getAttribute("username") == null){
            UserAccount ua = UserUtil.newSignUp();
            ctx.setAttribute("username", ua.getUsername);
            ctx.setAttribute("password", ua.getPassword);
        }
        us = new UserAccount(ctx.getAttribute("username"), ctx.getAttribute("password"));
    }
}

public class MemberPageTest extends GenericWebTest {
    @Test
    public void test1(){
        MemberPage mp = new MemberPage();
        mp.login(ua); //login using the already created user account
    }
}
查看更多
登录 后发表回答