[英]Does JUnit 5 guarantee the happens-before relationship for ordered test methods?
[英]JUnit: @Before only for some test methods?
我有一些常見的設置代碼,我已將其分解為標有@Before
的方法。 但是,不必為每個測試都運行所有這些代碼。 有沒有辦法標記它,以便@Before
方法只在某些測試之前運行?
只需將不需要安裝代碼的測試移到單獨的測試類中即可。 如果您有一些測試通用的其他代碼,可以幫助保留這些代碼,請將其移到幫助程序類中。
或使用TestNG 。 它使您可以更好地控制測試。
不確定@Before,但是最近我想出了一個策略,可以有選擇地運行@After塊。 實施是直接的。 我將一些標志設置為默認值,作為測試類的一部分。 它們被重置為@Before類中的默認值。 在該類中,我需要做特定於標志的事情,我在@標志中設置了這些標志並檢查了標志值以完成各自的工作。
完全同意將相關代碼移至內部類的觀點。 所以這就是我所做的。
這是代碼:
class Testing {
@Test
public void testextmethod1() {
System.out.println("test ext method 1");
}
@Nested
class TestNest{
@BeforeEach
public void init() {
System.out.println("Init");
}
@Test
public void testmethod1() {
System.out.println("This is method 1");
}
@Test
public void testmethod2() {
System.out.println("This is method 2");
}
@Test
public void testmethod3() {
System.out.println("This is method 3");
}
}
@Test
public void testextmethod2() {
System.out.println("test ext method 2");
}
}
這是輸出
test ext method 1
test ext method 2
Init
This is method 1
Init
This is method 2
Init
This is method 3
注意:我不確定Junit4是否支持此功能。 我在JUnit5中這樣做
JUnit 4.12 提供 Enclosed Runner 之類的
@RunWith(Enclosed.class)
public class GlobalTest{
@RunWith(MockitoJUnitRunner.class)
public class InnerTest{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.