- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要在我的 Android 应用程序中测试单个 Activity 。 ActivityInstrumentationTestCase2
的文档说:
This class provides functional testing of a single activity.
以及 ActivityTestRule
的文档说:
This rule provides functional testing of a single activity.
几乎相同的词。除了我编写的两个示例之外,执行相同的操作。所以我应该更喜欢 ActivityTestRule
在 ActivityInstrumentationTestCase2
反之亦然?
我看到的是扩展 ActivityInstrumentationTestCase2
看起来像 JUnit3 风格的测试(它的祖先是 junit.framework.TestCase
,测试方法应该以单词 test
开头)。
使用 ActivityTestRule
package sample.com.sample_project_2;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {
@Rule
public ActivityTestRule<SecAct> mActivityRule = new ActivityTestRule(SecAct.class);
@Test
public void foo() {
onView(withId(R.id.editTextUserInput)).perform(typeText("SAMPLE"));
}
}
扩展 ActivityInstrumentationTestCase2
package sample.com.sample_project_2;
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class ApplicationTest2 extends ActivityInstrumentationTestCase2<SecAct> {
public ApplicationTest2() {
super(SecAct.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testFoo2() {
onView(withId(R.id.editTextUserInput)).perform(typeText("SAMPLE 2"));
}
}
最佳答案
对于您的示例,没有区别。您可以使用其中任何一个。
根据 OO 原则,我们应该“优先组合而不是继承”。 ActivityTestRule<>
的用法是通过合成而ActivityInstrumentationTestCase2<>
虽然是继承。
有时,我更喜欢为我的测试类提供一个公共(public)基类,以便重用公共(public)初始化。这有助于我根据主题对测试进行分组。 ActivityTestRule<>
允许我做这样的事情。
出于这些原因,我更喜欢 ActivityTestRule<>
.否则,我没有看到任何区别。
关于android - ActivityInstrumentationTestCase2 与 ActivityTestRule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140559/
我已经编写了工作正常的仪器测试,但是在将依赖项升级到之后出现错误无法解决 ActivityTestRule 错误 androidTestImplementation 'com.android.supp
我在 gradle 中添加了以下依赖项: 但是还是无法导入ActivityTestRule,报错 最佳答案 看起来您正在使用 Espresso 2.2.2 中的旧依赖项。对于当前版本(Espresso
我正在学习使用 espresso 来测试我的应用程序,但是 ActivityTestRule 总是无法解决。这是我的 app/build.gradle 配置,我确定我将测试类放在 src/androi
我需要在我的 Android 应用程序中测试单个 Activity 。 ActivityInstrumentationTestCase2 的文档说: This class provides funct
我正在尝试使用 androidX espresso 库通过仪器测试来测试我的 UI。在我的年级,我有: apply plugin: 'com.android.application' apply pl
这是我的仪器测试的范围: ... import androidx.test.rule.ActivityTestRule @RunWith(AndroidJUnit4::class) @LargeTes
我修改了 ActivityTestRule 以重试执行失败的测试用例。每当测试失败时,我都需要重新启动 Activity 并重新运行执行。我已尝试调用 mActivity.finish() 但当前 A
在探索测试时,我遇到了 ActivityTestRule 和 IntentTestRule,据我所知,IntentTestRule 是 ActivityTestRule 的扩展,用于 Espresso
我正在使用 Espresso 2.1 和 ActivityTestRule,我正在寻找一种方法来在我的应用程序中调用 onCreate() 之前设置一些静态标志。 我有一些我不想在仪器测试期间调用的初
ActivityTestRule类在其构造函数中接受 initialTouchMode 参数。类(class)引用(或在线任何地方)中没有解释这一点,除非如下: initialTouchMode -
我想在测试 Activity 中测试一个 fragment 。我将 TestTragmentActivity 添加到 androidTest 以及 AndroidManifest.xml 文件。但是当
我有一个用于 UI 测试的 Espresso 测试套件,如下所示: @RunWith(AndroidJUnit4.class) public class SpecialUiTests { @R
我需要在每次测试前清空我的用户数据 // Kotlin code fun getActivity() = activityRule.getActivity() Before fun setUp() {
我在导入一些 Android UI 测试框架类时遇到了问题 - 我就是不知道出了什么问题! 这是我的课: @RunWith(AndroidJUnit4.class) @LargeTest public
我有一个接受深层链接的应用程序。 list .xml:
SingleLaunchActivityTestCase 的文档表示此类现已弃用,应替换为 ActivityScenarioRule或 ActivityTestRule .但是这是如何做到的呢? Si
我正在尝试在 Kotlin 中为我的 android 应用程序进行 UI 测试。由于使用 ActivityTestRule 的新系统,我无法使其工作:它编译正确,并且在运行时,我得到: java.la
我正在使用 Kotlin 开发 Android 应用程序。我正在将仪器测试添加到我的项目中。现在,我正在寻找一种方法来启用/禁用启动 Activity 的逻辑,因为它会使我的测试以意想不到的方式运行。
我怎么听ActivityTestRule's beforeActivityLaunched() android测试中的方法。 我的解决方法是创建自定义 ActivityTestRule 并在构造函数上
我尝试用 espresso 初始化我的 Activity 并模拟 onCreate 但没有找到一个好的解决方案,而不是将 Dagger 添加到我的项目中: @Rule public ActivityT
我是一名优秀的程序员,十分优秀!