转自:http://my.oschina.net/songxueyan/blog/147011
不要界面,使用单元测试完成业务逻辑功能
一。配置文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.diandong.tools"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.diandong.tools"
android:label="Test for my app"/>
</manifest>
|
说明:
9行:增加访问网络权限
15行:允许单元测试
17~20行:配置单元测试 targetPackage需要和 <manifest>标签中的 package= 一致
二。Android get和post方法:
|
三。测试类
package com.diandong.tools;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.test.AndroidTestCase;
public class MyTest extends AndroidTestCase{
public void testGet() throws Throwable{
SendGetOrPost sendGetOrPost = new SendGetOrPost();
String url = "http://10.0.2.2/xmlhttp/testmysql.php?email=931918906@qq.com";
//虚拟机把自身当作localhost或者127.0.0.1 访问电脑的localhost 使用10.0.2.2
String result = sendGetOrPost.sendGet(url);
System.out.println(result);
}
public void testPost() throws Throwable{
SendGetOrPost sendGetOrPost = new SendGetOrPost();
String url = "http://10.0.2.2/xmlhttp/insertuser.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", "smile645@sinaa.cn"));
params.add(new BasicNameValuePair("pass", "e10adc3949ba59abbe56e057f20f883e"));//123456的md5
String result =sendGetOrPost.sendPost(url, params);
System.out.println(result);
}
}
四。window-->show view -->outline中运行测试 run as android junit test