标签: android-testing

什么单元测试,在Android应用程序中

我的应用程序主要是GUI,它们与服务器通信以获取大部分信息.如果出现任何问题,通常会在网络调用中或对JSON对象做出错误的假设.

单元测试不适合这些与网络相关的任务和与i/o相关的任务,否则它们不会被称为单元测试.

所以我试图在我的案例中收集单元测试的要点.为什么我会测试Android按钮是否可以单击或EditText可以看到我键入的内容?我只是不明白实现这些繁琐的测试的效用

private void initElements(){
    placeButton = (Button) findViewById(R.id.currplace);
    placeButton.setText(MainActivity.this.getString(R.string.findingLocation));
    placeButton.setEnabled(false);
    selectplaceLayout = (LinearLayout)findViewById(R.id.selectplaceLayout);
    selectplaceLayout.setVisibility(View.GONE);
    splash = (RelativeLayout)findViewById(R.id.splashbg);
    infoLayout = (LinearLayout)findViewById(R.id.infoLayout);
}
Run Code Online (Sandbox Code Playgroud)

如果以上方法通过,我的所有活动都在onCreate中运行,那么我知道应用程序有效.对此进行单元测试将是一个冗余耗时的事情.耗时,因为我不熟悉jUnit和Android测试框架中的所有方法.

所以,长话短说,重点是什么?有什么特别的方法我应该考虑这些测试吗?到目前为止,我所看到的所有示例和教程仅仅讨论了最简单的示例,为了简洁起见,但我无法想到在主要的客户端 - 服务器应用程序中进行单元测试的任何实际用途.

通过访问我已经知道并声明并初始化的android视图,我期望发现什么?我必须以过于有限的方式思考这个问题

所以,洞察力赞赏

junit android unit-testing robotium android-testing

15
推荐指数
1
解决办法
1453
查看次数

Android Base64编码和解码在单元测试中返回null

我正在尝试使用http://developer.android.com/reference/android/util/Base64.html类在Android中解码Base64编码的字符串.

encodeToString和decode方法都返回null,我不知道出了什么问题,这是我的解码代码:

// Should decode to "GRC"
String friendlyNameBase64Encoded = "R1JD";

// This returns null
byte[] friendlyNameByteArray = Base64.decode(friendlyNameBase64Encoded, Base64.DEFAULT);

// Fails with NullPointerException
String friendlyName = new String(friendlyNameByteArray, "UTF-8");
Run Code Online (Sandbox Code Playgroud)

我正在运行Android API 23.1.0

java junit android unit-testing android-testing

15
推荐指数
4
解决办法
6051
查看次数

Android Espresso Ui测试验证ActionPage的标签文本

我正在尝试使用Espresso测试ActionPage的文本.但是,当我运行Ui Automation Viewer时,我可以看到ActionPage显示为View而不是ActionView,它没有TextView.

我试过像这样检查ActionLabel文本,但这不起作用:

onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
Run Code Online (Sandbox Code Playgroud)

我有一个我的ActionPage的ID,所以我可以找到它onView(withId(R.id.actionPage))但我不知道如何访问它的孩子来获取ActionLabel文本.我尝试编写自定义匹配器,但这也不起作用:

onView(withId(R.id.actionPage)).check(matches(withChildText("MyText")));

static Matcher<View> withChildText(final String string) {
        return new BoundedMatcher<View, View>(View.class) {
            @Override
            public boolean matchesSafely(View view) {
                ViewGroup viewGroup = ((ViewGroup) view);
                //return (((TextView) actionLabel).getText()).equals(string);
                for(int i = 0; i < view.getChildCount(); i++){
                    View child = view.getChildAt(i);
                    if (child instanceof TextView) {
                        return ((TextView) child).getText().toString().equals(string);
                    }
                }
                return false;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with child text: " + string);
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我,ActionLabel似乎没有自己的id,它不是TextView ...我怎么能检查它里面的文字?

+------>FrameLayout{id=-1, visibility=VISIBLE, width=320, height=320, …
Run Code Online (Sandbox Code Playgroud)

android ui-automation android-testing android-espresso

15
推荐指数
1
解决办法
1996
查看次数

如何在Android Studio中调试检测测试?

在Android Studio中,当我调试检测测试时,测试不会在任何断点上停止.调试单元测试工作.我有一个简单的检测测试,只检查是否显示用户名edittext:

@RunWith(AndroidJUnit4.class)
public class LogonActivityTest {

    @Rule
    public ActivityTestRule<LogOnActivity> mActivityRule = new ActivityTestRule<>(LogOnActivity.class, true, false);

    @Before
    public void setUp() throws Exception {
        mActivityRule.launchActivity(new Intent()); // breakpoint here
    }

    @Test
    public void testSimple() throws Exception {
        onView(withId(R.id.act_logon_et_username)).check(matches(isDisplayed())); // breakpoint here
    }
}
Run Code Online (Sandbox Code Playgroud)

build.gradle我已正确设置

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Run Code Online (Sandbox Code Playgroud)

如何调试检测测试?我正在使用Espresso,Mockito和Dagger 2.

android mockito android-testing android-studio android-espresso

15
推荐指数
1
解决办法
5543
查看次数

在InstrumentationTestCase运行之间重置应用程序状态

我的一位QA工程师正在支持具有相当大的代码库和许多不同的SharedPreferences文件的应用程序.前几天他来找我,询问如何在测试运行之间重置应用程序状态,就像它已经卸载 - 重新安装一样.

Espresso(他正在使用)和Android测试框架本身都不支持它,所以我不知道该告诉他什么.使用本机方法清除所有不同的SharedPreferences文件将是一个非常脆弱的解决方案.

如何在仪表期间重置应用程序状态?

android-testing android-espresso android-instrumentation

15
推荐指数
3
解决办法
1万
查看次数

如何使用Espresso测试特定活动?

我刚刚开始在Android中进行测试,这看起来非常基本,但经过大量的谷歌搜索,我仍然无法找到答案.

在我的Android应用程序中,显示的第一个活动是登录屏幕,然后是主屏幕,其中包含导航到其他活动的选项.为了测试我想要的活动,我现在必须首先完成这两项活动.

如何设置Espresso测试(使用ActvityTestRule/JUnit4),以便它立即启动我想要测试的活动?

编辑

更具体地说,我的问题是,在Espresso测试的所有教程中,所有测试都从应用程序的主要活动开始,其ActivityTestRule看起来像这样

@Rule
public final ActivityTestRule<MainActivity> mRule = new ActivityTestRule<MainActivity>(MainActivity.class);
Run Code Online (Sandbox Code Playgroud)

我希望测试从指定的活动开始,目前我通过不断重复这样的测试代码来导航

onView(withId(R.id.go_to_other_activity_button)).perform(click())
Run Code Online (Sandbox Code Playgroud)

android android-testing android-espresso

15
推荐指数
1
解决办法
9323
查看次数

升级到Android Studio 3.0后不存在支持注释

将项目升级到AndroidStudio 3.0-beta1后,androidTest文件停止编译。

找不到很多软件包,其中一些是:

错误:程序包android.support.annotation不存在
错误:找不到符号类StringRes
错误:无法访问
android.support.v7.app.AppCompatActivity的AppCompatActivity 类文件

我已经加了

androidTestCompile "com.android.support:support-annotations:25.3.1"
Run Code Online (Sandbox Code Playgroud)

进入build.gradle

但是即使这样,我仍然没有找到错误的包。我尝试从Android Studio内部和终端运行测试./gradlew connectedCheck

android android-testing android-studio gradle-android-test-plugi android-studio-3.0

15
推荐指数
2
解决办法
2万
查看次数

Espresso - 使用异步加载的数据断言TextView

我正在使用谷歌Espresso for Android编写UI测试,我一直坚持如何断言TextView文本,这些内容是从Web服务异步加载的.我目前的代码是:

public class MyTest extends BaseTestCase<MyActivity>{
    public void setUp() throws Exception {
        // (1) Tell the activity to load 'element-to-be-loaded' from webservice
        this.setActivityIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("data://data/element-to-be-loaded")));
        getActivity();

        super.setUp();
    }

    public void testClickOnReviews(){
        // (2) Check the element is loaded and its name is displayed
        Espresso
            .onView(ViewMatchers.withId(R.id.element_name))
            .check(ViewAssertions.matches(ViewMatchers.withText("My Name")));

        // (3) Click on the details box
        Espresso
            .onView(ViewMatchers.withId(R.id.details_box))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // (4) Wait for the details screen to open
        Espresso
            .onView(ViewMatchers.withId(R.id.review_box));

        // Go back to element screen
        Espresso.pressBack();
    }
} …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-espresso

14
推荐指数
1
解决办法
2万
查看次数

如何自动化android的升级测试

我们一直在使用espresso进行Android自动化,其中包括升级测试

对于升级测试,我们需要执行3个步骤:

  1. 在旧版本中执行一些操作以准备一些数据
  2. 升级到新版本(封面安装)
  3. 检查旧版本中保存的数据是否已正确保留,升级后无其他问题.

目前我们正以非常笨拙的方式做到这一点:

#Before: prepare data on old version
adb -s $DEVICE shell am instrument -e class com.example.test.upgrade.UpgradeTest#prepareDataIn${version} -w com.example.test/com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner;

#install new version
adb -s $DEVICE install -r new_version.apk;

#After: test after upgrading
adb -s $DEVICE shell am instrument -e class com.example.test.upgrade.UpgradeTest#testUpgradeFrom${version} -w com.example.test/com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner;
Run Code Online (Sandbox Code Playgroud)

我们将升级测试从某个版本分解为2个部分之前/之后,因为我们不知道我们是否能够(以及如何)在测试中安装新版本.

但是,adb命令的这个3步测试看起来很愚蠢,我们无法轻易获得junit报告.

那么有谁知道一个更好的方法来进行Android升级测试,或者你能指出我们做错了什么?

它不仅限于Espresso,如果您正在使用其他框架,您如何使用它进行升级测试?

提前致谢.

junit android automated-tests android-testing android-espresso

14
推荐指数
1
解决办法
749
查看次数

运行Espresso测试时,Dagger代码给出NoClassDefFoundError,正常运行正常

开始探索Espresso 2.0,但似乎遇到了打嗝.我无法让测试成功运行任何包含Dagger的项目.当我运行测试时,我得到以下异常(最后的整个堆栈跟踪):

java.lang.NoClassDefFoundError: com/pdt/daggerexample/model/DaggerExampleAppModule$$ModuleAdapter$ProvideMySingletonProvidesAdapter
Run Code Online (Sandbox Code Playgroud)

应用程序在未从AndroidInstrumentationTest运行时运行.

以下是一些相关文件,我还将项目上传到github,以便更快地结账/复制https://github.com/paul-turner/espressoDaggerExample.

的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.pdt.daggerexample"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
        }


    }

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.jakewharton:butterknife:5.1.1'
    compile 'com.squareup.dagger:dagger:1.2.2'
    provided 'com.squareup.dagger:dagger-compiler:1.2.2' …
Run Code Online (Sandbox Code Playgroud)

android android-testing dagger android-espresso

14
推荐指数
1
解决办法
4736
查看次数