尝试运行简单的 Android JUnit 测试.得到:“测试运行失败:没有测试结果";我错过了什么? [英] Trying to run trivial Android JUnit tests. Getting: "Test run failed: No test results" What am I missing?

查看:40
本文介绍了尝试运行简单的 Android JUnit 测试.得到:“测试运行失败:没有测试结果";我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过 JUnit,现在我正在尝试在 Android 项目上设置它.

我的测试项目相当复杂,包括一些 JNI,但目前我的测试项目完全是微不足道的.我在网上找到了很多关于如何制作测试项目的例子(看起来完全不同),但似乎无论我遵循哪个,我都会得到相同的结果.

这是我的 JUnit 项目代码:

<代码>包 com.mycompany.myproject.test;导入 android.test.AndroidTestCase;公共类 SimpleTestCaseExample 扩展 AndroidTestCase {公共无效 test_testOne() {失败(只是总是失败");}}

当我运行时,我在 Logcat 中看到以下内容:

<前>标准输出 INSTRUMENTATION_STATUS: numtests=2标准输出 INSTRUMENTATION_STATUS: test=test_testOne标准输出 INSTRUMENTATION_STATUS_CODE:0标准输出 INSTRUMENTATION_STATUS: id=InstrumentationTestRunner标准输出 INSTRUMENTATION_STATUS: current=2标准输出 INSTRUMENTATION_STATUS:class=com.mycompany.myproject.test.SimpleTestCaseExample标准输出 INSTRUMENTATION_STATUS: 流=标准输出 INSTRUMENTATION_STATUS: numtests=2标准输出 INSTRUMENTATION_STATUS: test=testAndroidTestCaseSetupProperly标准输出 INSTRUMENTATION_STATUS_CODE:1标准输出 INSTRUMENTATION_STATUS: id=InstrumentationTestRunner标准输出 INSTRUMENTATION_STATUS: current=2标准输出 INSTRUMENTATION_STATUS:class=com.mycompany.myproject.test.SimpleTestCaseExample标准输出 INSTRUMENTATION_STATUS: 流=.标准输出 INSTRUMENTATION_STATUS: numtests=2标准输出 INSTRUMENTATION_STATUS: test=testAndroidTestCaseSetupProperly标准输出 INSTRUMENTATION_STATUS_CODE:0标准输出 INSTRUMENTATION_RESULT:流=InstrumentationTestRunner=..的标准输出测试结果标准输出时间:0.07标准输出正常(2 个测试)标准输出 INSTRUMENTATION_CODE:-1

但是,我在控制台中得到以下信息:

<前>在设备模拟器上启动检测 android.test.InstrumentationTestRunner-5554收集测试信息测试运行失败:无测试结果

我尝试了各种不同的方法,弄乱了基本的 TestCase 类、TestSuite 类或各种其他选项.我试图只举一个最简单的例子,因为我真的还在努力学习它是如何工作的.

无论我尝试什么,我都会看到这个错误.

任何建议将不胜感激!

如果我遗漏了一些关键信息,请告诉我,我会更新.

解决方案

好的,我想通了.

而且几乎没有人会猜到问题是什么.我不确定是什么让我尝试了它.

我有一些 JNI 代码可以将错误消息打印到标准输出.该代码未在我的测试项目中运行,但我使用相同的模拟器.出于这个原因,我有一个/data/local.prop 将 stdout 重定向到 logcat.

事实证明,测试工具希望 JUnit 测试的输出出现在标准输出上.当stdout的logcat重定向打开时,stdout上什么都没有,测试系统也没有得到输出,所以运行失败.

我删除了 stdout 到 logcat 的 local.prop 重定向,并重新启动了模拟器,现在它可以工作了.

我从未想过测试系统依赖于读取标准输出本身.

I have never used JUnit before, and now I'm trying to set it up on an Android project.

My project under test is fairly complex, including some JNI, but my test project, at the moment, is completely trivial. I have found many examples (that look totally different) online of how to make a test project, but it seems that no matter which one I follow, I get the same results.

Here's my JUnit project code:


package com.mycompany.myproject.test;

import android.test.AndroidTestCase;

public class SimpleTestCaseExample extends AndroidTestCase {
    public void test_testOne() {
        fail("Just Always Fail");
    }
}

When I run, I see the following in Logcat:

stdout    INSTRUMENTATION_STATUS: numtests=2
stdout    INSTRUMENTATION_STATUS: test=test_testOne
stdout    INSTRUMENTATION_STATUS_CODE: 0
stdout    INSTRUMENTATION_STATUS: id=InstrumentationTestRunner
stdout    INSTRUMENTATION_STATUS: current=2
stdout    INSTRUMENTATION_STATUS: class=com.mycompany.myproject.test.SimpleTestCaseExample
stdout    INSTRUMENTATION_STATUS: stream=
stdout    INSTRUMENTATION_STATUS: numtests=2
stdout    INSTRUMENTATION_STATUS: test=testAndroidTestCaseSetupProperly
stdout    INSTRUMENTATION_STATUS_CODE: 1
stdout    INSTRUMENTATION_STATUS: id=InstrumentationTestRunner
stdout    INSTRUMENTATION_STATUS: current=2
stdout    INSTRUMENTATION_STATUS: class=com.mycompany.myproject.test.SimpleTestCaseExample
stdout    INSTRUMENTATION_STATUS: stream=.
stdout    INSTRUMENTATION_STATUS: numtests=2
stdout    INSTRUMENTATION_STATUS: test=testAndroidTestCaseSetupProperly
stdout    INSTRUMENTATION_STATUS_CODE: 0
stdout    INSTRUMENTATION_RESULT: stream=
stdout    Test results for InstrumentationTestRunner=..
stdout    Time: 0.07
stdout    OK (2 tests)
stdout    INSTRUMENTATION_CODE: -1

But, I get the following in the Console:

Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
Collecting test information
Test run failed: No test results

I have tried a variety of different things, messing with the basic TestCase class, or the TestSuite class, or a variety of other options. I tried to just go for the most trivial example because I'm really still trying to learn how this works.

Whatever I try, I see this error.

Any suggestions would be appreciated!

If I'm missing some critical information, please let me know and I'll update.

解决方案

OK, I figured it out.

And there is very little chance that anybody would have guessed what the problem is. I'm not sure what made me try it.

I have some JNI code that prints error messages to stdout. That code is not running in my test project, but I use the same emulator. For that reason, I had a /data/local.prop that redirects stdout to logcat.

It turns out that the test tools expect the output from the JUnit tests to appear on stdout. When the logcat redirect of stdout is on, nothing ends up on stdout, and the test system doesn't get the output, and so it fails to run.

I removed my local.prop redirect of stdout to logcat, and rebooted the emulator, and now it works.

It never occurred to me that the test system relied upon reading stdout itself.

这篇关于尝试运行简单的 Android JUnit 测试.得到:“测试运行失败:没有测试结果";我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆