如何在Android J Unit Test上循环测试运行?

时间:2014-03-05 09:14:15

标签: java android eclipse junit robotium

我在Eclipse上为我的应用程序创建了一个测试。我正在使用Robotium 5.0.1。我正在进行的测试是......启动MainActivity.class>点击menuitem(addperson)>这会自动将其带到添加屏幕并填充编辑文本并按下“保存”,然后关闭应用程序;测试顺利通过。

我想要做的是多次运行测试而无需重新运行测试,例如测试应用程序以添加100个人。

这是我的测试类:

package com.androidbegin.databasetutorial.test;

import com.androidbegin.databasetutorial.MainActivity;
import com.robotium.solo.Solo;
import com.androidbegin.databasetutorial.R;

import android.test.ActivityInstrumentationTestCase2;

public class InitialButtonTest extends
    ActivityInstrumentationTestCase2<MainActivity> {

private Solo solo;
public InitialButtonTest() {
    super(MainActivity.class);

}

@Override
public void setUp() throws Exception {
    //setUp() is run before a test case is started. 
    //This is where the solo object is created.
    solo = new Solo(getInstrumentation(), getActivity());
}

public void testAddRemove() throws Exception
{
    solo.assertCurrentActivity("Check on first activity", MainActivity.class);
    solo.clickOnActionBarItem(R.id.action_addperson);
    solo.enterText(0, "id number");
    solo.enterText(1, "Name");
    solo.enterText(2, "Last Name");
    solo.enterText(3, "Address");
            solo.enterText(4, "Contact number");
            solo.enterText(5, "Code");
            solo.enterText(6, "Date");
    solo.clickOnMenuItem("Save Note");


}
}

您能帮助我或引导我使用我可以在此代码中实现的循环代码。

几乎没有要求(虽然不是必需的): solo.enterText(0)&lt;&lt;如果可以添加最多7位数的随机数。

感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:0)

只需将其放入for循环

public void testAddRemove() throws Exception
{
    //it runs it 1000 times
    addRemove(1000);
}

public void addRemove(int NUMBER_OF_TIMES) throws Exception
{
for(int i=0; i<NUMBER_OF_TIMES; i++){
    solo.assertCurrentActivity("Check on first activity", MainActivity.class);
    solo.clickOnActionBarItem(R.id.action_addperson);
    solo.enterText(0, "id number");
    solo.enterText(1, "Name");
    solo.enterText(2, "Last Name");
    solo.enterText(3, "Address");
    solo.enterText(4, "Contact number");
    solo.enterText(5, "Code");
    solo.enterText(6, "Date");
    solo.clickOnMenuItem("Save Note");
}
}