单独的测试通过,但均失败

时间:2019-04-29 11:20:13

标签: android-espresso ui-testing mockserver

这是我的两个意式浓缩咖啡测试:

@RunWith(AndroidJUnit4::class)
class TradersActivityNetworkTest {

    private val context = InstrumentationRegistry.getInstrumentation().getContext()
    private lateinit var mockServer: MockWebServer

    @Rule
    @JvmField
    var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
    }

    @Test
    fun network_clientError_showToast() {
        mockServer.enqueue(MockResponse()
                .setResponseCode(400))

        tradersIntentTestRule.launchActivity(Intent())
        onView(withText(R.string.client_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))

    }

    @Test
    fun network_clientError_traderNotFound_showToast() {
        mockServer.enqueue(MockResponse()
                .setResponseCode(404)
                .setBody(FileUtil.getStringFromFile(context, "trader_404_not_found.json")))

        tradersIntentTestRule.launchActivity(Intent())

        onView(withText(R.string.trader_not_found)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
    }


@After
@Throws
fun tearDown() {
    mockServer.shutdown()
}

如果我仅通过Android Studio开始network_clientError_showToast测试,则测试成功。 enter image description here

如果我仅开始进行network_clientError_traderNotFound_showToast测试,那也是成功通过。

enter image description here

很好。

但是如果我像在类TradersActivityNetworkTest中启动所有测试一样启动这两个测试,则:

enter image description here

我得到错误。测试network_clientError_showToast失败:

then test `network_clientError_showToast` is fail with message:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131623975>[client_error] value: Client Error

View Hierarchy:
+>LinearLayout{id=-1, visibility=VISIBLE, width=556, height=149, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,192)(wrapxwrap) gr=#51 ty=2005 fl=#1000098 fmt=-3 wanim=0x1030004}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->AppCompatTextView{id=16908299, res-name=message, visibility=VISIBLE, width=406, height=83, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@9d762b2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=75.0, y=33.0, text=Trader not found, input-type=0, ime-target=false, has-links=false}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at com.myproject.activity.TradersActivityNetworkTest.network_clientError_showToast(TradersActivityNetworkTest.kt:63)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

为什么单独的测试通过了,但都失败了?

0 个答案:

没有答案