美文网首页
[Android] 使用Android Test 进行测试却无法

[Android] 使用Android Test 进行测试却无法

作者: storyteller_F | 来源:发表于2020-03-20 21:07 被阅读0次

[Android] 使用Android Test 进行测试却无法打印Log怎么办

问题

在Android Test中进行测试还是很方便的,但有时也会不太方便。比如我有很多数据,检测一个函数是否工作正常,放到一个数组中遍历测试的,当出现问题时,我不知道是哪个数据处理出现了问题。

如果你想要使用Log的功能,发现毫无用处,找不到你的日志在哪里。
这个日志应该不是由你当前的软件打印的,而是包名为your_original_package_name.test 的软件打印的,可能跟这个有关吧。反正就是Android Studio 没有展示日志。

思考

虽然日志无法打印,但是出现错误时还是会有提示的,长得好像Exception(其实不是,后面会说)。

既然是个“异常”,那就捕获它吧。然后换成自己的异常,在这个异常中添加一些调试信息,甚至自己做一个异常类不就好了。

    try {
        //...
    } catch (Exception e) {
        //...
    }

但是这样其实不能够捕获异常,仔细看那个错误,是java.lang.AssertionError,是Error 而不是Exception

谷歌的文档写的是

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

Error 是一个Throwable 的子类,标识一个严重错误,一个resonable application 不应该尝试去捕获它。

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

上面说一个应用不应该捕获Error 但是我不管,我就要这么干。反正运行这段代码的也不是一个resonable application

try {
    assertFalse(...);
} catch (AssertionError ignored) {
    throw new AssertionError("hello world");
}

这样就可以正常调试了。

相关文章

  • [Android] 使用Android Test 进行测试却无法

    [Android] 使用Android Test 进行测试却无法打印Log怎么办 问题 在Android Test...

  • Android Unit Test学习

    Android Unit Test学习 @(单元测试)[Android|Markdown] 单元测试 这几天接触了...

  • Android Instrumentation测试框架使用示例

    Instrumentation 是Android studio自带的测试框架,可使用它进行Android应用的单元...

  • UnitTest mock

    使用mockito做测试时,有些Android类无法mock,比如android.util.Log,可以在buil...

  • Android Studio单元测试

    今天简单介绍下Android Studio 使用代码进行单元测试。 Android Studio默认是支持JUni...

  • flutter学习笔记(一)

    目录结构 ios ios代码所在 android android代码所在 lib 主要的代码编写地 test 测试...

  • 单元测试

    使用Mockito 1.gradle配置 2.在需要测试的类中,点击右键->Go to->Test,android...

  • Android 单元测试

    一、浏览 Android 单元测试 官网 Android 的单元测试有两种方式:JUnit test(本地虚拟机测...

  • Android APP测试流程

    Android APP测试流程 一、 Monkey测试(冒烟测试) 使用monkey测试工具进行如下操作: APP...

  • Android Test测试

    在实际的开发中几乎访问网络已经成为一个app的标配,那么每次写完一个网络请求都要重新打包在模拟器或者...

网友评论

      本文标题:[Android] 使用Android Test 进行测试却无法

      本文链接:https://www.haomeiwen.com/subject/ymmsyhtx.html