Android 仪器测试永远卡"Running Tests" Android Studio



i使用最新的Android Studio 2.1.2使用Android Espresso测试,并且测试正常运行,但似乎并不像独立的测试应用程序返回结果,以反射回到Android Studio及其显示"永远运行的测试"

我意识到这是一个古老的问题,但是我只是遇到了这个问题,没有看到任何其他问题的搜索结果。

在我的情况下,这是由于测试的代码具有堆栈溢出异常而引起的,看来测试跑步者无法处理。我只能通过查看" Android监视器"并在Logcat中查看。

因此,如果您到达永远处于"运行测试"的地步,您可能想在logcat中查看测试跑步者无法处理的例外。

您必须尝试从build.gradle文件中删除testCoverageEnabled选项,如果它存在。可能的重复Gradle构建工具长期用于Android测试

如果这对任何人有帮助。就我而言,执行背景任务后,我错误地设置了空闲状态(false而不是true),因此浓缩咖啡被卡住了以为是空闲的。

您可以添加这样的退出测试类,并将其包含在您的测试套件或遗嘱执行程序中

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"});
    }
}

这是测试套件

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ABC.class, ExitTests.class})
public class TestExecutionOrder {
}

最新更新