标签: android-testing

espresso-contrib 3.3.0 工作正常,但任何较新的版本都会导致构建失败

我们已经定义了依赖关系androidTestImplementation("androidx.test.espresso:espresso-contrib:3.3.0")。仪器测试工作正常。但是当我们尝试更新到较新的版本(例如 3.4.0 或 3.5.0)时,我们收到以下错误:

Instrumentation did not complete:
Instrumentation code: 0
Instrumentation result bundle: 

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedDevShortTimeoutDebugAndroidTest'.
> There were failing tests. See the report at: 
Run Code Online (Sandbox Code Playgroud)

报告显示以下内容: 测试总结

我找到了Android Tests:没有找到测试Gradle:没有找到测试,但没有一个给我指出解决方案。

使用以下 androidTestImplementation 依赖项:

androidTestImplementation(project(":sharedTest"))
androidTestImplementation("androidx.test:core-ktx:1.5.0")
androidTestImplementation("androidx.test:rules:1.5.0")
androidTestImplementation("androidx.test:runner:1.5.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
androidTestImplementation("androidx.test.ext:junit-ktx:1.1.4")
androidTestImplementation("androidx.test.espresso:espresso-contrib:3.3.0")
androidTestImplementation("com.microsoft.appcenter:espresso-test-extension:1.5")
androidTestImplementation("org.mockito:mockito-android:$mockitoTestVersion")
androidTestImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
androidTestUtil("androidx.test:orchestrator:1.4.2")
Run Code Online (Sandbox Code Playgroud)

有谁有提示或解决方案吗?

android-testing android-espresso

5
推荐指数
1
解决办法
684
查看次数

编写Android测试惰性列未检测到所有孩子

我在可组合项中有一个 LazyColumn,我想检查该 LazyColumn 中的每个项目是否存在。

这是我的可组合项:(注意:实际上在我的实际情况中,每个项目都有不同的组件,我只是使其相似以简化问题)

@Composable
fun HomeScreen(){
    LazyColumn(){
        item {
            HomeBanner()
            Text("Test1",Modifier.testTag("tag1"))
        }
        item {
            HomeBanner()
            Text("Test2",Modifier.testTag("tag2"))
        }
        item {
            HomeBanner()
            Text("Test3",Modifier.testTag("tag3"))
        }
        item {
            HomeBanner()
            Text("Test4",Modifier.testTag("tag4"))
        }
        item {
            HomeBanner()
            Text("Test5",Modifier.testTag("tag5"))
        }
        item {
            HomeBanner()
            Text("Test6",Modifier.testTag("tag6"))
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这就是它的样子:

在此输入图像描述

这是我的测试课:


@ExperimentalCoroutinesApi
@ExperimentalMaterialApi
@ExperimentalPagerApi
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class HomeScreenTest {
    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @ExperimentalMaterialApi
    @get:Rule(order = 1)
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Inject
    lateinit var mainRepository: MainRepository

    @Before
    fun setup() {
        hiltRule.inject()
    }

    @Test
    fun property_list_is_displayed() …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-jetpack-compose lazycolumn android-jetpack-compose-testing

5
推荐指数
2
解决办法
3757
查看次数

是否可以使用Robolectric测试抽象活动

我在代码中使用抽象活动类,从活动类中抽象出一些特性.

我正在尝试使用扩展抽象类Robolectricgradle-android-test-plugin使用子类来测试抽象活动类.我似乎无法让它工作.

有没有人在这方面有任何经验,甚至可能吗?基本结构是:

@RunWith(RobolectricGradleTestRunner.class)
public class AbstractActivityTest {
    private ActivityTest activity;

    @Before
    public void setUp() throws Exception {
        activity = Robolectric.buildActivity(ActivityTest.class).create().get();
    }


    private class ActivityTest extends AbstractActivity {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

最初,我收到了子类不是静态的错误消息,所以我把它设为静态.现在我得到以下两个失败:

initializationError FAILED
java.lang.Exception: Test class should have exactly one public constructor

initializationError FAILED
java.lang.Exception: No runnable methods
Run Code Online (Sandbox Code Playgroud)

我在@Test方法中的任何明显真实的测试都会成功.

junit android robolectric android-testing

4
推荐指数
1
解决办法
1740
查看次数

如何自动化DDMS快照机制?

Android DDMS是否提供API?我需要自动化本机堆的快照机制.或者是否有任何用于分析DDMS本机堆快照的后处理工具.

android ddms android-ndk android-testing

4
推荐指数
2
解决办法
4028
查看次数

如何使用Mockito在Robospice和Retrofit上测试API?

我一直在关注这篇博客文章,其中展示了如何使用Mockito和Retrofit模拟请求.问题是我在Robospice上使用它们,它不需要Callback在服务接口上提供as参数(因为它将是一个同步调用):

@GET("/foo/bar")
User foo(@Query("bar") String baz);
Run Code Online (Sandbox Code Playgroud)

所以我不能以这种方式拦截我测试的回调:

Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
User user = new User();
cb.getValue().success(user, null);
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个目标?谢谢!

android mockito android-testing robospice retrofit

4
推荐指数
1
解决办法
900
查看次数

Android Instrumentation HOME按钮

任何想法为什么将HOME键发送到在Instrumentation下运行的应用程序什么都不做?

import android.app.Instrumentation;
import android.view.KeyEvent;

public class MyInstrumentation extends Instrumentation {

  public void sendKeys() {
    // Works reliably
    this.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);

    // Does nothing, nothing is printed to logcat
    this.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
  }
}
Run Code Online (Sandbox Code Playgroud)

使用Robotium时以及直接使用Instrumentation时也是如此.模拟器和非root设备上的行为相同.

instrumentation android robotium android-testing android-instrumentation

4
推荐指数
1
解决办法
951
查看次数

针对viewpager listviews的Espresso 2.0 AmbiguousViewMatcherException

当我尝试在测试下运行时,我得到了AmbiguousViewMatcherException,如何从多个视图中获得预期的视图.

昂达(hasToString(startsWith( "ALS")))inAdapterView(allOf(withId(android.R.id.list)))atPosition(0).perform(点击())..

android.support.test.espresso.AmbiguousViewMatcherException: '(with id: android:id/list)' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1440, height=2560, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1440, height=2392, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909171, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=16908290, res-name=content, …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-espresso

4
推荐指数
1
解决办法
2928
查看次数

如何在Espresso中测试ActionMenuItemView的图标

我在操作栏中有一个按钮,其图标根据布尔值而更改。我想检查使用了哪个可绘制资源。

这是更改图标的代码:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem item = menu.findItem(R.id.menu_favorite);
    if(mIsFavorite)
        item.setIcon(R.drawable.ab_icon_on);
    else
        item.setIcon(R.drawable.ab_icon_off);
}
Run Code Online (Sandbox Code Playgroud)

当需要更改图标时,菜单无效:

// request menu update
supportInvalidateOptionsMenu();
Run Code Online (Sandbox Code Playgroud)

最后,我要检查结果的espresso代码:

@Test
public void action_setUnsetFavorite() {
    // check favorite off
    onView(withImageDrawable(R.drawable.ab_icon_off))
            .check(matches(isDisplayed()));

    // click favorite button
    onView(withId(R.id.menu_favorite))
            .perform(click());

    // check favorite on
    onView(withImageDrawable(R.drawable.ab_icon_on))
            .check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

请注意,我使用的是此处找到的自定义匹配器。

android android-actionbar android-testing android-support-library android-espresso

4
推荐指数
1
解决办法
1359
查看次数

如何在espresso测试中调用自定义视图上的方法?

我有一个自定义视图,我需要调用一个特定的方法来打开一个活动.在浓缩咖啡测试中,这样做的正确方法是什么?我只需要膨胀这个视图或者我需要编写一个自定义的ViewAction?

android android-layout android-testing android-espresso

4
推荐指数
1
解决办法
2876
查看次数

MockWebServer:llegalStateException:start()已被调用

我尝试使用MockWebServer进行测试。

我想用模拟的响应进行UI测试,以便可以测试有效\无效的UI更改,例如登录或在登录API中显示错误。

但是,每次我运行代码时,都会得到以下异常:

java.lang.IllegalStateException: start() already called
Run Code Online (Sandbox Code Playgroud)

码:

@RunWith(AndroidJUnit4.class)
public class UITestPlayground {

    String testUrl = "http://testurl.com/";
    MockWebServer server = new MockWebServer();

    @Rule
    public IntentsTestRule<LoginActivity> mIntentsRule = new IntentsTestRule<>(LoginActivity.class);

    @Before
    public void beforeHelper() throws IOException {
        TestHelper.removeUserAndTokenIfAny(mIntentsRule.getActivity());
        URLS.baseUrl = testUrl;
        server.url(URLS.baseUrl);
        //try to shutting down the server JUT IN CASE...
        server.shutdown();
        server.start();

    }

    @After
    public void afterHelper() throws IOException {
        server.shutdown();
    }


    @Test
    public void invalidLoginDueNotValidJSONResponse() {

        server.enqueue(new MockResponse().setBody("Something not valid JSON response"));

        String emailToBeTyped = "tester@tester.com"; …
Run Code Online (Sandbox Code Playgroud)

android-testing mockwebserver

4
推荐指数
1
解决办法
1094
查看次数