GVKun编程网logo

android-maven-plugin,instrumentation testing和testSize

12

本文将分享android-maven-plugin,instrumentationtesting和testSize的详细内容,此外,我们还将为大家带来关于AndroidActivityInstrume

本文将分享android-maven-plugin,instrumentation testing和testSize的详细内容,此外,我们还将为大家带来关于Android ActivityInstrumentationT…、Android AS 升级 3.1 编译报错:The SourceSet ''instrumentTest'' is not recognized by the Android Grad...、Android Espresso测试’无法解析符号’InstrumentationRegistry”、Android InstrumentationTestCase getFilesDir()返回null的相关知识,希望对你有所帮助。

本文目录一览:

android-maven-plugin,instrumentation testing和testSize

android-maven-plugin,instrumentation testing和testSize

我正在使用maven来构建,运行和检测我的 Android应用程序. Android测试框架有三个不同的测试范围 @SmallTest,@MediumTest和 @LargeTest,android-maven-plugin有能力通过 testTestSize或 test/testSize参数选择测试范围.该参数可以是小型中的一个,可以从相关范围运行您的测试.

但是,如果我想同时进行中小型测试,不仅是小型还是非中型测试,我该怎么办?有这个问题的解决方案吗?

解决方法

根据 InstrumentationTestRunner API doc,这就是Android SDK的设计和应该如何工作的方式:

Running all small tests: adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunner

Running all medium tests: adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunner

Running all large tests: adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunner

即使您使用plain adb命令来运行测试,也必须使用两个进程分别运行中小型测试,一个接一个. Android Maven插件只是adb命令的另一个包装器,所以没有办法通过android-maven-plugin配置AFAIK来改变默认行为.

如果你仔细阅读InstrumentationTestRunner API doc,你会注意到有一个有趣的命令用法:

Filter test run to tests with given annotation: adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

If used with other options,the resulting test run will contain the union of the two options. e.g. “-e size large -e annotation com.android.foo.MyAnnotation” will run only tests with both the LargeTest and “com.android.foo.MyAnnotation” annotations.

注释配置作为实验API添加(标记为@hide,有关详细信息,请参阅this version history),并且未在am instrument options list中记录.理论上,您可以创建自己的注释类(请参阅SmallTest.java作为示例),标记所有@MediumTest与@CustomizedTest一起使用-e size和-e annotation来实现你想要的:同时从两个注释中运行union测试,所有这些都在一个命令中.

不幸的是,android-maven-plugin不支持注释配置,请参阅plugin documentation和latest source code.可能的解决方法是使用exec-maven-plugin运行plain adb shell am instrument命令.

希望这是有道理的.

Android ActivityInstrumentationT…

Android ActivityInstrumentationT…


ActivityInstrumentationTestCase2 用来测试单个的Activity,被测试的Activity可以使用InstrumentationTestCase.launchActivity 来启动,然后你能够直接操作被测试的Activity

ActivityInstrumentationTestCase2 也支持:

  • 可以在UI线程中运行测试方法.
  • 可以注入Intent对象到被测试的Activity

ActivityInstrumentationTestCase2 取代之前的ActivityInstrumentationTestCase ,新的测试应该使用ActivityInstrumentationTestCase2作为基类。

Android ActivityInstrumentationTestCase2示例 - Smile宅 - Smile宅 

Focus2ActivityTest 的代码如下,用于测试Android ApiDemos示例解析(116):Views->Focus->2. Horizontal

 

 

publicclassFocus2ActivityTest

 extendsActivityInstrumentationTestCase2<Focus2> {

  

 privateButton mLeftButton;

 privateButton mCenterButton;

 privateButton mRightButton;

  

 publicFocus2ActivityTest() {

 super("com.example.android.apis", Focus2.class);

 }

  

 @Override

 protectedvoidsetUp() throwsException {

 super.setUp();

 finalFocus2 a = getActivity();

 mLeftButton = (Button) a.findViewById(R.id.leftButton);

 mCenterButton = (Button) a.findViewById(R.id.centerButton);

 mRightButton = (Button) a.findViewById(R.id.rightButton);

 }

  

 @MediumTest

 publicvoidtestPreconditions() {

 assertTrue("center button should be right of left button",

 mLeftButton.getRight() < mCenterButton.getLeft());

 assertTrue("right button should be right of center button",

 mCenterButton.getRight() < mRightButton.getLeft());

 assertTrue("left button should be focused", mLeftButton.isFocused());

 }

  

 @MediumTest

 publicvoidtestGoingRightFromLeftButtonJumpsOverCenterToRight() {

 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);

 assertTrue("right button should be focused", mRightButton.isFocused());

 }

  

 @MediumTest

 publicvoidtestGoingLeftFromRightButtonGoesToCenter()  {

  

 getActivity().runOnUiThread(newRunnable() {

 publicvoidrun() {

 mRightButton.requestFocus();

 }

 });

 // wait for the request to go through

 getInstrumentation().waitForIdleSync();

  

 assertTrue(mRightButton.isFocused());

  

 sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);

 assertTrue("center button should be focused",

 mCenterButton.isFocused());

 }

}

setUp 中初始化mLeftButtonmCenterButtonmRightButton,调用每个测试方法之前,setUp 都会被调用。

testPreconditions 通常为第一个测试方法,用来检测后续的测试环境是否符合条件。

testGoingRightFromLeftButtonJumpsOverCenterToRight 中调用sendKeys 可以模拟按键消息。

testGoingLeftFromRightButtonGoesToCenter ,使用runOnUiThread 来为mRightButton 请求focus ,使用runOnUiThread 的原因是因为本测试方法不在UI线程中运行。  getInstrumentation 可以取得Instrumentation对象,有了Instrumentation 对象就可以对Activity进行大部分的操作,waitForIdleSync() 等待application 回到idle 状态,之后就可以检测mRightButton 是否获得了焦点。

 

Android AS 升级 3.1 编译报错:The SourceSet ''instrumentTest'' is not recognized by the Android Grad...

Android AS 升级 3.1 编译报错:The SourceSet ''instrumentTest'' is not recognized by the Android Grad...

AndroidStudio 升级到 3.1 后编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin.

项目中 gradle 配置

sourceSets {
    main {
        manifest.srcFile ''AndroidManifest.xml''
        java.srcDirs = [''src'']
        resources.srcDirs = [''src'']
        aidl.srcDirs = [''src'']
        renderscript.srcDirs = [''src'']
        res.srcDirs = [''res'']
        assets.srcDirs = [''assets'']
        jniLibs.srcDirs = [''libs'']
    }
    // Move the tests to tests/java, tests/res, etc...
    instrumentTest.setRoot(''tests'')
    // Move the build types to build-types/<type>
    // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
    // This moves them out of them default location under src/<type>/... which would
    // conflict with src/ being used by the main source set.
    // Adding new build types or product flavors should be accompanied
    // by a similar customization.
    debug.setRoot(''build-types/debug'')
    release.setRoot(''build-types/release'')
}

原因

是这里出问题了 instrumentTest.setRoot (‘tests’) ,你可能在升级 Android Studio 时更新了项目的 gradle 配置,这里的配置中 instrumentTest 已被弃用,不适用于现在的 gradle 版本。

解决办法

用 androidTest 替换 instrumentTest,编译运行即可。

Android Espresso测试’无法解析符号’InstrumentationRegistry”

Android Espresso测试’无法解析符号’InstrumentationRegistry”

我试图导入
import android.support.test.InstrumentationRegistry;

我的build.gradle文件

androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

在默认配置:

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

这里有一个图书馆吗?我正在尝试导入InstrumentationRegistry,但不能识别它!

解决方法

检查你使用什么样的测试.

仪器仪表用于仪器测试
使用仿真器或设备,它们被放置在src / androidTest中并使用config androidTestCompile.
如果您使用文件夹src / test中的JVM的本地单元测试,您应该使用
config testCompile

testCompile 'com.android.support.test:runner:0.2'

之后,您可以导入InstrumentationRegistry,但您将在运行时获得其他错误.

Android InstrumentationTestCase getFilesDir()返回null

Android InstrumentationTestCase getFilesDir()返回null

我正在使用InstrumentationTestCase对我的应用程序的一个组件进行单元测试.

该组件将数据持久保存到内部存储并使用Context :: fileList();检索持久化文件.

我遇到以下问题:在应用程序中使用此方法(在设备上)工作完全正常.但是当我尝试使用InstrumentationTestCase(Android-)单元测试(也在设备上)时,我在fileList()方法中得到一个NullPointerException.我深入研究android源代码,发现getFilesDir()(see source here)返回null并导致此错误.

要重现的代码如下:

public class MyTestCase extends InstrumentationTestCase
{   
    public void testExample() throws Exception
    {
        assertNotNull(getInstrumentation().getContext().getFilesDir()); // Fails
    }
}

我的问题是:这种行为是否有意?我该怎么做才能绕过这个问题?我是使用InstrumentationTestCase还是应该使用不同的东西?

我找到了this question,但我不确定这是否涵盖了我遇到的同样问题.

解决方法

我认为您将测试数据与测试应用程序分开是正确的.

您可以通过执行以下命令为Instrumentation app创建文件目录来解决Null问题

adb shell
cd /data/data/<package_id_of_instrumentation_app>
mkdir files

您只能在模拟器或root设备上执行此操作.

然后从你的问题测试不会失败.我做了它并且还将名为tst.txt的文件上传到文件dir,所有以下测试都成功:

assertNotNull(getInstrumentation().getContext().getFilesDir());
assertNotNull(getInstrumentation().getContext().openFileInput("tst.txt"));
assertNotNull(getInstrumentation().getContext().openFileOutput("out.txt",Context.MODE_PRIVATE));

但我认为为测试项目提供数据的更方便的方法是使用测试项目的资产,您可以在其中简单地保存一些文件并打开它们:

assertNotNull(getInstrumentation().getContext().getAssets().open("asset.txt"));

或者如果要将某些测试结果保存到文件中,可以使用ExternalStorage:

File extStorage = Environment.getExternalStorageDirectory();
assertNotNull(extStorage);

关于android-maven-plugin,instrumentation testing和testSize的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android ActivityInstrumentationT…、Android AS 升级 3.1 编译报错:The SourceSet ''instrumentTest'' is not recognized by the Android Grad...、Android Espresso测试’无法解析符号’InstrumentationRegistry”、Android InstrumentationTestCase getFilesDir()返回null等相关知识的信息别忘了在本站进行查找喔。

本文标签: