我在http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/找到了比較結果。如何在JUnit中準備類似於@BeforeTest的東西?在JUnit中相當於TestNG @BeforeTest
2
A
回答
1
據我所知,BeforeTest指定了一個方法,該方法在一組測試運行之前運行,並且可以爲該組測試定義。除了普通套件外,JUnit中沒有相同的分組,所以您必須定義一個套件並在套件中正常使用@BeforeClass
和@AfterClass
。
如果您想要更復雜的行爲,請參閱TestRule,特別是ExternalResource。
1
不像TestNG那樣熟悉,但根據我的理解,您可以使用Categories
,Suite
和@BeforeClass
/@AfterClass
的組合做類似處理。
例如:
import org.junit.BeforeClass;
import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Categories.class)
@SuiteClasses(MyClassTest.class)
@IncludeCategory(IntegrationTest.class)
public class StaticTests {
@BeforeClass
public static void setUp() {
System.out.println("Hello, World!");
}
}
然後你的測試標誌事情@Category(IntegrationTest.class)
內,你就會有測試,從多個不同的測試類,你可以到處跑初始化的邏輯分組。
類別允許您標記包含在套件中的特定測試,儘管它也可能(如果您按照類首先分開)只是將相關的測試包括在套件中或讓它們從基類繼承它具有該配置。哪一個最好取決於你想如何分組你的測試和你的特定用例。
由於Matthew Farwell提到,也有TestRule
s給你一個更細粒度的控制圍繞一套測試設置。
相關問題
- 1. FAILED CONFIGURATION:@BeforeTest beforeTest。 MAVEN; TestNG,硒
- 2. 失敗配置:@BeforeTest beforeTest上運行該代碼TestNG的
- 3. TestNG:@BeforeClass和@BeforeTest的優先級
- 4. CLI中的JUnit測試相當於
- 5. TestNG,Junit和log4j
- 6. 在testng的@BeforeTest中訪問spring上下文
- 7. TestNG代替DukeScript中的JUNIT
- 8. JUnit 4與TestNG 6
- 9. 在Windows Shell中使用Junit或TestNG
- 10. TestNG如何在beforetest繼承失敗時跳過測試
- 11. JUnit AfterClass在Midje Clojure中是否相當於?
- 12. 一起使用JUnit和TestNG
- 13. 在TestNG測試中顯示來自@BeforeTest註釋的安裝日誌信息
- 14. @Before Suite和@BeforeTest方法在TestNG中執行時不會被調用
- 15. TestNg的基礎類@BeforeTest只發生一次每夾具
- 16. 你能更好地解釋TestNG的@BeforeTest註解的語義嗎?
- 17. 無法用TestNG使用beforeTest註解來啓動瀏覽器
- 18. 在AIX上運行JUnit或testng TC
- 19. @BeforeTest beforeTest(「WIN8」,「firefox」,「http://10.xxx:4467/wd/hub」)java.net.MalformedURLException:無協議:remoteurl
- 20. CGColorGetComponents在Android中相當於
- 21. 相當於ungetc在java中
- 22. 在Python中相當於JavaBean
- 23. invalidateLayout在iOS8中相當於
- 24. 在R中,相當於upper_bound()
- 25. 在mssql中相當於initcap
- 26. FormCloseQuery在MFC中相當於
- 27. 在JS中相當於RSACryptoServiceProvider
- 28. 在WPF中相當於QStackedLayout
- 29. 在Java中相當於STXXL
- 30. SDO_GEOMETRY在Postgresql中相當於