In java Development, it is common to use junit for unit testing. What about android? The answer is yes!
The usage is also very simple. You only need to add two lines of configuration in AndroidManifest. xml, and then write a class that inherits AndroidTestCase. The other is the same as using junit in java.
Sample AndroidManifest. xml code:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
- Package = "com. javake. hzy. filesave"
- Android: versionCode = "1"
- Android: versionName = "1.0" type = "codeph" text = "/codeph">
- <Uses-sdk android: minSdkVersion = "8"/>
- <! -- Junit test configuration key configuration first -->
- <Instrumentation android: name = "android. test. InstrumentationTestRunner"
- Android: targetPackage = "com. javake. hzy. filesave" android: label = "my app test"/>
- <Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
- <! -- The second key configuration of junit test configuration -->
- <Uses-library android: name = "android. test. runner"/>
- <Activity android: name = ". FileSave"
- Android: label = "@ string/app_name">
- <Intent-filter>
- <Action android: name = "android. intent. action. MAIN"/>
- <Category android: name = "android. intent. category. LAUNCHER"/>
- </Intent-filter>
- </Activity>
-
- </Application>
- </Manifest>
Sample Code for unit test:
- Package com. javake. hzy. filesave;
- Import java. io. FileOutputStream;
- Import android. content. Context;
- Import android. test. AndroidTestCase;
-
- /**
- * The test class is used to test file read/write operations.
- * For unit tests, you only need to inherit the AndroidTestCase class.
- * Use test as the prefix before testing.
- * Right-click the Test and choose Android JUnit Test from the run as pop-up menu.
- * @ Author hzy
- *
- */
- Public class MyTest extends AndroidTestCase {
- /**
- * Test Method 1: create a file and write a string
- */
- Public void test01 (){
- Context context = this. getContext ();
- System. out. println (context );
- Try {
- FileOutputStream out = context. openFileOutput ("hello.txt", Context. MODE_PRIVATE );
- Out. write ("hello world my name is hzy". getBytes ());
- Out. close ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- }