我是新手,在Android上進行儀器測試,並從https://github.com/googlesamples/android-testing/tree/master/runner/AndroidJunitRunnerSample的示例中學習使用AndroidJUnit4。Android上沒有可運行的方法JUnit4儀器測試
該代碼完美地工作。
想實現我的項目相同的登錄名,不過,我得到以下異常:
Running tests
Test running started
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:791)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:754)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:341)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:238)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
我AndroidTestSuite樣子:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({InstrumentationTestSuite.class})
public class AndroidTestSuite {}
而且我InstrumentationTestSuite樣子:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({LoginInstrumentationTest.class})
public class InstrumentationTestSuite {}
我的LoginInstrumentationTest是
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginInstrumentationTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
LoginActivity.class);
@Test
public void attemptToLogin() {
login("[email protected]", "12345678");
}
private void login(String email, String password) {
// Enter user email
onView(withId(R.id.email)).perform(typeText(email),
closeSoftKeyboard());
// Enter user password
onView(withId(R.id.password)).perform(typeText(password),
closeSoftKeyboard());
// Click on the login button
onView(withId(R.id.email_sign_in_button)).perform(click());
// Testing
onView(withId(R.id.email)).check(matches(withText(email)));
}
}
從我搜索的內容來看,通常我得到的錯誤是由於我的方法之前丟失了@Test
。
我在我的代碼和我正在學習的代碼之間注意到的差異是gradle版本,我使用1.5.0並使用2.0.0-beta2。
我還發現我在測試過程中調用的Instrumentation.java有幾個「無法解析符號」錯誤,我無法找到原因,因爲我使用與示例代碼相同的依賴關係。
任何人都知道可能是No runnable methods
異常的原因以及如何解決它?
您究竟如何執行測試?從Android Studio內部? –
@DougStevenson我去了Run> Edit Configurations,並將新的Android Testa設置爲模塊應用程序中的所有模塊,將Specific Instrumentation runner設置爲android.support.test.runner.AndroidJUnitRunner。 測試實際上運行並打開我的手機上的應用程序,但總是失敗做例外。 – filipebarretto