3 回答

TA貢獻1853條經驗 獲得超9個贊
要為您的測試用例設置前提條件,您可以使用類似這樣的東西 -
@Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}

TA貢獻1846條經驗 獲得超7個贊
如果您需要在所有測試開始之前運行該方法,則應該使用注釋@BeforeClass,或者如果您需要每次執行該類的測試方法時都執行相同的方法,則必須使用@Before
鐵
@Before
public void executedBeforeEach() {
//this method will execute before every single test
}
@Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}

TA貢獻2011條經驗 獲得超2個贊
您可以使用測試套件。
測試套件
@RunWith(Suite.class)
@Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
@BeforeClass
public static void setup() {
// the setup
}
}
并且,測試類
public class Test2Class {
@Test
public void test2() {
// some test
}
}
public class TestClass {
@Test
public void test() {
// some test
}
}
或者,您可以有一個處理設置的基類
public class TestBase {
@BeforeClass
public static void setup() {
// setup
}
}
然后測試類可以擴展基類
public class TestClass extends TestBase {
@Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
@Test
public void test() {
// some test
}
}
但是,每次執行時,這都會為其所有子類調用該setup方法。TestBase
添加回答
舉報