如何为在Android Studio中的一个机器人库运行仪器测试? [英] How to run instrumentation tests for an android library in android studio?

查看:166
本文介绍了如何为在Android Studio中的一个机器人库运行仪器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档(的http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing-Android-Libraries)说,这只是自动生成的应用程序,并与库作为依赖运行,但它不会告诉你什么包装应用的活动将被调用,这意味着你不能用仪器类测试(例如: http://developer.android.com/reference/android/test/ActivityTestCase.html )。
由于他们需要知道的有关活动,来初始化超级构造函数。

The documentation (http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing-Android-Libraries) says that it will just auto-generate the app and run with the library as a dependency, but it doesn't tell you what the wrapping applications activities will be called, which means you can't test it with the instrumentation classes (eg. http://developer.android.com/reference/android/test/ActivityTestCase.html). Since they need to know the activity in question, to initialize the super constructor.

有谁知道正确的方式做这种测试在Android Studio中的gradle用?

Does anyone know the proper way to do this type of testing in android studio with gradle?

推荐答案

好吧,我有一个可能的解决方案。
它是否是正确的方法,我会看到别人怎么说。

Alright, I've got one possible solution. Whether or not it is the "right" approach I'll see what people say.

在理论上没有什么可以阻止我在

In theory nothing stops me creating a "TestActivity.java" class somewhere under

/ src目录/ androidTest / JAVA / COM ....香蕉/ TestActivity.java

/src/androidTest/java/com....bananas/TestActivity.java

这是真实的。您可以将潜在的ActivityTest.java androidTest下坐在中引用这一点。

And this is true. You can reference this within a potential ActivityTest.java sitting under androidTest.

public class ActivityTest extends ActivityInstrumentationTestCase2<TestActivity> {

public ActivityTest() {
    super(TestActivity.class);
}
...

但是有一个毛刺/ catch语句。如果在测试活动需要引用的任何字符串/常量(如RES /布局/ activity_test.xml)他们的不能的是在 / src目录/ androidTest / RES /布局/ 。此文件夹生成过程中莫名其妙地被忽略。即使它被列为对构建的gradle的资源源文件夹(我的gradle通过检查倾销(android.sourceSets.androidTest.dump的println()),内容不要建立和放大器时,发现;运行测试......,你会得到:

There is however one glitch/catch. IF the TestActivity needs to reference any strings/constants (eg. res/layout/activity_test.xml) they cannot be under /src/androidTest/res/layout/. This folder is inexplicably ignored during build. Even though it is listed as the res source folder for the gradle build (I checked via gradle dumping (println android.sourceSets.androidTest.dump()), the contents are not found when going to build & run the tests...and you get:

没有资源发现给定的名称匹配...

No resource found that matches the given name ...

所以,这意味着你可以用自定义的活动,请您测试库,而无需旋转起来一个完整独立的测试应用程序,但你得到一些额外的杂波在库的res文件夹。的(尽管这些可以从最终.aar手动除外)。

So, this means that you can test libs with custom activities as you please, without having to spin up a full separate test app, but you get some extra clutter in your res folder for the library. (though these could be manually excluded from the final .aar).

这将是很好,如果有人可以解释这个奇怪的疑难杂症,或证明我错了,但在其他方面工作的。 :)

It would be nice if someone could explain this odd gotcha, or prove me wrong, but otherwise this works. :)

修改/更新

有一点要补充的,就是要确保在AndroidManifest.xml中下 / src目录/ androidTest 包含类似:

One thing to add in, is to make sure that the AndroidManifest.xml under /src/androidTest contains something like:

<application>
    <activity
        android:name=".TestActivity"
        android:label="@string/title_activity_test" >
    </activity>
</application>

getActivity()的ActivityInstrumentationTestCase2内将如果忽略引发错误:了java.lang.RuntimeException:无法解析的活动:意向{行动= android.intent.action 。主要...

这篇关于如何为在Android Studio中的一个机器人库运行仪器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆