差異處
這裏顯示兩個版本的差異處。
java:junit:tech:ignore [2015/10/21 23:17] tony |
java:junit:tech:ignore [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>java junit}} | + | |
- | ====== Conditionally ignore tests ====== | + | |
- | ===== Problem ===== | + | |
- | 雖然寫的是java程式,但並非所有功能都能夠跨平台執行;又或者測試僅針對某一種平台或情形等,而會有條件的去決定是否執行測試案例。 | + | |
- | ===== How to? ===== | + | |
- | ==== Ignore Annotation ==== | + | |
- | 最簡單的無條件式的忽略是使用@Ignore,通常用於debug或有原因的不想執行才會用上: | + | |
- | <code java> | + | |
- | @Ignore | + | |
- | public void test() { | + | |
- | // ... skip to run | + | |
- | } | + | |
- | </code> | + | |
- | {{:java:junit:tech:junit_cond_ignore_anno_ignore.png|}} | + | |
- | ==== Assume Class ==== | + | |
- | 最簡單的條件式忽略,可以在@Before宣告的方法中,宣告你要限制的條件。以這例子而言,只有在Windows上執行測試,才會真正的執行: | + | |
- | <code java> | + | |
- | @Before | + | |
- | public void setup() { | + | |
- | Assume.assumeTrue(Platform.isWindows()); | + | |
- | // ... skip to run | + | |
- | } | + | |
- | </code> | + | |
- | {{:java:junit:tech:junit_cond_ignore_assume.png|}}\\ | + | |
- | \\ | + | |
- | 以這種情況來說,放在@BeforeClass宣告的方法中,會更為洽當,僅會執行一次而不會每個@Test都執行: | + | |
- | <code java> | + | |
- | @BeforeClass | + | |
- | public static void classSetup(){ | + | |
- | Assume.assumeTrue(Platform.isLinux()); | + | |
- | // ... skip to run | + | |
- | } | + | |
- | </code> | + | |
- | \\ | + | |
- | 如果每個@Test有個別條件,可以將Assume放在@Test宣告的方法中: | + | |
- | <code java> | + | |
- | @Test | + | |
- | public void testNetShareGetInfo() { | + | |
- | Assume.assumeTrue(Platform.isWindows()); | + | |
- | // ... skip to run | + | |
- | } | + | |
- | </code> | + | |
- | {{:java:junit:tech:junit_cond_ignore_assume_in_test.png|}} | + | |
- | ===== Reference ===== | + | |
- | * [[http://stackoverflow.com/questions/1689242/conditionally-ignoring-tests-in-junit-4|conditionally-ignoring-tests-in-junit-4]] | + | |
- | * [[http://stackoverflow.com/questions/23410738/run-unit-tests-only-on-windows|run-unit-tests-only-on-windows]] | + | |
- | * [[https://code.google.com/p/junit-ext/|Junit-ext]] | + | |
- | ===== ===== | + | |
- | ---- | + | |
- | \\ | + | |
- | ~~DISQUS~~ | + |