Android 端测试app
参考:
测试官方文档:
https://developer.android.com/training/testing?hl=zh-cn
附带app打包发布:
https://blog.csdn.net/qq_38436214/article/details/112288954
测试示例:【注意 引入包,context获取接口变更】
https://blog.csdn.net/wq6ylg08/article/details/91347989
--------------------------------------------------------------------------------------------
前期数据处理
1、读取json:https://codeantenna.com/a/VARoqkgFJo
// 序列化数据 // ========================== public class MapBean implements Serializable { public static final int TYPE_PARENT=0;//父类 public static final int TYPE_CHILD=1;//子类 private int viewType; //类型 private String caption; //标题 private List<MapBean> childList;//定义一个装载多个子类的列表 public MapBean(String caption,int viewType){//表示获得的消息类型 this.caption=caption; this.viewType=viewType; } public int getViewType(){ return viewType; } public String getCaption(){ return caption; } //折叠展开列表 public void setChildList(List<MapBean> childList){ this.childList=childList; } public List<MapBean> getChildList(){ return childList; } } // --------------------------------------- public class MainActivity extends AppCompatActivity { private List<MapBean> mapBeanList;//定义全局列表 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMaps(); } private void initMaps() { // 解析Json数据 StringBuilder newstringBuilder = new StringBuilder(); InputStream inputStream = null; try { inputStream = getResources().getAssets().open("MapBean.json"); InputStreamReader isr = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(isr); String jsonLine; while ((jsonLine = reader.readLine()) != null) { newstringBuilder.append(jsonLine); } reader.close(); isr.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } String str = newstringBuilder .toString(); Gson gson = new Gson(); Type MapBeanListType = new TypeToken<ArrayList<MapBean>>(){}.getType(); mapBeanList=gson.fromJson(str, MapBeanListType); }
2、查找元素 Uiselector :https://www.cnblogs.com/jianXu/p/5158396.html
Uiobject2: https://blog.csdn.net/mqqzt/article/details/49981207
---------------------------------------------------------------------------------------------
测试思路
1、构建测试app1,即TestRunner
2、构建启动测试app2,用与调用1中的测试类及用例
2.1 启动方式:2中的app调用1中的测试用例,如
am instrument [options] component : 【AndroidJUnitRunner类是一个JUnit测试运行器,允许运行JUnit 3或JUnit 4测试类在 Android 设备上,包括那些使用Espresso和UI Automator框架。】
===》 怎么运行手机中现有的instrumentation, 并输出详细结果,同时将profiling性能数据写到本地文件中?
- 先列出手机中已安装的instrumentation:adb shell pm list instrumentation
- adb shell am instrument XXX
====》 命令的具体使用,比如com.le.tcauto.uitest.test是包含所有测试代码的应用的包名:(来自:http://blog.csdn.net/swordgirl2011/article/details/50881390)
- 运行所有的用例: adb shell am instrument -w com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行一个类中的所有用例: adb shell am instrument -w -r -e class com.letv.leview.setproxy com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行类中的某个方法:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy#testDemo com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行多个类的所有用例:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy,com.letv.leview.resetdate com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行所有测试用例除了指定的类:adb shell am instrument -w -r -e notClass com.letv.leview.setproxy com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行所有测试除了指定的用例:adb shell am instrument -w -r -e debug false -e class com.letv.leview.setproxy#testDemo com.le.tcauto.uitest.test/android.support.test.runner.AndroidJUnitRunner
- 运行文件中的所列的用例:adb shell am instrument -w -e testFile /sdcard/tmp/testFile.txt com.android.foo/com.android.test.runner.AndroidJUnitRunner 文件制定的 格式为:com.android.foo.FooClaseName#testMethodName
- 运行指定测试切片的用例:adb shell am instrument -w -e numShards 4 -e shardIndex 1 com.android.foo/android.support.test.runner.AndroidJUnitRunner
- 运行指定注解的测试用例:adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner。如果使用多个选项,则运行的用例为两者的交集,比如:“-e size large -e annotation com.android.foo.MyAnnotation”,将只运行同时含LargeTest和MyAnnotation注解的用例。
- 运行没有指定注解的用例:adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner,指定多个注解,用“,”隔开,e.g. adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation,com.android.foo.AnotherAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner
- 以上所有参数也可以通过<meta-data>标签配置在AndroidManifest文件,比如 <meta-data android:name="listener" android:value="com.foo.Listener"/>,通过shell命令 传入的参数将覆盖AndroidManifest文件中配置的参数。
解析参考:https://blog.csdn.net/Liu_Liu_Q/article/details/79700683
https://developer.android.com/studio/command-line/adb.html?hl=zh-cn#am
2.2 运行app2需要获取
1)系统签名,gradle中配置
2)manifest中配置userid
操作参考:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:sharedUserId="android.uid.system"
package="com.test.starttest">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
。。。。
</application>
</manifest>