Java 我们如何在UnitTestCase中编写两个@beforeTest方法?
在我的测试用例中有两种方法。对于我的每个测试方法,我想使用两种不同的Java 我们如何在UnitTestCase中编写两个@beforeTest方法?,java,unit-testing,testng,Java,Unit Testing,Testng,在我的测试用例中有两种方法。对于我的每个测试方法,我想使用两种不同的@BeforeTest或@beforethod方法 我在单元测试类中编写了两个@BeforeMethod方法,但这两个方法都是针对每个单元测试方法执行的 那么,我们如何声明@BeforeMethod方法来分别执行特定的测试方法呢 我的单元测试类如下所示: public class MyUnitTest{ String userName = null; String password = null; // Meth
@BeforeTest
或@beforethod
方法
我在单元测试类中编写了两个@BeforeMethod
方法,但这两个方法都是针对每个单元测试方法执行的
那么,我们如何声明@BeforeMethod
方法来分别执行特定的测试方法呢
我的单元测试类如下所示:
public class MyUnitTest{
String userName = null;
String password = null;
// Method 1
@Parameters({"userName"})
@BeforeMethod
public void beforeMethod1(String userName){
userName = userName;
}
@Parameters({"userName"})
@Test
public void unitTest1(String userNameTest){
System.out.println("userName ="+userName);
}
// Method 2
@Parameters({"userName","password"})
@BeforeMethod
public void beforeMethod2(String userName,String password){
this.userName = userName;
this.password = password;
}
@Parameters({"userName","password"})
@Test
public void unitTest2(String userNameTest,String passwordTest){
System.out.println("userName ="+this.userName+" \t Password ="+this.password);
}
}
有没有办法做到:
beforethod1
方法是否仅对unitTest1()执行
beforethod2
方法是否仅对unitTest2()执行
您有两个选择:
- 可以使用方法参数声明@BeforeMethod,检查该方法的名称,如果是unitTest1,则调用beforeMethod1(),如果是unitTest2,则调用beforeMethod2()。这可能不是我的第一选择,因为如果更改方法的名称,它会有点脆弱
- 将这些方法及其before方法放在单独的类中,可能共享一个公共超类