robotium测试自动例子 androidCalculator

来源:互联网 发布:欧盟 知乎 编辑:程序博客网 时间:2024/06/05 06:38
package com.calculator.test;
import com.calculator.Main;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Smoke;


public class CallculatreTest extends ActivityInstrumentationTestCase2<Main> {

private Solo solo;

public CallculatreTest() {
super("com.calculator",Main.class);
}

public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
//该方法用来初始化solo,绑定对应的Activity。
}

    //////////////////测试用例


@Smoke
public void testactivity() throws Exception { 
assertTrue(solo.searchText("0.0"));
   String appname=solo.getCurrentActivity().getLocalClassName();
String expencted= "Main";
assertEquals("类检查正确",appname,expencted);

}
public void testfistnum1() throws Exception {   
  solo.enterText(0,"123");
  boolean expencted= true;
  boolean acture=solo.searchText("123");
  assertEquals("整型检查正确",expencted,acture);
}

public void testfistnum2() throws Exception {   
solo.enterText(0,"asdf");
boolean expencted= true;
boolean acture=solo.searchText("asdf");
assertEquals("字符串检查正确",expencted,acture);
}

public void testsecondnum1() throws Exception {   
solo.enterText(1,"123");
boolean expencted= true;
boolean acture=solo.searchText("123");
assertEquals("整型检查正确",expencted,acture);

}

public void testsecondnum2() throws Exception {   
   solo.enterText(1,"asdf");
   boolean expencted= true;
   boolean acture=solo.searchText("asdf");
   System.out.println(acture);
assertEquals("字符串检查正确",expencted,acture);
}

public void testresult() throws Exception {   
   solo.enterText(1,"123");
   solo.enterText(0, "3");
   solo.clickOnButton(0);
assertTrue(solo.searchText("369.0"));
}



@Override
public void tearDown() throws Exception {
try {
//Robotium will finish all the activities that have been opened
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}

}


dos下执行的命令是:adb shell am instrument -w com.example.android.fmcg.test/android.test.InstrumentationTestRunner&

其中 com.example.android.fmcg.test是测试的包名