Android - 独立的 Fragment UI 测试工具 [英] Android - Independent Fragment UI testing tool
问题描述
我一直在寻找一种方法来单独测试我的 Fragment 的 UI(即,独立于其他 Fragment 和 Activity),但我找不到办法做到这一点.
I've been looking for a way to test the UI of my Fragments separately (ie, independently from other fragments and activities) but I can't find a way to do it.
特别是,假设我有 Fragment A、Fragment B 和 Fragment C.进入 Fragment C 的唯一方法(应用方面)是首先通过 Fragment A 和 Fragment B.我正在寻找一种直接测试 Fragment C 的方法(可能通过模拟其依赖项,如果存在的话),而不必通过 Fragment A 和 B.
In particular, let's say I have Fragment A, Fragment B and Fragment C. The only way (app-wise) to go to Fragment C is by passing through Fragment A and Fragment B first. I am looking for a way to test Fragment C directly (potentially by mocking its dependencies, if any exists), without having to pass through Fragment A and B.
我目前研究的工具:
monkey:仅用于通过命令行生成伪随机事件.不是我想要的.
monkey: only used to generate pseudo-random events through command line. Not what I want.
monkeyrunner:它可以运行 Python 程序将事件流发送到我的 Android 应用程序,但它不能直接使用这些脚本针对特定 Fragment.
monkeyrunner: it can run Python programs to send event streams to my Android app, but it cannot target a particular Fragment directly with those scripts.
Espresso:白盒测试工具.这与我想要的很接近,但它仍然需要在到达 Fragment C 之前通过 Fragment A 和 B(即,您需要启动您的应用,然后测试将从那里运行).
Espresso: white-box testing tool. This comes close to what I want, but it still requires passing through Fragment A and B before reaching Fragment C (ie, you need to start your app and then the tests will run from there).
UI Automator:黑盒测试工具.这也很接近,但同样,在测试我想要的片段(片段 C)之前,它需要通过前面的片段.
UI Automator: black-box testing tool. This also comes close, but again, it requires passing through the previous Fragments before testing the one I want (Fragment C).
有没有办法直接测试一个 Fragment 的 UI?
Is there any way to test the UI of a Fragment directly?
推荐答案
我正在使用自定义 FragmentTestRule
和 Espresso 来单独测试我的每个 Fragment
.
I'm am using a custom FragmentTestRule
and Espresso to test each of my Fragments
in isolation.
我有一个专用的 TestActivity
来显示我的应用中经过测试的 Fragments
.就我而言,Activity
仅存在于 debug
变体中,因为我的检测测试针对 debug
运行.
I have a dedicated TestActivity
that shows the tested Fragments
in my app. In my case the Activity
only exists in the debug
variant because my instrumentation tests run against debug
.
TL;DR 使用 FragmentTestRule 库由 @brais-gabin.
1.在 src/debug/java/your/package/TestActivity.java
中创建一个 TestActivity
和一个内容视图,其中测试了 Fragment
将添加到:
1. Create a TestActivity
in src/debug/java/your/package/TestActivity.java
with a content view where the tested Fragment
will be added to:
@VisibleForTesting
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(R.id.container);
setContentView(frameLayout);
}
}
2.为 debug
变体创建一个 AndroidManifest.xml 并声明 TestActivity
.这是在测试时启动 TestActivity
所必需的.将此 Manifest 添加到 src/debug/AndroidManifest.xml
中的 debug
变体:
2. Create a AndroidManifest.xml for the debug
variant and declare the TestActivity
. This is required to start the TestActivity
when testing. Add this Manifest to the debug
variant in src/debug/AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name="your.package.TestActivity"/>
</application>
</manifest>
3.在 src/androidTest/java/your/test/package/FragmentTestRule.java
的 androidTest
变体中创建 FragmentTestRule
:
3. Create the FragmentTestRule
in the androidTest
variant at src/androidTest/java/your/test/package/FragmentTestRule.java
:
public class FragmentTestRule<F extends Fragment> extends ActivityTestRule<TestActivity> {
private final Class<F> mFragmentClass;
private F mFragment;
public FragmentTestRule(final Class<F> fragmentClass) {
super(TestActivity.class, true, false);
mFragmentClass = fragmentClass;
}
@Override
protected void afterActivityLaunched() {
super.afterActivityLaunched();
getActivity().runOnUiThread(() -> {
try {
//Instantiate and insert the fragment into the container layout
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
mFragment = mFragmentClass.newInstance();
transaction.replace(R.id.container, mFragment);
transaction.commit();
} catch (InstantiationException | IllegalAccessException e) {
Assert.fail(String.format("%s: Could not insert %s into TestActivity: %s",
getClass().getSimpleName(),
mFragmentClass.getSimpleName(),
e.getMessage()));
}
});
}
public F getFragment(){
return mFragment;
}
}
4.然后你可以单独测试Fragments
:
public class MyFragmentTest {
@Rule
public FragmentTestRule<MyFragment> mFragmentTestRule = new FragmentTestRule<>(MyFragment.class);
@Test
public void fragment_can_be_instantiated() {
// Launch the activity to make the fragment visible
mFragmentTestRule.launchActivity(null);
// Then use Espresso to test the Fragment
onView(withId(R.id.an_id_in_the_fragment)).check(matches(isDisplayed()));
}
}
这篇关于Android - 独立的 Fragment UI 测试工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!