简体   繁体   English

Android Instrumentation 测试永远停留在“运行测试”上 Android Studio

[英]Android Instrumentation Tests Stuck “Running Tests” Forever Android Studio

我将 Android Espresso 测试与最新的 Android Studio 2.1.2 一起使用,并且测试运行正常,但独立测试应用程序似乎没有返回结果以反映回 Android Studio,并且它显示永远运行测试

I realize this is an old question, but I just ran into this and didn't see any other search results that had the same problem.我意识到这是一个老问题,但我刚遇到这个问题,没有看到任何其他有同样问题的搜索结果。

In my case, this was caused by the code under test having a stack overflow exception, which it seems that the test runner failed to handle.就我而言,这是由于被测代码存在堆栈溢出异常而导致的,这似乎是测试运行器无法处理的。 I was only able to figure it out by looking at the "Android Monitor" and looking in the logcat.我只能通过查看“Android Monitor”并查看 logcat 来弄清楚。

So if you get to the point where AS just sits at "running test" forever, you might want to look in logcat for an exception that the test runner couldn't handle.因此,如果您到了 AS 永远处于“运行测试”状态的地步,您可能需要在 logcat 中查找测试运行器无法处理的异常。

You have to try removing testCoverageEnabled option from build.gradle file if it's there.如果存在,您必须尝试从 build.gradle 文件中删除 testCoverageEnabled 选项。 possible duplicate Gradle build tool long for android Tests可能重复的Gradle 构建工具很长,适用于 android 测试

In case this helps anyone.万一这对任何人都有帮助。 In my case, I was setting the idle state wrongly(false instead of true) after doing a background task, so espresso was stuck thinking that was idle.就我而言,我在执行后台任务后错误地设置了空闲状态(false 而不是 true),因此 espresso 一直认为这是空闲的。

You can add an exit test class like this and include this in your Test Suite or Executor in the last您可以添加这样的退出测试类,并在最后将其包含在您的测试套件或执行器中

import android.os.Looper;

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import java.io.IOException;

import static androidx.test.InstrumentationRegistry.getInstrumentation;

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExitTests {

    @Rule
    public ActivityTestRule<MainActivity> myActivityTestRule =
            new ActivityTestRule<>(MainActivity.class, false, true);

    public void init(){
        getInstrumentation().waitForIdleSync();
        if(Looper.myLooper() == null)
            Looper.prepare();
    }


    @Test
    public void Exit() throws InterruptedException, IOException {
        Runtime.getRuntime().exec(new String[] {"am", "force-stop", "com.package.name"});
    }
}

Here is the Test Suite这是测试套件

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ABC.class, ExitTests.class})
public class TestExecutionOrder {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM