標籤:lis build dir stack ref todo 用例 rac protect
1.測試的相關概念
1、根據是否知道原始碼分類:
黑箱測試: a - b - c 邊值測試白盒測試: 根據原始碼寫測試方法 或者 測試案例;
2、根據測試的粒度分類:
方法測試:寫完一個方法後就測試單元測試:測試一個能夠獨立啟動並執行商務邏輯單元;整合測試:整體測試專案 聯調系統測試:對整個系統進行測試
3、根據測試的暴力程度:
1、煙霧測試 (Smoke Test):高頻次的點擊軟體2、壓力測試:使用測試載入器:LoadRunner、Jmeter
2.單元測試
Junit
01_Junit單元測試 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml
單元測試的步驟:
1、寫一個業務類,寫一個業務方法:
public class CalcService {public static int add(int x,int y){ return x+y;}
}
2、寫一個測試類別,寫一個測試方法,用來測試業務方法
public class CalcServiceTest extends AndroidTestCase{public void testAdd(){ int result = CalcService.add(4, 5); assertEquals(9, result); }}
3、在資訊清單檔中添加測試需要的包
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.itheima.junit"android:versionCode="1"android:versionName="1.0" ><!-- 添加指令集,添加到manifest節點的裡面,指令集會把應用程式部署到模擬器上運行 --><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.junit"></instrumentation><application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 添加JUnit的測試包 ,添加到application節點的裡面--> <uses-library android:name="android.test.runner"/> ....</application></manifest>
3.Logcat日誌工具的使用
日誌的等級:
error:最高等級,錯誤資訊,紅色
warn:比較高,警告資訊,橙色
debug:較高,調試資訊,藍色
info:一般,一般資訊,綠色
verbose:一般,所有資訊,黑色
4.把資料存放區到檔案
Android應用程式儲存資料的方式:
1、儲存到檔案2、SQLite資料庫3、內容提供者4、sharedproferrences儲存資料5、網路/data/data/應用程式套件名/info.txt
5.從檔案中讀取資料並顯示到介面上
(1)把檔案儲存到當前應用程式的目錄下的步驟:
1、建立一個檔案,目錄/data/data/<包名>/檔案名稱2、建立一個檔案輸出資料流,把資料寫到檔案上3、關閉輸出資料流。代碼: //儲存資料 File file = new File("/data/data/com.itheima.login/info.txt"); FileOutputStream fos = new FileOutputStream(file); String info = qq + "##"+ pwd; fos.write(info.getBytes()); fos.close(); Toast.makeText(this, "儲存資料成功", 0).show();
(2)讀取檔案中的資料,並顯示到介面上
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = (EditText) findViewById(R.id.et_qq); et_pwd = (EditText) findViewById(R.id.et_pwd); cb = (CheckBox) findViewById(R.id.cb); //讀取檔案中的資料,並顯示到介面上 Map<String,String> map = readInfo(this); if(map != null){ et_qq.setText(map.get("qq")); et_pwd.setText(map.get("pwd")); } } /** * 讀取檔案中的資料 * @param ctx * @return */public Map<String,String> readInfo(Context ctx){ String qq = ""; String pwd = ""; Map<String,String> map = new HashMap<String,String>(); try { File file = new File("/data/data/com.itheima.login/files/info.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String info = br.readLine(); String[] array = info.split("##"); qq = array[0]; pwd = array[1]; map.put("qq", qq); map.put("pwd", pwd); return map; } catch (Exception e) { e.printStackTrace(); return null; } }
6.儲存到SD卡(重點)
異常資訊:09-21 23:25:32.068: W/System.err(24718): java.io.FileNotFoundException: /storage/sdcard/info.txt: open failed: EACCES (Permission denied)
步驟:
1、 在SD卡上建立一個檔案,2、建立一個輸出資料流往sd卡上寫資料String data = "dsfdsae"; File file = new File(Environment.getExternalStorageDirectory(), "info.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(data.getBytes()); fos.close();3、在資訊清單檔中添加訪問SD卡的許可權 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
7.擷取SD的大小及可用空間
//獲得sd卡的目錄對象 File file = Environment.getExternalStorageDirectory(); //獲得sd卡總空間的大小 long total = file.getTotalSpace(); //轉換資料大小的資料單位 String totalSize = Formatter.formatFileSize(this, total); //獲得sd卡剩餘空間的大小 long usable = file.getUsableSpace(); String usableSize = Formatter.formatFileSize(this, usable); tv.setText(usableSize+"/"+totalSize);
8.檔案的許可權概念
檔案的4種操作模式:Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容,如果想把新寫入的內容追加到原檔案中。可以使用Context.MODE_APPENDContext.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案。MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取;MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入。如果希望檔案被其他應用讀和寫,可以傳入: openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);android有一套自己的安全模型,當應用程式(.apk)在安裝時系統就會分配給他一個userid,當該應用要去訪問其他資源比如檔案的時候,就需要userid匹配。預設情況下,任何應用建立的檔案,sharedpreferences,資料庫都應該是私人的(位於/data/data/<package name>/files),其他程式無法訪問。除非在建立時指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有這樣其他程式才能正確訪問。
9.SharedPreferences第二種儲存方式(重點)
主要用於(1)往SharedPreferences儲存資料 public void save(View v){ String data = et.getText().toString().trim(); if(TextUtils.isEmpty(data)){ Toast.makeText(this, "請輸入資料", 0).show(); return; }else{ //得到一個SharedPreferences SharedPreferences sp = this.getSharedPreferences("info", Context.MODE_PRIVATE); //SharedPreferences提供了一個編輯器,協助我們儲存資料 Editor editor = sp.edit(); editor.putString("data", data); //把資料儲存到SharedPreferences中 editor.commit(); }}(2)從SharedPreferences讀取資料public String readData(){ String data; try { //得到一個SharedPreferences SharedPreferences sp = this.getSharedPreferences("info", Context.MODE_PRIVATE); //根據參數名稱得到資料 data = sp.getString("data", null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); data = ""; } return data; }
10.使用序列化器產生一個xml檔案
//1,初始化一個xml檔案的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器參數 File file = new File(Environment.getExternalStorageDirectory(),"backup.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "UTF-8"); //3.開始寫xml檔案. serializer.startDocument("UTF-8", true); serializer.startTag(null, "smss"); for(SmsInfo info : smsInfos){ //開始寫sms節點 serializer.startTag(null, "sms"); //開始寫body節點 serializer.startTag(null, "body"); serializer.text(info.getBody()); //body節點結束 serializer.endTag(null, "body"); //開始寫address節點 serializer.startTag(null, "address"); serializer.text(info.getAddress()); serializer.endTag(null, "address"); //開始寫data節點 serializer.startTag(null, "date"); serializer.text(info.getDate()+""); serializer.endTag(null, "date"); // sms節點結束 serializer.endTag(null, "sms"); } //smss根節點結束 serializer.endTag(null, "smss"); //xml 結束 serializer.endDocument(); fos.close(); Toast.makeText(this, "備份簡訊成功", 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "備份簡訊失敗", 0).show(); }
11.使用pull解析xml格式的資料 (重要)
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 設定activity顯示的布局 setContentView(R.layout.activity_main); TextView tv_info = (TextView) findViewById(R.id.tv_info); StringBuilder sb = new StringBuilder(); try { //擷取我們解析出來的天氣資訊 List<Channel> channels = WeatherService.getAllWeatherInfos(getClass().getClassLoader().getResourceAsStream("weather.xml")); for(Channel channel : channels){ sb.append(channel.toString()); sb.append("\n"); } //把解析出來的天氣資訊設定到textview上 tv_info.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "解析天氣資訊失敗", 0).show(); } }}
public class WeatherService {/** * 解析伺服器返回的資料 擷取天氣資訊 * @param is 伺服器返回的包含天氣資訊的流 (xml) * @return */public static List<Channel> getAllWeatherInfos(InputStream is) throws Exception{ List<Channel> channels = null; Channel channel = null; //1.擷取xml解析器 XmlPullParser parser = Xml.newPullParser(); //2.設定xml解析器的參數 parser.setInput(is, "utf-8"); //3.開始解析xml檔案. int type = parser.getEventType();// 擷取當前的事件的類型 while (type!=XmlPullParser.END_DOCUMENT){ //需要讓pull解析器解析到檔案的末尾 switch (type) { case XmlPullParser.START_TAG: if("weather".equals(parser.getName())){//總的開始節點 channels = new ArrayList<Channel>(); //初始化集合 }else if("channel".equals(parser.getName())){//某個城市的資訊開始了. channel = new Channel(); //擷取到id的屬性值 String id = parser.getAttributeValue(0); channel.setId(Integer.parseInt(id)); //解析city節點 }else if("city".equals(parser.getName())){ String city = parser.nextText(); channel.setCity(city); //解析溫度節點 }else if("temp".equals(parser.getName())){ String temp = parser.nextText(); channel.setTemp(temp); //解析風力節點 }else if("wind".equals(parser.getName())){ String wind = parser.nextText(); channel.setWind(wind); //解析pm250節點 }else if("pm250".equals(parser.getName())){ String pm250 = parser.nextText(); channel.setPm250(Integer.parseInt(pm250)); } break; //判斷xml的結束節點 case XmlPullParser.END_TAG: if("channel".equals(parser.getName())){ //把解析的內容加入到集合中 channels.add(channel); channel = null; } break; } type = parser.next(); } is.close(); return channels;//把所有的頻道的集合返回回去 }}
安卓基礎乾貨(二):安卓測試以及解析