我一直在关注Android测试的书并实施了这个样本:
public class TemperatureConverterActivityTests extends
ActivityInstrumentationTestCase2<TemperatureConverterActivity> {
private TemperatureConverterActivity mActivity;
private EditText mCelsius;
private EditText mFarenheit;
public TemperatureConverterActivityTests() {
this("TemperatureConverterActivityTests");
}
public TemperatureConverterActivityTests(String name) {
super(TemperatureConverterActivity.class);
setName(name);
}
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
mCelsius = (EditText) mActivity
.findViewById(org.mighter.tc.R.id.celsius);
mFarenheit = (EditText) mActivity
.findViewById(org.mighter.tc.R.id.farenheit);
}
protected void tearDown() throws Exception {
}
public final void testPreconditions() {
assertNotNull(mActivity);
}
public final void testHasInputFields() {
assertNotNull(mCelsius);
assertNotNull(mFarenheit);
}
}
然而,测试没有完成。
我在控制台中获得了什么:
Android Launch!
adb is running normally.
Performing android.test.InstrumentationTestRunner JUnit launch
Automatic Target Mode: Several compatible targets. Please select a target device.
Uploading TemperatureConverterTest.apk onto device '80A358103041548074'
Installing TemperatureConverterTest.apk...
Success!
Project dependency found, installing: TemperatureConverter
Application already deployed. No need to reinstall.
Launching instrumentation android.test.InstrumentationTestRunner on device 80A358103041548074
Collecting test information
Test run failed: Instrumentation run failed due to 'Process crashed.'
Test run finished
Sending test information to Eclipse
Running tests...
然后它挂起,这是JUnit视图:
LogCat的唯一信息:
05-28 17:23:54.952: W/ActivityManager(224): Crash of app org.mighter.tc running instrumentation ComponentInfo{org.mighter.tc.test/android.test.InstrumentationTestRunner}
05-28 17:23:55.042: I/TestRunner(7174): <!>android.test.TestPrinter 50<!> started: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.062: I/TestRunner(7174): <!>android.test.TestPrinter 56<!> finished: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.062: I/TestRunner(7174): <!>android.test.TestPrinter 77<!> passed: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.072: I/TestRunner(7174): <!>android.test.TestPrinter 50<!> started: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.082: I/TestRunner(7174): <!>android.test.TestPrinter 56<!> finished: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.082: I/TestRunner(7174): <!>android.test.TestPrinter 77<!> passed: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.652: I/TestRunner(7194): <!>android.test.TestPrinter 50<!> started: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 56<!> finished: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 77<!> passed: testHasInputFields(org.mighter.tc.test.TemperatureConverterActivityTests)
05-28 17:23:55.853: I/TestRunner(7194): <!>android.test.TestPrinter 50<!> started: testPreconditions(org.mighter.tc.test.TemperatureConverterActivityTests)
尝试调试,但它永远不会到达testPreconditions()。 感谢。
答案 0 :(得分:2)
只需要在tearDown()中调用super;