はじめに
これは いろふ Advent Calendar 2013 : ATND の7日目です
6日目
@kazuhito_m さんの "性善説"いろふさん登場! #irof_history でした
7日目:irofさんをJUnitで動かしてみた
id:irof さんは走るとすぐに疲れます
走ったら疲れた。
— irof@らんばだ (@irof) February 14, 2013
走るだけで疲れるなんて
— irof@らんばだ (@irof) February 14, 2013
走ってみた。疲れた。もうだめだ。
— irof@らんばだ (@irof) March 1, 2013
寒いから走ったら疲れて散々です。
— irof@らんばだ (@irof) March 10, 2013
そしてirofさんといえばJUnitです。
- JUnitでテスト名の制約を取っ払う - 日々常々
- 関ジャバでJUnitな話をしたので #kanjava #junitbook - 日々常々
- NUnitのTestCaseが羨ましいのでJUnitでできるようにしてみた - 日々常々
そこでirofさんをJUnitにしてみました
Irof Runner
irofさんと一緒に走りましょう!
@RunWith(Irof) class SampleIrofTest { @Test void "test 01"(){ } @Test void "test 02"(){ } // .... @Test void "test 162"(){ } @Test void "test 163"(){ // test failed! }
しかしirofさんは走ると疲れてしまいます
162*1テストケースを走ると疲れてしまうので、163個目で疲れてエラーになってしまいます
ソースコード
https://github.com/irof/irof_history/tree/master/irof-runner
maven central repositoryに公開する気はないので、使いたい場合には下記をコピペしてくればいいと思います( ´∀`)
Irof.groovy
package irof.runner import org.junit.runner.notification.RunNotifier import org.junit.runners.BlockJUnit4ClassRunner import org.junit.runners.model.FrameworkMethod import org.junit.runners.model.InitializationError class Irof extends BlockJUnit4ClassRunner { static final int MAX_IROF_COUNT = 162 static int testCount = 0 Irof(Class<?> klass) throws InitializationError { super(klass) } @Override protected void runChild(FrameworkMethod method, RunNotifier notifier) { testCount++ if(testCount > MAX_IROF_COUNT){ throw new IrofTiredException() } originalRunChild(method, notifier) } protected void originalRunChild(FrameworkMethod method, RunNotifier notifier){ super.runChild(method, notifier) } }
IrofTiredException.groovy
package irof.runner class IrofTiredException extends RuntimeException { IrofTiredException(){ super("つかれた") } }
分からなければJUnit実践入門を読めばいいと思います(ステマ)
JUnit実践入門 ~体系的に学ぶユニットテストの技法 (WEB+DB PRESS plus)
- 作者: 渡辺修司
- 出版社/メーカー: 技術評論社
- 発売日: 2012/11/21
- メディア: 単行本(ソフトカバー)
- 購入: 14人 クリック: 273回
- この商品を含むブログ (63件) を見る
まとめ
さぁみんなも自分のテストコードにirofさんをインストールしましょう!
8日目
@fukai_yas さん
いろふAdvent Calendar 2013 : 人生、気合いと具合 - blog
*1:いろふ