1

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.

enter image description here

Now when I run the same test from Gradle right menu. It shows me the correct result that test has failed.

enter image description here

Did I do anything wrong or is this an android studio bug?


Edit

Run configuration image

enter image description here

Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96

1 Answers1

1

You need to add -ea option in the "VM options" box in your 3rd image. This is to enable assertion feature for the JVM. Without this -ea, JVM just ignore assertion statement. When you run the test with gradle, it used a default configuration so that's why your test was run correctly.

Lam
  • 21
  • 1
  • 3
  • on the first image, in the test result navigator, just right above "All test passed" if you click on the OK green button, it shows which tests did run? Ignore the -ea, it does not matter in your case :) – Lam Jan 10 '17 at 03:48
  • first time running test after opening Android studio says no test found – N J Jan 10 '17 at 03:54
  • If there is no ExampleTestClass.class file was generated under build/xxx/classes folder (do a search for all file in the build folder). If it is the case, your "test" folder somehow was not recognised as a "source" folder. Take a look at the app.iml file in your project ( ). If you are lucky then delete the file and open/import the project again will work. Or you can find someway to include "test" folder as a source folder. – Lam Jan 10 '17 at 06:20
  • not working its preserving the result of previous run Gradle task. – N J Jan 10 '17 at 06:38
  • delete the class file, or change some code or do a clean build to force it to build again. Dont run gradle test, now do you see the class file generated? – Lam Jan 10 '17 at 07:16
  • Now it's saying `Class not found: "com.xxx.xxx.network.UrlConstantTest"Empty test suite.` – N J Jan 10 '17 at 07:48
  • found this similar to your issue, hope it help : http://stackoverflow.com/questions/38056901/class-not-found-empty-test-suite-when-running-unit-tests-in-android-studio – Lam Jan 10 '17 at 09:35