後是否有可能運行已方法標註了@Before
類JUnit的@Test
方法,但是忽略只對該測試的@Before
方法?運行JUnit @Test忽略@前/ @
編輯:如果JUnit支持此功能,而不是解決方法,我很感興趣。我知道解決方法如將測試移到另一個類中或刪除註釋,並在每種測試方法中手動調用setUp()
。
假設在一個類中有30個測試,並且其中29個@Before
真的簡化了測試初始化,但是對於其中一個(或多個)它是無用的/它使事情變得複雜。
public class MyTestClass {
@Before
public void setUp() {
//setup logic
}
@Test
public void test1() {
//[...]
}
@Test
public void test2() {
//[...]
}
//more tests here
@Test(ignoreBefore = true, ignoreAfter = true //false by default)
//something equivalent to this
public void test20() {
//[...]
}
}
您可以在'@ Before'和'@ After'註釋中將'test20'移動到單獨的類中嗎? – user1516873
@ user1516873從技術上講,是的,但我想在一個測試類中保留一個類的所有測試。當這個類被設計/開發時,測試不被認爲是重構,也不是解決方案。此外,單元測試的定義表明它們是獨立的,可以分開運行。 – m3th0dman