Unit Tests can ensure the quality of our applications. Generally, after the business layer is developed, we test the business layer to ensure that no bugs occur at the business layer, after the business layer is passed, the control layer can call the business layer to complete the required functions.
In the past, junit was used for testing during Java Development and the System. out. println () method was used to print data on the console. Next I will explain how to perform unit tests and log output on Android applications.
1. unit test and log output
1.1 unit test steps:
Step 1: In AndroidManifest. insert the following code into the xml file and insert <use-library android: name = "android. test. runner "/>, and then insert <instrument android: name =" android. test. instrumentationTestRunner "android: targetPackage =" Name of the Activity Package inserted here "android: label =" Name of the label inserted here, which can be obtained by yourself"
Step 2: Write unit test code. The test class must inherit the AndroidTestCase class.
Step 3: perform the test
1.2 test example
First, add the required code in step 1 to AndroidManifest. xml. The final code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. csdn. android. junit"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Uses-library android: name = "android. test. runner"/>
<Activity
Android: label = "@ string/app_name"
Android: name = ". Junit_testActivity">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Instrumentation
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "cn. csdn. android. junit"
Android: label = "MyJunitTest"
>
</Instrumentation>
</Manifest>
Create a test case:
The use case method is public void func1 () {}. The common mistake for beginners is to add parameters to the test method. You must note that parameters are not required for the test method, if an internal exception occurs in the test method, it is recommended that you directly throw the exception instead of capturing it. After the exception is thrown, it is obtained by the test framework and displayed on the console. The content of the test class is as follows:
Package cn. csdn. android. junit;
Import junit. framework. Assert;
Import android. test. AndroidTestCase;
Public class MyTest extends AndroidTestCase {
Public void testFunc1 () throws Throwable {
User u = new User ();
U. func1 ();
}
Public void testFunc2 () throws Throwable {
User u = new User ();
Assert. assertEquals (20, u. func2 ());
}
}
The role of the Assert class is to determine the relationship between the obtained result and the expected value.
Step 3: run the test case: first go to the Outline view. If there is no such test case in Eclipse, find it in the toolbar window and add it. Right-click the Test case, find [Run As], and click [Android JUnit Test]. The result bar is displayed in green, indicating that it is correct:
1.3 log output
The Console can only output the installation information, if you add a line of System in the program. out. println ("aaa"). For Java development, it will be printed on the console, but it cannot be printed on the console in Android, but it will be printed in LogCat, outputs Information in Android programs. Generally, Android is used. util. static Methods of the Log class are implemented. The log Content output by the Log class ranges from few to many, namely, ERROR, WRAN, INFO, DEBUG, and VERBOSE, which correspond to the first letter of five different types, they are Log. e (), Log. w (), Log. I (), Log. d (), Log. v () Five static methods, with different output colors, as shown in: click each button to filter logs of its own type and the right type. For example, click I, IEW logs are displayed, whereas VD logs are not displayed,
1) write the log output test class, and modify the Code as follows:
Package cn. csdn. android. junit;
Import junit. framework. Assert;
Import android. test. AndroidTestCase;
Public class MyTest extends AndroidTestCase {
Private final static String TAG = "myTag ";
Public boolean onTouchEvent (MotionEvent event ){
Log. I (TAG, "onkeyTouch ");
ShowInfo ("" + event. getX () + "" + event. getY ());
Return super. onTouchEvent (event );
}
Note: Log. I (String tag, String identifier), tag is a tag defined in this information. It is often used as the class name of its class during development, so that we can track output information, it is also convenient for us to know the class from when we see the information. msg is the content that this parameter wants to output.
2) test execution: The execution method is almost the same as the preceding test method. Open the Log Viewer "LogCat" to check whether this information is output, for example:
When we open it, we will find that there are a lot of output content. This is what we need to add a filter to select the tag information we specified, marked as we are calling Log. the TAG specified in I (TAG, "onkeyTouch"). In this example, it is "MyTest". The method for adding a filter is as follows:
Click "+" to bring up the Add LogFilter interface. The information filled in is as follows:
Click "OK" and add a tab named "MyTest" in the LogCat Manager. The information we want in the Code is displayed in this tab:
Author 3G-Xiao Yang