简体   繁体   English

Android 单元测试给出错误的结果

[英]Android Unit test are giving wrong result

When I run a unit test for a new created project then it shows me the wrong result (Says All Tests are passed) .当我为新创建的项目运行单元测试时,它向我显示了错误的结果(说所有测试都通过了)

If you see in the image below 4 is expected while 6+2 surely not 4 .如果您在下图中看到4 是预期的,而 6+2 肯定不是 4

在此处输入图片说明

Now when I run the same test from Gradle right menu.现在,当我从Gradle右侧菜单运行相同的测试时。 It shows me the correct result that test has failed.它向我显示了测试失败的正确结果。

在此处输入图片说明

Did I do anything wrong or is this an android studio bug?我做错了什么还是这是一个android studio错误?


Edit编辑

Run configuration image运行配置镜像

在此处输入图片说明

You need to add -ea option in the "VM options" box in your 3rd image.您需要在第三张图像的“VM 选项”框中添加-ea选项。 This is to enable assertion feature for the JVM.这是为了启用 JVM 的断言功能。 Without this -ea , JVM just ignore assertion statement.没有这个-ea ,JVM 只会忽略断言语句。 When you run the test with gradle, it used a default configuration so that's why your test was run correctly.当您使用 gradle 运行测试时,它使用了默认配置,这就是您的测试正确运行的原因。

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

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