我是Android上的仪器测试的新手,并且正在https://github.com/googlesamples/android-testing/tree/master/runner/AndroidJunitRunnerSample的示例中学习使用AndroidJUnit4。
该代码完美无缺。
尝试在我的项目上实现相同的登录,但是,我得到以下异常:
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("filipebarretto@xxxxxxxxxx.com", "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
例外的原因是什么,以及如何解决它?
答案 0 :(得分:2)
我终于解决了这个问题。问题是我还计划运行单元测试,而不仅仅是仪器测试。
我有一个类UnitTestSuite,如:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({LoginUnitTest.class})
public class UnitTestSuite {}
LoginUnitTest没有实施任何测试。
出于某种原因,即使没有从AndroidTestSuite调用UnitTestSuite,它也试图运行并返回No runnable methods
,因为LoginUnitTest没有实现测试。我已经添加了单元测试,现在测试工作正常。