尝试运行简单的 Android JUnit 测试。获取:“测试运行失败:无测试结果”我缺少什么?

发布于 2024-09-14 03:17:05 字数 1922 浏览 3 评论 0原文

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

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

这是我的 JUnit 项目代码:


package com.mycompany.myproject.test;

import android.test.AndroidTestCase;

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

当我运行时,我在 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

但是,我在控制台中看到以下内容:

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

我尝试了各种不同的操作,弄乱了基本 TestCase 类、TestSuite 类或各种其他选择。我试图只举一个最简单的例子,因为我仍然在尝试了解它是如何工作的。

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

任何建议将不胜感激!

如果我遗漏了一些重要信息,请告诉我,我会更新。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-09-21 03:17:05

好吧,我明白了。

而且几乎没有人能猜到问题所在。我不知道是什么让我尝试的。

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

事实证明,测试工具期望 JUnit 测试的输出出现在 stdout 上。当 stdout 的 logcat 重定向打开时,stdout 上不会有任何结果,并且测试系统无法获取输出,因此无法运行。

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

我从来没有想到测试系统依赖于读取标准输出本身。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文