「BeforeTest」只打印一次,而不打印兩次。我究竟做錯了什麼?
***對不起。我沒有注意到你寫的是@BeforeTest,但是在你的示例中@BeforeTest幾乎等於@BeforeClass,並且更好地使用@BeforeClass,當你沒有測試類時。
@BeforeClass」應該在你的測試方法,而不是不同!
//Example
package test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Tests {
private String bClass;
private String bMethod1;
private String bMethod2;
@BeforeClass
public void beforeClass() {
bClass = "BeforeClass was executed once for this class";
}
@BeforeMethod
public void beforeMetodTest1() {
bMethod1 = "It's before method for test1";
}
@Test
public void test1() {
System.out.println(bClass);
System.out.println(bMethod1);
}
@BeforeMethod
public void beforeMethodTest2() {
bMethod2 = "It's before method for test2";
}
@Test
public void test2() {
System.out.println(bClass);
System.out.println(bMethod2);
}
}
@BeforeClass將在這個類中執行一次,在你所有的測試方法同一個類中聲明。@BeforeMethod將之前的測試方法執行,在此之前寫它
@BeforeClass可能只是一個在測試類,差異@BeforeMethod!(如果它是一些@BeforeClass,它們輪流執行,但它不是一個正確的組成測試)
P.S.對不起,我的英文:)