多数情况下我在代码里面会在一些容易出错的地方加入Log.i等打印日志的语句,这样可以直接在运行后清楚地看到一些无法在界面上看到的中间数据的结果,但这样往往十分局限,和调试一样一次只能测试一条数据。所以有时写测试代码显得很有必要,不要觉得这是QA做的而不愿去写,你在给自己找茬的时候思维往往更活跃,如果是在你思路不是很清晰请就写测试代码,那么你将能绕过许多坑,是你自己的编码思路更加清晰,这样效率自然提高了。而且有时候我们会需要静下来一阵让大脑休息休息,这段时间就可以用来跑测试代码了。我们只需要做单元测试就够了,太复杂的反而容易让人感到烦躁,另外测试的数据是可以复杂粘贴的,作为一名程序员要学会懒,懒是我们的基本素养,能复用的就复用。下面这个类中junit.textui.TestRunner.run(new TestSuite(TempTester.class))可以测试TempTester这个类所有以test开头的函数,另外每次测试都会先执行setUp()方法,然后是测试代码,最后是tearDown()方法,也就是将每个测试都区分开,setUp方法中就可以用来读测试数据文本了。
import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import java.io.File; import java.io.FileOutputStream; /** * Created by ShareLone on 2017/10/12. */ public class TempTester extends TestCase{ public TempTester(String name){ super(name); } public static void main(String[] args){ // junit.textui.TestRunner.run(suite()); junit.textui.TestRunner.run(new TestSuite(TempTester.class)); //所有以test起头的函数都会作测试用例被添加 } // public static Test suite(){ // TestSuite suite = new TestSuite(); // suite.addTest(new TempTester("testWhat")); // return suite; // } public void testWhat() { int sum = 0; int a = 1; int b = 2; sum = a + b; assertEquals("testWhat", 1, sum); } public void testWho(){ String who = "ShareLone"; assertEquals("testWho", "Solone", who); } FileOutputStream testStream; @Override protected void setUp() throws Exception { //File testFile = new File("text.txt"); //testStream = new FileOutputStream(testFile); } @Override protected void tearDown() throws Exception { //testStream.close(); } }
在正式测试前线放入错误的测试数据,确保你的测试代码是可以测出错误的,上面我这样写显得很没有必要是因为这测试的太简单了,看都能看出来,但具体到复杂的情况就显
得很有必要了。
下面测试代码的运行结果
"E:\DEVELOP SOFTWARE\android Studio\jre\bin\java" -Didea.launcher.port=7540 "-Didea.launcher.bin.path=E:\DEVELOP SOFTWARE\android Studio\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "E:\DEVELOP SOFTWARE\android Studio\lib\idea_rt.jar;E:\DEVELOP SOFTWARE\android Studio\plugins\junit\lib\junit-rt.jar;E:\AndroidHelper\SDK\platforms\android-25\data\res;F:\develop place\for android\DGSRCMGSYS2\app\build\intermediates\classes\test\GooglePlay\debug;F:\develop place\for android\DGSRCMGSYS2\app\build\intermediates\classes\GooglePlay\debug;F:\develop place\for android\DGSRCMGSYS2\app\libs\nineoldandroids-2.4.0.jar;C:\Users\DELL\.gradle\caches\modules-2\files-2.1\com.google.code.gson\gson\2.6.2\f1bc476cc167b18e66c297df599b2377131a8947\gson-2.6.2.jar;F:\develop place\for android\DGSRCMGSYS2\app\libs\volley.jar;F:\develop place\for android\DGSRCMGSYS2\app\libs\jpush-android-3.0.8.jar;C:\Users\DELL\.android\build-cache\2ab0dee61601cebf5fcf2ffa907897957fd1630e\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\2ab0dee61601cebf5fcf2ffa907897957fd1630e\output\res;F:\develop place\for android\DGSRCMGSYS2\app\libs\commons-net-3.3.jar;F:\develop place\for android\DGSRCMGSYS2\app\libs\BaiduLBS_Android.jar;C:\Users\DELL\.android\build-cache\056da9de2fa64ad050d02711c7812926e7b0e1d3\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\056da9de2fa64ad050d02711c7812926e7b0e1d3\output\res;C:\Users\DELL\.android\build-cache\236b093f726d4cda201c5367a8eac35280015261\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\236b093f726d4cda201c5367a8eac35280015261\output\res;F:\develop place\for android\DGSRCMGSYS2\app\libs\ksoap2-android-assembly-3.0.0-RC.4-jar-with-dependencies.jar;C:\Users\DELL\.android\build-cache\9c05b71d5c1af8bc8da0d1308bd92547765e869c\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\9c05b71d5c1af8bc8da0d1308bd92547765e869c\output\res;C:\Users\DELL\.android\build-cache\e7fa9f42e6fc98991ae198fc38f2e5589cc9636c\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\e7fa9f42e6fc98991ae198fc38f2e5589cc9636c\output\res;C:\Users\DELL\.android\build-cache\88c49e8bfa735e92b1456d6663cb6a2a8d0531d5\output\res;C:\Users\DELL\.android\build-cache\88c49e8bfa735e92b1456d6663cb6a2a8d0531d5\output\jars\classes.jar;C:\Users\DELL\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;F:\develop place\for android\DGSRCMGSYS2\app\libs\jcore-android-1.1.6.jar;C:\Users\DELL\.android\build-cache\f52cbb59a5b07893dda650cf3b73bd20fc675182\output\res;C:\Users\DELL\.android\build-cache\f52cbb59a5b07893dda650cf3b73bd20fc675182\output\jars\classes.jar;C:\Users\DELL\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;E:\AndroidHelper\SDK\extras\android\m2repository\com\android\support\support-annotations\25.3.1\support-annotations-25.3.1.jar;C:\Users\DELL\.android\build-cache\ab40685e30868d5d74fa3c3424409ff6a87b9f2c\output\res;C:\Users\DELL\.android\build-cache\ab40685e30868d5d74fa3c3424409ff6a87b9f2c\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\44fccb449e310e428f19bdb683ea3467bfb157f3\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\44fccb449e310e428f19bdb683ea3467bfb157f3\output\res;C:\Users\DELL\.android\build-cache\61bd77e2d50ca59aec7116270d906aabc1ad3eb6\output\res;C:\Users\DELL\.android\build-cache\61bd77e2d50ca59aec7116270d906aabc1ad3eb6\output\jars\classes.jar;C:\Users\DELL\.android\build-cache\b3c2c9249309b1a670e6329c9afadd245bba111f\output\res;C:\Users\DELL\.android\build-cache\b3c2c9249309b1a670e6329c9afadd245bba111f\output\jars\classes.jar;C:\Users\DELL\.gradle\caches\modules-2\files-2.1\com.jakewharton\butterknife-annotations\8.0.0\67e564dac5d137ba17cbb464ce66e2bd026ee532\butterknife-annotations-8.0.0.jar;F:\develop place\for android\DGSRCMGSYS2\app\build\intermediates\sourceFolderJavaResources\test\GooglePlay\debug;F:\develop place\for android\DGSRCMGSYS2\app\build\intermediates\sourceFolderJavaResources\GooglePlay\debug;F:\develop place\for android\DGSRCMGSYS2\build\generated\mockable-android-25.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.dgsrcmgsys.ui.TempTester junit.framework.ComparisonFailure: testWho Expected :Solone Actual :ShareLone <Click to see difference>...中间省略几十行
junit.framework.AssertionFailedError: testWhat
Expected :1
Actual :3
<Click to see difference>
...中间省略几十行
Process finished with exit code -1