Android - ActivityUnitTestCase - 测试总是通过

标签 android testing android-studio

我正在使用 Android Studio 来尝试和测试我的 Activity 。这是基本代码:

public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
    public MyActivityTest() {
        super(MyActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @SmallTest
    public void testSomething() {
        Assert.assertNotNull("something is null", null);
    }
}

我希望这个测试用例失败。我尝试的一切都通过了。这似乎是一个奇怪的问题,但我怎样才能让我的测试用例失败呢?我做错了什么?

最佳答案

我设法让这个工作,有点。我在 bug report 上找到了这个:

We are in the process of deprecating ActivityUnitTestCase. We recommend to move business logic to a separate class and unit test it with gradle unit test support (mockable android.jar).

所以我改为扩展 ActivityInstrumentationTestCase2 并将测试作为仪器测试而不是单元测试运行。那奏效了。这基本上是我现在所拥有的:

public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
    public MyActivityTest() {
        super(MyActivity.class);
    }

    public void testSomething() throws Exception {
        //test goes here

        Assert.assertEquals(message, expectedObject, actualObject);
    }
}

我仍然不确定为什么我会看到之前的行为,但至少我现在可以测试了。这是我的测试构建配置的屏幕截图:

enter image description here

关于Android - ActivityUnitTestCase - 测试总是通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329319/

相关文章:

android - 如何在 Android 中从 PostExecute 正确启动 Activity ?

Android:测试应用内计费的策略?

javascript - 如何使用 jasmine 测试带有 DOM 元素的 JavaScript?

android - 如何在 Android Studio 项目中同步期间禁用 *sources.jar 下载

在 android studio 中使用设备调试时,Android Logcat 为空

android - 服务在特定时间运行 Android

Android 模拟器在 MBP M1 Pro 上停止响应

android - 高亮显示的 EditText 文本颜色

reactjs - 如果你使用React测试库/Enzyme的mount函数,它仍然是 "unit testing"吗?

android - 如何打开具有不同名称的 Android 项目的副本?