赞
踩
####调试(DEBUG):调试是一种修正程序逻辑错误有效手段,是每一个程序不可或缺的步骤,可以说,不懂调试的程序员不是合格的程序员。
public class LogUtils { private LogUtils () { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } private static final int VERBOSE = 1; private static final int DEBUG = 2; private static final int INFO = 3; private static final int WARN = 4; private static final int ERROR = 5; private static final int NOTHING = 6; private static final int LEVEL = DEBUG; private static final String TAG = "DEBUG"; // 下面四个是默认tag的函数 public static void v ( String msg ) { if ( LEVEL <= VERBOSE ) Log.v(TAG, msg); } public static void d ( String msg ) { if ( LEVEL <= DEBUG ) Log.d(TAG, msg); } public static void i ( String msg ) { if ( LEVEL <= INFO ) Log.i(TAG, msg); } public static void w ( String msg ) { if ( LEVEL <= WARN ) Log.w(TAG, msg); } public static void e ( String msg ) { if ( LEVEL <= ERROR ) Log.e(TAG, msg); } // 下面是传入自定义tag的函数 public static void v ( String tag, String msg ) { if ( LEVEL <= VERBOSE ) Log.v(tag, msg); } public static void d ( String tag, String msg ) { if ( LEVEL <= DEBUG ) Log.d(tag, msg); } public static void i ( String tag, String msg ) { if ( LEVEL <= INFO ) Log.i(tag, msg); } public static void w ( String tag, String msg ) { if ( LEVEL <= WARN ) Log.w(tag, msg); } public static void e ( String tag, String msg ) { if ( LEVEL <= ERROR ) Log.e(tag, msg); } }
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LogUtils.d(getTitle()+""); //None tag parameter input . Just print title
LogUtils.d(getLocalClassName(), "onCreate: "+getTitle()); //print title
}
}
###(2)调节Logcat字体风格:
先打开Setting,搜索logcat,可以设置颜色、背景、加粗等。
我给出个人建议:
打印APP中你想要知道的数据;方法执行的周期运作情况等。
下面用红色框出的为必须掌握的快捷键。
例子
可以输入形参让函数进行执行返回结果的操作。
也可以查看当前参数的值。
一句话,一个断点什么问题都出来了.断点一般被我们用于寻找较为隐蔽的细节问题,比如数据较多的情况出现数据异常等。
每当我们创建工程的时候可以看到有两个可以供我们测试的文件:Instrumented text & Unit test。
Unit Test 和 Instrument Test 的区别
在 Android Developer 给出的 Instrument Test 解释如下:
Instrumented unit tests are unit tests that run on physical devices and emulators, instead of the Java Virtual Machine (JVM) on your local machine. You should create instrumented unit tests if your tests need access to instrumentation information (such as the target app’s Context) or if they require the real implementation of an Android framework component (such as a Parcelable or SharedPreferences object). Using instrumented unit tests also helps to reduce the effort required to write and maintain mock code. You are still free to use a mocking framework, if you choose, to simulate any dependency relationships. Instrumented unit tests can take advantage of the Android framework APIs and supporting APIs, such as the Android Testing Support Library。
简单的翻译: Instrumented Unit test 是允许在真机或者模拟器上的,而不是运行在本地环境下的虚拟机中。如果在测试时需要使用 instrumentation information(例如 app Context),或者你需要获取 Android 框架的组件(例如 SharedPrederences),这时候你就可以创建一个 instrumented unit test。使用 Instrumented unit test 可以帮助减少编写模拟环境的代码。当然你也可以使用一些 mock 框架。使用 Instrumented unit test 可以很好的利用 Android framework api 和 supporting API。
在 Android Developer 给出的 Local Unit Tests(Unit Test)解释如下:
If your unit test has no dependencies or only has simple dependencies on Android, you should run your test on a local development machine. This testing approach is efficient because it helps you avoid the overhead of loading the target app and unit test code onto a physical device or emulator every time your test is run. Consequently, the execution time for running your unit test is greatly reduced. With this approach, you normally use a mocking framework, like Mockito, to fulfill any dependency relationships.
简单的翻译:如果你的单元测试不依赖Android环境,那么你需要使用一个本地的单元测试,运行在本地的JVM上,这样的话,就有利于你的测试速度,因为加载物理设备是很耗费时间的。你可以使用类似于 Mockito 这种 Java 测试框架。
简单的总结一下是,这些情况适合用 Instrumented unit test :
1.测试时需要使用 Android api支持
2.测试时需要使用 Android 中的组件
3.测试时需要访问 Android 中的特定环境元素(例如 Context)
其他情况优先考虑使用 Unit Test ,因为它的速度要快很多,效率也要高很多。其次,Instrumented unit tes 是基于 Unit Test 的。
###单元测试检验数据库操作
流程
1.SQLiteOpenUtils
SQLiteOpenUtils
public class SQLiteOpenUtils extends SQLiteOpenHelper { private static final String NAME = "MadJieJie.db"; private static final int VERSION = 1; public SQLiteOpenUtils ( Context context) { super(context, NAME, null, VERSION); } /** * Create MadJieJie db. * @param db */ @Override public void onCreate ( SQLiteDatabase db ) { db.execSQL("CREATE TABLE user(" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "account VARCHAR(50) NOT NULL," + //account "password VARCHAR(50) NOT NULL" + //password ")"); } @Override public void onUpgrade ( SQLiteDatabase db, int oldVersion, int newVersion ) { } }
####2.编写数据库工具类DBUtils-插入数据
DBUtils
public class DBUtils { private DBUtils () { throw new UnsupportedOperationException("can't init"); } /** * Insert into user's data. * * @param context * @param account * @param password * @return */ public static boolean insertUserData ( Context context, final String account, final String password ) { SQLiteOpenUtils sqLiteOpenUtils = new SQLiteOpenUtils(context); SQLiteDatabase db = sqLiteOpenUtils.getWritableDatabase(); try { db.execSQL("INSERT TABLE user(account,password) VALUES(?,?)", new String[]{ account, password });//error statement db.execSQL("INSERT INTO TABLE user(account,password) VALUES(?,?)", new String[]{ account, password });//right statement return true; } catch( SQLException e ) { e.printStackTrace(); return false; } finally { if ( db != null ) db.close(); if ( sqLiteOpenUtils != null ) sqLiteOpenUtils.close(); } } }
双击选中该类,点击Go To,在点击Test
选中创建一个新的测试类
通过编译的模样
因为错误被捕获了-Logcat可以查看到
把try-catch语句去掉
对较为复杂的函数,如数据库,网络操作等。。。
MVP框架中的P操作测试-用Mockito框架。
参考文章:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。