张亚洲 发表于 2015-1-7 08:58:25

【我分享】 Android自动化测试(Jenkins+Robotium+Ant+Junit)[PART one]

前期的环境搭建和代码网上有很多资料,在这里我就不一一细说了,详细说一下我在整个过程中遇到的问题。
自动化测试一般的过程:
写testcase;
执行testcase;
记录log;
xml转化为html;
1、 java.lang.ExceptionInInitializerError
我在整个的测试过程中碰到过还几次这个error,开始查了很多资料,没有解决,最后发现是路径的问题,我在测试过程中用了像类似“C:/user/..”这样的路径,但是在Android模拟器上是找不到的,所以就会报ExceptionInInitializerError。 切记:是在Android模拟器上进行测试的,本机上的路径是不可用的!
2、 log
在测试的过程中记录日志,我本来打算直接用log4j-1.2.17.jar,后来发现还是有一部分问题的,需要再加一个android-logging-log4j.jar,像平常用的log4j.peoperties也不能用在Android上,Android真正的log配置应该用下面这一段代码:
LogConfigurator logConfigurator = new LogConfigurator();
//sdcard
logConfigurator.setFileName(Environment.getExternalStorageDirectory()
+ File.separator + "Logs" + File.separator +DocEnvConstants.LOCALE+ File.separator +"xxx.log");
logConfigurator.setRootLevel(Level.DEBUG);
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.setFilePattern("%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n");
//%d %-5p [%c{2}]-[%L] %m%n
logConfigurator.setMaxFileSize(1024 * 1024 * 5);
logConfigurator.setImmediateFlush(true);
logConfigurator.configure();
log = Logger.getLogger(AndroidDocTest.class.getName());
3、 ClassNotFoundException
Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
xml文件里面的包名是否正确。这种问题一般是马虎所导致的,仔细查看一下你的AndroidManifest.xml中的包名是否正确。

4、 Android模拟器上文件的读写问题:
我也是在本次测试过程中才发现问题,最开始用的平时用的文件读写的方式,后来发现不对,Android文件的读写和平时用的文件读写是不同的,附代码:
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
BufferedReader br= new BufferedReader(new InputStreamReader(in, "UTF-8"));
String tempString = null;
boolean found = false; //if find the testcase log
while ((tempString = br.readLine()) != null) {
}
}

页: [1]
查看完整版本: 【我分享】 Android自动化测试(Jenkins+Robotium+Ant+Junit)[PART one]