Android测试用例
测试用例独立app之外,也处于app之中;对于不同数据具体良好测试作用,隔离app运行程序之外,对于数据检测具有很大用处。
添加依赖
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
测试类
@RunWith(AndroidJUnit4.class)
运行快捷键ctrl+shift+f10
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.qishui.banshan0807333", appContext.getPackageName());
}
@Test
public void useGetCarList() {
//断言为真,如果false ,AssertionError 抛出 message
//assertTrue("hello", false);
//断言为假,如果true ,AssertionError 抛出 message
//assertFalse("everyone",true);
//断言相等,期待着与实际值
//assertEquals("12", new String("123"));
//断言不相等,期待着与实际值
//assertNotEquals("12", new String("123"));
//判断数组
//boolean[] objects1 = new boolean[10];
//boolean[] objects2 = new boolean[0];
//assertArrayEquals("111", objects1, objects2);
//断言不为空
//assertNotNull("zzzz", null);
//断言为空
//assertNull("1111111111","");
//断言内存地址相同
//assertSame("wwwwww", "12", new String("12"));
//assertThat 详细用法;
//https://www.cnblogs.com/shangren/p/8039215.html
}
}
BaoShanTest.class
package com.qishui.banshan0807;
import android.support.test.runner.AndroidJUnit4;
import com.google.gson.Gson;
import com.qishui.banshan0807.app.BaoshanKeys;
import com.qishui.banshan0807.bean.parse.GetLoginBean;
import com.qishui.banshan0807.bean.post.PostLoginBean;
import com.qishui.basiclibrary.net.ok.OkHttpManager;
import com.qishui.basiclibrary.net.ok.StringCallBackResult;
import com.qishui.basiclibrary.util.EncDesUtils;
import com.qishui.basiclibrary.util.GsonUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import okhttp3.Call;
import static org.junit.Assert.assertEquals;
/**
* @author Zhou
* Created on 2018/9/19 17:21.
* Email:qishuichixi@163.com
* Desc:
*/
@RunWith(AndroidJUnit4.class)
public class BaoShanTest {
@Test
public void testLogin() {
String phone = "11111111111";
String password = "111111a";
String params = EncDesUtils.enc(new Gson().toJson(new PostLoginBean(phone, password)));
OkHttpManager.postJson(BaoshanKeys.KEY_LOGIN, params).execute(new StringCallBackResult() {
@Override
public void onError(Call call, Exception e, int id) {
super.onError(call, e, id);
}
@Override
public void onResponse(String response, int id) {
super.onResponse(response, id);
GetLoginBean cb = GsonUtils.parse(response, GetLoginBean.class);
assertEquals("1", cb.getCode());
}
});
}
}
Java main测试用例
快捷键运行ctrl+shift+f10
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class TestDp {
public static void main(String[] args) {
String baseUrl="F://dpres//value";
int baseDp=360;
String[] defaultDPArr = new String[]{"240","267","320","360","384", "392", "400", "410", "411", "432","440","480", "533", "560","592", "600", "640", "662", "720", "768", "800", "811", "820", "960", "961", "1024", "1280", "1365"};
for(int i=0;i<defaultDPArr.length;i++){
Integer value=Integer.valueOf(defaultDPArr[i]);
getFile(baseDp,value,baseUrl);
}
}
private static void getFile(float baseDp,int Dp, String pathFile ) {
float size=Dp/baseDp;
StringBuilder sb=new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<resources>");
sb.append("\n");
for(float i=0;i<1;i=i+0.1f){
float temp=i*size;
String value=String.valueOf(temp);
if(value.length()>=3){
value=value.substring(0,3);
}
String value0=String.valueOf(i);
if(i==0){
value0="0";
}
if(value0.length()>=3){
value0=value0.substring(0,3);
}
sb.append("<dimen name=\"").append("dp").append(value0).append("\">").append(value).append("dp").append("</dimen>").append("\n");
}
for(int i=1;i<=baseDp;i=i+1){
float temp=i*size;
String value=String.valueOf(temp);
sb.append("<dimen name=\"").append("dp").append(i).append("\">").append(value).append("dp").append("</dimen>").append("\n");
}
for(int i=1;i<=50;i=i+1){
float temp=i*size;
String value=String.valueOf(temp);
sb.append("<dimen name=\"").append("sp").append(i).append("\">").append(value).append("sp").append("</dimen>").append("\n");
}
sb.append("</resources>\n");
//System.out.println(sb.toString());
String dir=pathFile+"//values-sw"+Dp+"dp";
try {
if(!new File(dir).exists()){
new File(dir).mkdirs();
}
String fileNme=dir+"//dimens.xml";
System.out.println(fileNme);
if(new File(fileNme).exists()){
new File(fileNme).delete();
}
new File(fileNme).createNewFile();
FileOutputStream outSTr = new FileOutputStream(new File(fileNme));
BufferedOutputStream Buff = new BufferedOutputStream(outSTr);
Buff.write(sb.toString().getBytes());
Buff.flush();
Buff.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}