【发布时间】:2014-04-14 13:06:09
【问题描述】:
我正在尝试为我的应用程序进行一些 Android 测试。 我有一个包含两个按钮的 Activity (A),每个按钮都导航到另一个 Activity (B),其意图中具有不同的额外(数据)。
我可以测试其中一个,但不能同时测试两者。因为当我执行第一个的点击时,我导航到下一个 Activity (B) 并且第二个按钮不再可见。
我的问题是:
1-是否可以在同一个活动中进行许多测试?
2- 有没有一种好的方法或最佳实践来创建许多用于测试的案例场景?
示例 ---> 就像我单击第一个按钮一样,我导航到 Activity (B),然后再次重新启动 Activity (A),然后单击第二个按钮?
还是谢谢。
这是 Activitytest 的代码:
public class MyActivityTest extends
ActivityInstrumentationTestCase2<MyActivity> {
/**
* first button.
*/
View aButton;
/**
* second button.
*/
View bButton;
/**
* Activity under test.
*/
MyActivity activity;
/**
* Create a new MyActivityTest.
*/
public MyActivityTest() {
super(MyActivity.class);
}
/*
* (non-Javadoc)
*
* @see android.test.ActivityInstrumentationTestCase2#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
activity = getActivity();
}
/**
* Tests the behavior when the user selects first button.
*/
@MediumTest
public void testClickFirstButton() {
// ensure a valid handle to the activity has been returned
assertNotNull(activity);
aButton = (View) activity
.findViewById(MyPackage.R.id.first_button);
assertNotNull(aButton);
activity.runOnUiThread(new Runnable() {
public void run() {
aButton.performClick();
}
});
// wait for the request to go through
getInstrumentation().waitForIdleSync();
}
/**
* ========================================> This test is not running
* Tests the behavior when the user selects second button.
*/
@MediumTest
public void testClickSecondButton() {
// ensure a valid handle to the activity has been returned
assertNotNull(activity);
bButton = (View) activity
.findViewById(MyPackage.R.id.second_button);
assertNotNull(bButton);
activity.runOnUiThread(new Runnable() {
public void run() {
bButton.performClick();
}
});
// wait for the request to go through
getInstrumentation().waitForIdleSync();
}
}
【问题讨论】:
标签: android unit-testing android-testing