TestNG 在不同的测试类之间传递测试参数 [英] TestNG pass test parameter between different test class
本文介绍了TestNG 在不同的测试类之间传递测试参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法传递动态生成的数据并将它们传递给不同的测试类/套件?
Is there a way to pass a dynamically generated data and pass them to different test classes/suites?
我拥有的是以下内容:用户名/密码对由 TestUtil.signUpNewAccount() 创建;我想将此帐户对象传递给其他测试类,以便他们的测试方法可以使用它.
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(){//使用 newUserAccount .... };
- Test1Class.test2(){//使用 newUserAccount .... };
- Test2Class.test1(){//使用 newUserAccount .... };
- Test2Class.test2(){//使用 newUserAccount .... };
推荐答案
我最终使用了 TestNG 的 ITestContext.对于需要共享动态生成数据的测试类,我有一个抽象类,用@BeforeClass
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
}
}
这篇关于TestNG 在不同的测试类之间传递测试参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文