Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio 3 does not run tests in Spock

I updated Android Studio to version 3 and since then all my spock tests, when in a java module, do not run when trying to run them from inside the application (right click on groovy folder -> Run 'Tests in groovy'). I get a:

Class not found: "package.name.classname"Empty test suite."

Same if I try to run a single test.

If I run the test task from the gradle panel I get this: error. Cause: unknown.


On the other hand:

  • Any spock tests in android modules run fine.
  • All my java tests in all my modules run fine.
  • All my tests (spock and java) run fine when running them from outside AS using gradle (gradlew clean test).

My setup:

  • gradle v4.1
  • android gradle plugin v3.0.0
  • java version compatibility v1.8
  • in my java modules i use the gradle groovy plugin
  • in my android modules i use the groovy android gradle plugin

A few things I tried after searching in both google and here:

  • changing the android gradle plugin back to v2.3.3 and gradle to v3.3
  • trying to copy all groovy classes to build/classes/java/test
like image 547
le0nidas Avatar asked Oct 26 '17 13:10

le0nidas


People also ask

How to unit test in android studio?

You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.

What is test folder in Android Studio?

By default, Unit tests are written in src/test/java/ folder and Instrumentation tests are written in src/androidTest/java/ folder. Android studio provides Run context menu for the test classes to run the test written in the selected test classes.


1 Answers

So this is more of a workaround than an actual solution but it should give you your debugger back which is probably 90% of the value anyway:

You can run your test suite like:

./gradlew <module>:test --debug-jvm

And the jvm running your tests will suspend until a debugger attaches.

From Android Studio bring up the action chooser by pressing ctrl + shift + a (on linux anyway, check the equivalent for your OS) and select:

Attach to local process...

Once Android Studio attaches the tests will begin running.

The --debug-jvm flag can be used together with --tests to debug an individual test:

./gradlew <module>:test --tests fully.qualified.test.Test --debug-jvm
like image 84
Troy Patrick Avatar answered Sep 20 '22 18:09

Troy Patrick