Android:为片段编写测试用例
在我以前的项目中,我通过活动完成了大部分工作,并根据文档使用了ActivityInstrumentationTestCase2:Android:为片段编写测试用例,android,unit-testing,android-fragments,android-testing,Android,Unit Testing,Android Fragments,Android Testing,在我以前的项目中,我通过活动完成了大部分工作,并根据文档使用了ActivityInstrumentationTestCase2: 我知道如何使用活动测试用例;但说到片段,我没有太多的想法,也没有找到太多与之相关的文档。 那么,当我有几个片段具有一个或两个活动性时,如何编写测试用例呢? 任何示例代码或示例都会更有帮助。以下是使用ActivityInstrumentationTestCase2的粗略指南: 步骤1。创建一个空白活动以保存片段 步骤2: 在您的测试中,实例化您的片段并将其添加到空白活
我知道如何使用活动测试用例;但说到片段,我没有太多的想法,也没有找到太多与之相关的文档。 那么,当我有几个片段具有一个或两个活动性时,如何编写测试用例呢?
任何示例代码或示例都会更有帮助。以下是使用
ActivityInstrumentationTestCase2的粗略指南:
步骤1。创建一个空白活动以保存片段
步骤2:
在您的测试中,实例化您的片段并将其添加到空白活动
public class MyFragmentTest extends ActivityInstrumentationTestCase2<FragmentUtilActivity> {
private MyFragment fragment;
@Before
public void setup() {
fragment = new MyFragment();
getActivity().getFragmentManager().beginTransaction().add(1, fragment, null).commit();
}
}
如果您使用的是robolectric,那么使用该类非常简单:
将主活动用作发送给ActivityInstrumentationTestCase2的测试活动。然后,您可以通过启动片段的主要活动的片段管理器来处理片段。这甚至比拥有一个测试活动更好,因为它使用您在主活动中编写的逻辑来测试场景,从而提供更完整的测试
例如:
public class YourFragmentTest extends ActivityInstrumentationTestCase2<MainActivity> {
public YourFragmentTest(){
super(MainActivity.class);
}
}
public类YourFragmentTest扩展了ActivityInstrumentationTestCase2{
公共碎片测试(){
超级(MainActivity.class);
}
}
以下是我的工作解决方案:
在androidTest目录中为此创建一个instrumentation单元测试类,即:
public class FragmentTest extends
ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity testingActivity;
private TestFragment testFragment;
//...
}
重写setUp()方法(确保活动类中有R.id.fragmentContainer),您将在该方法末尾调用waitForIdleSync():
@Override
protected void setUp() throws Exception {
super.setUp();
// Starts the activity under test using
// the default Intent with:
// action = {@link Intent#ACTION_MAIN}
// flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
// All other fields are null or empty.
testingActivity = getActivity();
testFragment = new TestFragment();
testingActivity.getFragmentManager().beginTransaction().add(R.id.fragmentContainer,testFragment,null).commit();
/**
* Synchronously wait for the application to be idle. Can not be called
* from the main application thread -- use {@link #start} to execute
* instrumentation in its own thread.
*
* Without waitForIdleSync(); our test would have nulls in fragment references.
*/
getInstrumentation().waitForIdleSync();
}
编写一个测试方法,例如:
public void testGameFragmentsTextViews() {
String empty = "";
TextView textView = (TextView)testFragment.getView().findViewById(R.id.myTextView);
assertTrue("Empty stuff",(textView.getText().equals(empty)));
}
运行测试
目前,ActivityInstrumentationTestCase2已被弃用。现在,您可以使用规则在测试中使用活动:
为了使这些功能正常工作,您必须将依赖项添加到您的build.gradle中:
testCompile 'com.android.support.test:rules:0.5'
testCompile 'com.android.support.test:runner:0.5'
(请参见)AndroidX提供了一个库,FragmentScenario
,用于创建片段并更改其状态
app/build.gradle
dependencies {
def fragment_version = "1.0.0"
// ...
debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'
}
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
// The "fragmentArgs" and "factory" arguments are optional.
val fragmentArgs = Bundle().apply {
putInt("selectedListItem", 0)
}
val factory = MyFragmentFactory()
val scenario = launchFragmentInContainer<MyFragment>(
fragmentArgs, factory)
onView(withId(R.id.text)).check(matches(withText("Hello World!")))
}
}
示例
dependencies {
def fragment_version = "1.0.0"
// ...
debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'
}
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
// The "fragmentArgs" and "factory" arguments are optional.
val fragmentArgs = Bundle().apply {
putInt("selectedListItem", 0)
}
val factory = MyFragmentFactory()
val scenario = launchFragmentInContainer<MyFragment>(
fragmentArgs, factory)
onView(withId(R.id.text)).check(matches(withText("Hello World!")))
}
}
@RunWith(AndroidJUnit4::class)
类MyTestSuite{
@测试乐趣testEventFragment(){
//“fragmentArgs”和“factory”参数是可选的。
val fragmentArgs=Bundle()。应用{
putInt(“selectedListItem”,0)
}
val factory=MyFragmentFactory()
val场景=启动FragmentIncontainer(
碎片(args,工厂)
onView(withId(R.id.text)).check(匹配项(withText(“Hello World!”))
}
}
更多信息请访问。也许您应该添加另一个标签,如“android测试”,以获得更多关注。除此之外,活动就像一个片段,只是它有onCreateView()。即使在应用程序处于活动状态时,碎片也可能从内存中释放出来。@TheOriginalAndroid添加:-)您可以看看MVP方法,这将使您更容易进行此类测试。@DmitryZaitsev您能解释得更详细一点吗?或者在android studio中提供一个示例或链接;它会自动创建一个测试包。但是它不允许我在该包中添加活动。有什么想法吗?要测试support-v4片段,请使用SupportFragmentTestUtil.startVisibleFragment()
。当片段取决于其活动且活动本身从寻呼机运行片段时,如何处理此问题?@SiavashA:这取决于您尝试测试的内容。如果只想测试片段,那么应该封装片段中的所有功能,并模拟/存根任何外部依赖项。或者,如果活动与片段的耦合过于紧密,也可以直接为其编写测试。如果您有更具体的问题,请随意写一个新问题。@AnswerDroid活动不需要添加到test
包中,您只能添加到java
包中,因为活动类名中没有test关键字。这看起来很有趣。。我会检查它并让您知道GetActivity().getSupportFragmentManager().beginTransaction()。替换(R.id.fragment,new LOGIN片段(),“LOGIN”).commit();我正在做这件事。然后我想测试LoginFragment。如何实现?你能提供更多细节吗?你想测试什么?这是您应该进行基本单元测试的地方-请看这里:了解更多关于测试内容的想法。如何通过使用fragment onView的instrumentation test(withId(R.id.txt_first_num_add))执行此操作;你试过这个@Levon Petrosyan吗。我接受了这个答案。
dependencies {
def fragment_version = "1.0.0"
// ...
debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'
}
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
// The "fragmentArgs" and "factory" arguments are optional.
val fragmentArgs = Bundle().apply {
putInt("selectedListItem", 0)
}
val factory = MyFragmentFactory()
val scenario = launchFragmentInContainer<MyFragment>(
fragmentArgs, factory)
onView(withId(R.id.text)).check(matches(withText("Hello World!")))
}
}