单元测试是Android碎片 [英] Unit Test an Android Fragment

查看:216
本文介绍了单元测试是Android碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想单元测试一个Android片段类。

I want to unit test an Android Fragment class.

我可以设置使用AndroidTestCase测试或做我需要使用ApplicationTestCase?

Can I set up a test using AndroidTestCase or do I need to use ApplicationTestCase?

有没有这两个的TestCase如何使用任何有用的例子吗?在开发者网站的测试的例子是最小的,只是似乎把重点放在测试活动。

Are there any useful examples of how these two TestCases can be used? The testing examples on the developer site are minimal and just seem to focus on testing Activities.

所有我发现其他地方的情况发生在AndroidTestCase类扩展的例子,但随后所有这一切测试的两数相加在一起,或者如果上下文中使用,它只是做了简单的GET和测试自己是不是空!

All I've found elsewhere are examples where the AndroidTestCase class is extended but then all that's tested is adding two numbers together or if the Context is used, it just does a simple get and tests that something is not null!

据我了解,一个片段有一个活动内生活。所以,我能创建一个模拟活动,或获取应用程序或上下文提供的活动中,我可以测试我的片段?

As I understand it, a Fragment has to live within an Activity. So could I create a mock Activity, or get the Application or Context to provide an Activity within which I can test my Fragment?

我是否需要创建自己的活动,然后使用ActivityUnitTestCase?

Do I need to create my own Activity and then use ActivityUnitTestCase?

感谢您的帮助。

崔佛

推荐答案

假设你有一个名为MyFragmentActivity一FragmentActivity类,其中一个公共片段类叫MyFragment使用FragmentTransaction添加。只要创建,在您的测试项目扩展ActivityInstrumentationTestCase2一个JUnit测试案例类。然后,只需调用getActivity()和访问MyFragment对象和它的公众成员编写测试用例。

Suppose you have a FragmentActivity class called 'MyFragmentActivity' in which a public Fragment class called 'MyFragment' is added using FragmentTransaction. Just create a 'JUnit Test Case' class that extends ActivityInstrumentationTestCase2 in your test project. Then simply call getActivity() and access MyFragment object and its public members for writing test cases.

请参考下面的code片断:

Refer the code snippet below:

// TARGET CLASS
public class MyFragmentActivity extends FragmentActivity {
    public MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        myFragment = new MyFragment();
        fragmentTransaction.add(R.id.mainFragmentContainer, myFragment);
        fragmentTransaction.commit();
    }
}

// TEST CLASS
public class MyFragmentActivityTest extends android.test.ActivityInstrumentationTestCase2<MyFragmentActivity> {
    MyFragmentActivity myFragmentActivity;
    MyFragment myFragment;

    public MyFragmentActivityTest() {
        super(MyFragmentActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        myFragmentActivity = (MyFragmentActivity) getActivity();
        myFragment = myFragmentActivity.myFragment;
    }

    public void testPreConditions() {
        assertNotNull(myFragmentActivity);
        assertNotNull(myFragment);
    }

    public void testAnythingFromMyFragment() {
        // access any public members of myFragment to test
    }
}

我希望这有助于。接受我的回答,如果你发现这很有用。谢谢你。

I hope this helps. Accept my answer if you find this useful. Thanks.

这篇关于单元测试是Android碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆