2020-06-11追è¨
æ¬ç¨¿ã®å¯¾è±¡ã¯JUnit4ã§ããJUnit5ã§åçã®ã¢ããã¼ã·ã§ã³`@DisplayName(value)` ã追å ããã¦ããããããã®ãããªå°ç´°å·¥ã¯å¿ è¦ããã¾ããã
çºç«¯ã¯[twitter:@ayato_p]ããã®ãã¤ã¼ãã§ãããã¾ãããJUnitã®ãã¹ãã¡ã½ããåãæ¥æ¬èªã§æ¸ããã®ã¯ããã»ã©å¥ç°ãªè©±ã§ã¯ããã¾ããã
JUnitå®è·µå
¥éã§ããæ¥æ¬èªã®ã¡ã½ããåã使ãã¡ãªãããã¨è¨ãã³ã©ã ãããããµã³ãã«ã³ã¼ãã®ãã¹ãã¡ã½ããåã¯å¹³ç¶ã¨æ¥æ¬èªã§æ¸ããã¦ãã¾ãã
[isbn:978-4774153773:detail]
ãããæ¥æ¬èªã¡ã½ããåãªã®ã§ãå½ç¶ã®ããã«ã¡ã½ããåã«ç±æ¥ããåé¡ãå¹¾ããããã¾ããããè©°ã¾ãã®ã¯ããããã使ããªããã§ãããããã¯VMå¼æ°ãªã©ã§è§£æ±ºåºæ¥ã¾ãã
ä»ã«åç´ãªã¨ããã§ã¯å¥èªç¹ãä¸é»ã使ããªãæã§ãããããå°æ°ç¹ã使ãã¾ããããæ°åã§ã¯ããããã¨ãåºæ¥ã¾ãããããããå¶ç´ã«å¼ã£ããã£ãã¨ã㯠_ ã§ä»£ç¨ããã®ã常éã§ããã使ãããªã使ãããã§ãããã
ããGroovyã§
@Test public void "ããGroovyãªãä½ã»è£ã ãï¼"() { fail("ãã±ã¼ã") }
ããããé»ããâ¦â¦æ£ç´ãç¾å®è§£ã ã¨æãã¾ãã使ãããªããGroovyã«ä¸èªç±ããªã人ãªããJUnitãGroovyã§æ¸ãã®ã¯ããã»ã©æªãé¸æã§ã¯ç¡ãã¨æãã¾ããIDEãã©ãã ããµãã¼ããã¦ããããåé¡ã¯ããã¾ãããæã£ããããã£ã¦ãããæã¯ããã¾ãã
Groovyã§JUnitã使ããã¨ã®æ大ã®ã¡ãªããã¯ãããããPowerAssertã«ãªãã¾ããä»ã«ã¯Spockã¨ã®é£æºã»åãæ¿ããã·ã¼ã ã¬ã¹ã«è¡ãããªã©ã§ãããããã£ããstatic importå¿ãã¦MissingMethodExceptionã¨ããªã£ãããã¦ãJavaã§ã¯è¦æ £ããªãä¾å¤ãåããã¨ãå¤ãã®ã§ãGroovyã«ä¸æ £ããªäººãæ¬çªã§ãã£ã¬ã³ã¸çã«ä½¿ããã®ã§ã¯ããã¾ãããå£ãã¦ãããªã¢ãã£ã§ãã£ã¦ãã ããã
JUnitã§ãã¹ãåãæå®ãã
æ¬å½ã®åé¡ã«åãåãã¾ãããã
ç´æ¥ã®åé¡ã¯ããã¹ãã¡ã½ããåã«æ¥æ¬èªã使ãã¨å¾®å¦ãªå¶ç´ã®ããã§æãéãè¨è¿°åºæ¥ãªããã§ãããå¥ã«ããã¹ãã¡ã½ããåã«æ¥æ¬èªã使ããããã§ã¯ããã¾ããããã¹ãã¡ã½ããåã«æ¥æ¬èªã使ããã¨ã®ç®çã¯ãã¬ãã¼ããªã©ã«è¡¨ç¤ºããããã¹ãåãæ¥æ¬èªã«ããããã§ãããªãã°ããã¹ãã¡ã½ããåã®æ¥æ¬èªã®å¶ç´ãåã£æããããã§ã¯ãªãããã¹ãåãæ¥æ¬èªã§æå®ããããã¨ã¨ããã¨ãåºæ¥ã¾ãã
ã¡ã½ããåã®å¶ç´ã解é¤ãããã¨ã¯Javaã«ç¸ããã¦ããéãå³ããã§ããããã¹ãåãæå®ãããã¨ãªãåºæ¥ã¾ãããããªæãã
@Test(name = "ãã¹ãåããæååã§ã") public void test() { fail("ãã±ã¼ã"); }
â¦â¦ããããåã¤ãã¾ãããTestã¢ããã¼ã·ã§ã³ã«nameãªãã¦ã®ã¯ããã¾ãããããä½ãã°ããããã§ãããJUnitèªä½ã«æãå
¥ããã®ã¯ãåºæ¥ãªãçç±ã¯ãªããã§ããããããã¯ãã
ãªã®ã§ï¼å¼·å¼ï¼ãã¢ããã¼ã·ã§ã³ã¨Runnerãæ¸ãã¾ããããã
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Name { String value(); }
import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; public class Names extends BlockJUnit4ClassRunner { public Names(Class<?> klass) throws InitializationError { super(klass); } @Override protected String testName(FrameworkMethod method) { Name name = method.getAnnotation(Name.class); if (name == null) { return method.getName(); } return name.value(); } }
å®è¡ãã¦ã¿ã
å¾ã¯ãã®Runnerã¨ã¢ããã¼ã·ã§ã³ã使ç¨ãããã¹ããæ¸ãã ãã
import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.fail; @RunWith(Names.class) public class NamesSample { @Test public void normal() { } @Test @Name("123.456") public void å°æ°ç¹ä»ã() { } @Test @Name("ãã®ãã¹ãã¯ãã©ã®ãã¹ãï¼") public void å¥èªç¹ã¨çå符() { fail("ãã±ã¼ã"); } }
Eclipseã§å®è¡ããããããªæãã«ãªããæè¿ã¯IntelliJ IDEAæ´¾ã ãã©ã
ã¾ã¨ã
- ã©ããã¦ãå°æ°ç¹ã¨ã使ãããã£ããååãä»ãããããã«ããã£ã¦é¸æããããã
- JUnitã®ã«ã¹ã¿ã Runnerããç°¡åã«ä½ãã ããªãç°¡åã
- Eclipseã¨ãã§ããã¥ã¼ã®ãã¹ãåãããã«ã¯ãªãã¯ãã¦ããã¹ãã³ã¼ãã®ã¡ã½ããã«ç§»åããªããï¼ããã¯ãã©ã°ã¤ã³ã®å®è£ ãªãã§ãªãã¨ãï¼
- Groovyã§ãk