我是Android开发应用程序的新手,并尝试使用JUnit测试框架来测试下面的代码,使用ActivityInstrumentationTestCase2类,并在Android Docs中的TestingFundamentals上进行讲座。但是当Test运行时,我没有得到任何关于测试的信息(Eclipse中JUnit接口中的红色,绿色,蓝色条)(事实上,我相信测试没有发生,即使控制台说是的)。
以下是JUnit代码和清单文件:
package gml.android.mixdroid.test;
import java.util.ArrayList;
import gml.android.mixdroid.SeekbarActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarActivityTest extends ActivityInstrumentationTestCase2<SeekbarActivity> {
private SeekbarActivity mActivity;
private ArrayList<SeekBar> mSeekBars;
private ArrayList<TextView> mProgress;
private ArrayList<TextView> mTracks;
public SeekBarActivityTest(String pkg, Class<SeekbarActivity> activityClass) {
super("gml.android.mixdroid", SeekbarActivity.class);
}
public void setUp() throws Exception{
super.setUp();
mActivity = getActivity();
mSeekBars = mActivity.getSeekBars();
mProgress = mActivity.getProgressTextViews();
mTracks = mActivity.getTrackingTextViews();
}
public void testPreconditions(){
assertNotNull(mSeekBars);
assertNotNull(mProgress);
assertNotNull(mTracks);
for(SeekBar s : mSeekBars){
assertNotNull(s);
}
for(TextView p : mProgress){
assertNotNull(p);
}
for(TextView t : mTracks){
assertNotNull(t);
}
}
public void testInitSeekBars() {
assertEquals(SeekbarActivity.NUMBER_OF_SEEK_BARS, mSeekBars.size());
}
public void testInitTextViews() {
assertEquals(SeekbarActivity.NUMBER_OF_SEEK_BARS, mTracks.size());
assertEquals(SeekbarActivity.NUMBER_OF_SEEK_BARS, mProgress.size());
}
}
清单(活动):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gml.android.mixdroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SeekbarActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
Manifest(JUnit):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="gml.android.mixdroid.test">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="7" />
<instrumentation
android:targetPackage="gml.android.mixdroid.test"
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for MixDroid App"/>
<activity
android:name="gml.android.SeekbarActivity"/>
</manifest>
在Logcat中,我收到了:
[2011-04-03 03:51:48 - SeekbarTest] Project dependency found, installing: SeekbarActivity
[2011-04-03 03:51:50 - SeekbarActivity] Application already deployed. No need to reinstall.
[2011-04-03 03:51:50 - SeekbarTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-04-03 03:51:53 - SeekbarTest] Collecting test information
[2011-04-03 03:51:57 - SeekbarTest] Test run failed: Test run incomplete. Expected 1 tests, received 0
和JUnit View,没有信息测试
答案 0 :(得分:1)
使用此构造函数:
public SeekBarActivityTest() {
super("gml.android.mixdroid", SeekbarActivity.class);
}
答案 1 :(得分:0)