【Java】xUnitも何もない不毛な土地で、それなりのテストをしたい時のちっちゃいテストツール
概要
xUnitも何もない不毛な土地に生きています。数少ない村人達は、プログラムをデプロイしてweb画面をクリッククリックすることを単体テストと言い張るのです・・・・・・そんな環境でもテストがしたい! という方のために。(=自分用)
ソース
[TinyTest.java]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kontrapunkt.kunst1080.TinyTest; | |
import java.io.PrintStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.List; | |
public abstract class TinyTest { | |
// Framework | |
public interface Tester{ | |
boolean testMain(); | |
} | |
public static void testStart(Tester t){ | |
p("[ClassName = " + t.getClass().getName() + "]"); | |
p("---------- Test Start ----------"); | |
if (t.testMain()){ | |
p("---------- Test OK End ----------"); | |
} else { | |
p("---------- Test Abnormal End ----------"); | |
} | |
p(""); | |
} | |
// Utilitiy functions | |
public static void p(Object o){ | |
if(o instanceof Calendar){ | |
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmSS"); | |
Calendar cal = (Calendar)o; | |
System.out.println(df.format(cal.getTime())); | |
} else if (o instanceof List){ | |
List<Object> list = autoCast(o); | |
for (Object obj : list){ | |
p(obj); | |
} | |
} else { | |
System.out.println(o.toString()); | |
} | |
} | |
public static void line(){ | |
p("------------------------------"); | |
} | |
public static String getThisClassName(){ | |
StackTraceElement[] st = new Throwable().getStackTrace(); | |
String s = st[st.length - 1].getClassName(); | |
return s; | |
} | |
@SuppressWarnings("unchecked") | |
public static <T> T autoCast(Object o){ | |
return (T)o; | |
} | |
/** | |
* Switch stdout for file | |
* @param filepath | |
*/ | |
public static void setOutFile(String filepath){ | |
try{ | |
PrintStream out = new PrintStream(filepath); | |
System.setOut(out); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
で、こちらが動作サンプル[TestTinyTest.java]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kontrapunkt.kunst1080.TinyTest; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class TestTinyTest extends TinyTest { | |
public static void main(String[] args) { | |
testStart(new OKTest()); | |
//testStart(new NoTest()); | |
testStart(new NGTest()); | |
setOutFile("C:/temp/TestTinyTest.log"); | |
testStart(new FileTest()); | |
} | |
static class OKTest implements Tester { | |
public boolean testMain() { | |
p(new Date()); | |
p(Calendar.getInstance()); | |
p("fugafuga"); | |
return true; | |
} | |
} | |
static class NoTest implements Tester { | |
public boolean testMain() { | |
return true; | |
} | |
} | |
static class NGTest implements Tester { | |
public boolean testMain() { | |
p("1 == 2"); | |
return 1 == 2; | |
} | |
} | |
static class FileTest implements Tester { | |
public boolean testMain() { | |
p("1 == 2"); | |
return 1 == 2; | |
} | |
} | |
} |
稼働実績はあまりないので、粗はいっぱいあるかも。