单元测试整理

来源:互联网 发布:淘宝好评返现怎么罚款 编辑:程序博客网 时间:2024/06/03 17:49

单元测试,还是在刚接触android的时候写了个demo,工作之后,经历的公司都没有做过单元测试。最近在看包建强的《App研发录》,书中提到了这个,正好今天项目上线,闲下来可以学点东西。以后可能也不会用到,但是,多掌握点知识总是好的。
学习的时候主要参考了这篇文章http://www.jianshu.com/p/03118c11c199,这篇教程翻译自Google I/O 2015中关于测试的codelab。我主要记录一下学习过程中遇到的问题。
1.错误1
解决方法是:

defaultConfig {        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}

2.这里写图片描述
解决方法是:

dependencies {    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {        exclude group: 'javax.inject'    }}

今天就遇到这两个,以后有问题会继续更新。2016/5/6

2016/6/6
使用 robolectric,参考文章:
http://mp.weixin.qq.com/s?__biz=MzAwNzc4OTYwMA==&mid=2653509690&idx=3&sn=74b57f7078236bcb12b675cf34148a9f&scene=4#wechat_redirect
在使用robolectric的时候遇到的问题:

java.lang.RuntimeException: build\intermediates\bundles\debug\AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml

跟文章作者设置的一模一样啊,为啥会报错呢?哪有那么多为什么,有错误得解决啊。
看错误提示,需要指向自己项目的AndroidManifest.xml文件。OK,Config这个文件有个属性manifest,可以配置AndroidManifest.xml的路径。

@Config(constants = BuildConfig.class, sdk = 21,manifest = "E:/myproject/xxx/app/build/intermediates/manifests/full/debug/AndroidManifest.xml")@RunWith(RobolectricGradleTestRunner.class) 

配置好以后,运行测试方法,继续上边的错误。哇,居然不起作用。
看看RobolectricGradleTestRunner是怎么设置路径的。

AndroidManifest.xml、res、assets默认是用的这个文件夹下的private static final String BUILD_OUTPUT = "build/intermediates";

这里写图片描述
看下manifests的路径是怎么设置的,

/***flavor 和 type 都是从BuildConfig配置文件里取得值*public static final String BUILD_TYPE = "debug";*public static final String FLAVOR = "";**/if (FileFsFile.from(BUILD_OUTPUT, "manifests").exists()) {      manifest = FileFsFile.from(BUILD_OUTPUT, "manifests", "full", flavor, type, "AndroidManifest.xml");    } else {      manifest = FileFsFile.from(BUILD_OUTPUT, "bundles", flavor, type, "AndroidManifest.xml");    }

好像并没有用到Config.manifest这个配置,不过我们知道了,AndroidManifest.xml这个路径是神马,默认是build/intermediates/manifests/full/debug/AndroidManifest.xml,如果这个路径不存在,会查找build/intermediates/bundles/debug/instant-run/AndroidManifest.xml这个路径。
看项目目录,这两个都是存在的。那应该就是需要指定全路径吧。
我们可以继承RobolectricTestRunner,来自定义TestRunner,

public class CustomTestRunner extends RobolectricTestRunner {    private static final String BUILD_OUTPUT = "E:/myproject/xxx/app/build/intermediates/";    public CustomTestRunner(Class<?> testClass) throws InitializationError {        super(testClass);    }    @Override    protected AndroidManifest getAppManifest(Config config) {        String manifestPath = BUILD_OUTPUT + "manifests/full/debug/AndroidManifest.xml";        String resDir = BUILD_OUTPUT + "res/merged/debug";        String assetsDir = BUILD_OUTPUT + "assets/debug";        AndroidManifest manifest = createAppManifest(Fs.fileFromPath(manifestPath),                Fs.fileFromPath(resDir),                Fs.fileFromPath(assetsDir),"com.uthing");        return manifest;    }}

AndroidManifest.xml上层文件我直接写死了,可以参考RobolectricGradleTestRunner根据 flavor和build_type来动态改变。
最后设置成自己的testrunner类,

@Config(sdk = 21 )@RunWith(CustomTestRunner.class)

再次运行测试方法,尼玛,终于绿了。

2016/6/7

测试方法代码SplashActivity2 splashActivity2 = Robolectric.setupActivity(SplashActivity2.class);splashActivity2.findViewById(R.id.bt).performClick();Intent intent = new Intent(splashActivity2, LoginActivity.class);ShadowActivity shadowActivity = Shadows.shadowOf(splashActivity2);Intent actualIntent = shadowActivity.getNextStartedActivity();Assert.assertEquals(intent,actualIntent);

错误信息:Error:(30, 30) Gradle: error: cannot access AndroidHttpClient
class file for android.net.http.AndroidHttpClient not found
解决办法:
android {
useLibrary ‘org.apache.http.legacy’
}

0 0