测试
按岗位划分
按测试粒度分
- 方法测试:function test
- 单元测试:unit test
- 集成测试:integration test
- 系统测试:system test
按测试的暴力程度分
- 冒烟测试:smoke test
- 压力测试:pressure test
单元测试
//指定该测试框架要测试哪一个项目
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.junit"></instrumentation>
<uses-library android:name="android.test.runner"/>
SQLite运用
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
}
}
public class TestCase extends AndroidTestCase {
private MyOpenHelper oh;
private SQLiteDatabase db;
public void test(){
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
}
@Override
protected void setUp() throws Exception {
super.setUp();
oh = new MyOpenHelper(getContext(), "people.db", null, 1);
db = oh.getWritableDatabase();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
db.close();
}
public void insert(){
db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"a", 14000, "13888"});
}
public void delete(){
db.execSQL("delete from person where name = ?", new Object[]{"a"});
}
public void update(){
db.execSQL("update person set phone = ? where name = ?", new Object[]{186666, "a"});
}
public void select(){
Cursor cursor = db.rawQuery("select name, salary from person", null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String salary = cursor.getString(1);
System.out.println(name + ";" + salary);
}
}
public void insertApi(){
ContentValues values = new ContentValues();
values.put("name", "a");
values.put("phone", "15999");
values.put("salary", 16000);
db.insert("person", null, values);
}
public void deleteApi(){
int i = db.delete("person", "name = ? and _id = ?", new String[]{"a", "3"});
System.out.println(i);
}
public void updateApi(){
ContentValues values = new ContentValues();
values.put("salary", 26000);
int i = db.update("person", values, "name = ?", new String[]{"a"});
System.out.println(i);
}
public void selectApi(){
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name + ";" + phone + ";" + salary);
}
}
public void transaction(){
try{
db.beginTransaction();
ContentValues values = new ContentValues();
values.put("salary", 12000);
db.update("person", values, "name = ?", new String[]{"a"});
values.clear();
values.put("salary", 16000);
db.update("person", values, "name = ?", new String[]{"b"});
int i = 3/0;
db.setTransactionSuccessful();
}
finally{
db.endTransaction();
}
}
}
使用ListView显示数据
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return personList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person p = personList.get(position);
View v = null;
if(convertView == null){
v = View.inflate(MainActivity.this, R.layout.item_listview, null);
}
else{
v = convertView;
}
TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
tv_name.setText(p.getName());
TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);
tv_phone.setText(p.getPhone());
TextView tv_salary = (TextView) v.findViewById(R.id.tv_salary);
tv_salary.setText(p.getSalary());
return v;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] objects = new String[]{
"a",
"b",
"c"
};
ListView lv = (ListView) findViewById(R.id.lv);
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("photo", R.drawable.photo1);
map1.put("name", "c");
data.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("photo", R.drawable.photo2);
map2.put("name", "a");
data.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("photo", R.drawable.photo3);
map3.put("name", "b");
data.add(map3);
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview,
new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));
}
弹出对话框
public void click1(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(android.R.drawable.alert_dark_frame);
builder.setTitle("title");
builder.setMessage("内容");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", 0).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", 0).show();
}
});
AlertDialog ad = builder.create();
ad.show();
}
public void click2(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("title");
final String[] items = new String[]{
"男",
"女"
};
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了:" + items[which], 0).show();
dialog.dismiss();
}
});
builder.show();
}
public void click3(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("title");
final String[] items = new String[]{
"名字1",
"名字2",
"名字3",
"名字4"
};
final boolean[] checkedItems = new boolean[]{
true,
true,
false,
false
};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = "";
for(int i = 0; i < 4; i++){
text += checkedItems[i]? items[i] + "," : "";
}
Toast.makeText(MainActivity.this, text, 0).show();
dialog.dismiss();
}
});
builder.show();
}