开发者

launching an activty with different intent data by a same test app which uses instrumentation classes

开发者 https://www.devze.com 2023-04-03 10:31 出处:网络
I have implemented an test application using the instrumentation class. I want to to test my app for different intent data.

I have implemented an test application using the instrumentation class. I want to to test my app for different intent data.

Is there a way to launch our app under test, multiple number of tim开发者_Python百科es


It depends on how many test method you got in you instrumentation class

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
}


Found a solution , isn't the most beautiful one. In the setUp i did :

 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
}

Now in each test the activity will restart with the intent extra i want

0

精彩评论

暂无评论...
验证码 换一张
取 消