通过使用检测类的相同测试应用程序启动具有不同意图数据的活动

时间:2011-09-06 09:43:04

标签: android unit-testing

我使用instrumentation类实现了一个测试应用程序。我想测试我的应用程序的不同意图数据。

有没有办法在测试中多次启动我们的应用

2 个答案:

答案 0 :(得分:2)

这取决于您在仪器类中获得的测试方法数量

public void testFirstTime() {
  Intent intent = new Intent(getInstrumentation().getTargetContext(), MyActivity.class);
  Foo foo = new Foo();
  intent.putExtra("param", foo);
  setActivityIntent(intent);
  MyActivity myActivity = getActivity();
  assertNotNull(myActivity);
  // do some assert
}

public void testSecondTime() {
  Intent intent = new Intent(getInstrumentation().getTargetContext(), MyActivity.class);
  Bar bar = new Bar();
  intent.putExtra("param", bar);
  setActivityIntent(intent);
  MyActivity myActivity = getActivity();
  assertNotNull(myActivity);
  // do some other assert
}

答案 1 :(得分:0)

找到解决方案,不是最美的解决方案。 在setUp中我做了:

 protected void setUp() throws Exception {

    setActivityInitialTouchMode(false);



    if(stage == 0){
        in1 = new Intent();
        in1.putExtra(Defintiens.EXTRA_1,  CopyUSerDetailsServiceMock.getMock1());
        in1.putExtra(Defintiens.EXTRA_2, UserProtfolioMock.getMock1());
        setActivityIntent(in1);
    }else if (stage == 1){
        in2 = new Intent();
        in2.putExtra(Defintiens.EXTRA_1, getMock1());
        in2.putExtra(Defintiens.EXTRA_2, getMock1());
        setActivityIntent(in2);
    }else if (stage == 3){
        in3 = new Intent();
        in3.putExtra(Defintiens.EXTRA_1, getMock1());
        in3.putExtra(Defintiens.EXTRA_2, getMock1());
        setActivityIntent(in3);
    }else if (stage == 4){
        in4 = new Intent();
        in4.putExtra(Defintiens.EXTRA_1, getMock1());
        in4.putExtra(Defintiens.EXTRA_2, getMock1());

    }

    mActivity = getActivity();


    super.setUp();
}


  @Override
    protected void tearDown() throws Exception {
        mActivity.finish();
        super.tearDown();
    }

  private static int stage = 0;
    public void testInjectExtra2(){
        stage = 2;
         //In each test you should set the stage to tour testing 
         //...... Run your test
}

现在,在每个测试中,活动将以我想要的意图

重新启动