Launch junit/robolectric tests (gradle) - android

I have my unit/Robolectic tests located in
/app/src/test/java
This is made to distinguish android instrumentation tests located in /app/src/androidTest/java
How can I only run these unit tests which don't need whole project to be built (which is time consuming) from command line?
Is there any gradlew/java command?
I want to obtain the same result as if running from Android Studio junit tests from specific dir.

Sure. I assume you don't have any flavour:
./gradlew testDebug -Dtest.single=<your test class>

Related

How to exlude tests when generating coverage reports using Jacoco on Android with gradle

I want to run a single Gradle command which generates a coverage report as well as runs a specific set of tests, not every test in the package. In this instance, I want to generate the report on my instrumentation tests and not on my UI tests.
I have set up Jacoco so that I can run the Gradle command
./gradlew create<package-name>DebugAndroidTestCoverageReport to run all of my tests in the androidTest folder.
I've tried ./gradlew create<package-name>DebugAndroidTestCoverageReport --tests "com.myproject.test.*" to only run the tests located in the test package but this returns the error Unknown command-line option '--tests'.
Using the Gradle command test --tests "com.myproject.test.*" Works fine and runs the specified tests, but does not generate the coverage report.
Is there a way that I can have test filtering while still generating the coverage report?
After some searching I stumbled upon this question Android, Gradle. How start specific instrumented test method? which gave me the answer.
In order to filter out test cases by package you can use
./gradlew create<package-name>DebugAndroidTestCoverageReport -Pandroid.testInstrumentationRunnerArguments.package=com.example.test
This command will generate the coverage report only on the specified tests.

How to run Android test for all flavors in Android Studio

I need to run Android test for all flavors in Android Studio.
Having a test (ApplicationTest instrumentation) and Android Studio allows me to run it for selected flavor.
I have a lot of flavors (250) so I need to somehow say it to run for every flavor or something like that, and leave it to run one after other.
You can run the connectedAndroidTest gradle command - either from the command line or in the right hand side gradle menu - it should be under 'verification' tab.

Android Local Unit test with coverage report from command line

I am working on a new project for Android. Currently, I am using Android studio as IDE. I need to run Unit test and System (CI) test flows which can be run on both Local machine (JVM) and Emulator/Real Device for instrumentation.
Note that I am running all unit tests via command line.
For get the code coverage of the Emulator/Real Device I am using Jacoco.
and running the following command: gradlew createDebugCoverageReport
However, I can't find any way to run the Local machine unit test with coverage report from command line.
The only way is to run it from the android studio by selecting "Run XXX with Code Coverage":
Can you please advise if it is possible to run local unit test from command line with coverage. And get the report as an html file?
Thanks,
Zachi
If I understood correctly, you are trying to run the tests with coverage ability of the IntelliJ-based Android studio.
It basicly can be done using a Command Line Tool of the IntelliJ.
You can read more about it here, but it generally allows you to accomplish everything that can be done from GUI via the command line:
IntelliJ creating command line tools
For more general info regarding the coverage of the IntelliJ tools you can read here:
IntelliJ Code coverage
Hope it helps, good luck.
At this moment there is no default task to create reporting for Junit (local) tests in the gradle Android tools.
However, it's easy to create those.
Just follow the instructions to integrate the custom jacoco.gradle file here:
https://gist.github.com/mrsasha/384a19f97cdeba5b5c2ea55f930fccd4
You will then have tasks like these: test[Flavor]UnitTestCoverage
So to generate the reports, you just have to:
$ ./gradlew test[Flavor]UnitTestCoverage
The report can be generated using the android studio, after you run the test with coverage the results window appears, click the button bordered with red
Check this image:
By running ./gradlew tasks in the terminal if you are using the gradle wrapper or gradle tasks you will have a list of the available verification tasks (see the screenshot below):
You can refer to this link for more thorough test command lines.
You can use testDebugUnitTest, run it as ./gradlew <your_module_name>:testDebugUnitTest and it will run tests related to this particular module + it will generate an html report under your_module/build/reports/tests/testDebugUnitTest folder, with the coverage.

Running multiple test with connectedCheck using Espresso

I am using Espresso. I have multiple tests written, all in different classes but the same project. When I run I remove all but one class and try running, it runs fine. However, when I have them all in my project and try running the second one always fails.
All of my test are pretty basic right now. They just have setUp() and one testMethod().
Can someone tell me what I am doing wrong?
NOTE: I run via terminal with command gradle appName:cC
I found my answer. I need to add the Spoon Gradle Plugin
That's one option, but you don't have to, you can still use gradle command connectedCheck
Find out how to run the tests (including running tests for a specific module and on multiple devices) here at this Google example:
https://github.com/googlesamples/android-testing-templates/tree/master/AndroidTestingBlueprint

Instrumentation error running RoboElectric 2 tests in IntelliJ

I have a simple Android project that makes use of RoboElectric 2 and Maven.
I am able to run my tests fine using:
mvn clean test
but the tests fail to run inside IntelliJ IDEA . I get this strange error when attempting to run the tests from IntelliJ IDEA
Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{com.example/android.test.InstrumentationTestRunner}
Empty test suite.
I get no other errors in the IDE and everything compiles and deploys to device correctly from within IntelliJ.
"Unable to find instrumentation" suggests you try to run this as Android test - is it the case? It should be run as JUnit test (usage of RoboElectric suggests you've written standard unit tests)

Categories

Resources