connectedAndroidTest task runs all test even when class is specified - android

I m trying to run instrumented unit tests using connectedAndroidTest. At first I jus wanted run to particular class using cAT. So I tried below command
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.company.test.data.model.TestClassName
but it runs all the tests including the robotium and espresso tests which were written. I gone through lots of StackOverFlow posts, in all posts the answer is the above specified command.
when I tried
adb shell am instrument -e class com.company.test.data.model.TestClassName com.company.test/android.support.test.runner.AndroidJUnitRunner
It ran the tests as expected. The problem is I wont get jUnit Reports by the adb command but cAT will provide me test reports, code coverage reports as required.
Does anyone have solution for this issue to run specific tests?

It was actually the problem with gradle version. The developers had set the gradle version to 2.3 in which I was facing the issue. Then I have updated the gradle version to 2.10 and the issue is fixed.

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.

Android Studio 3.2 empty test suite

I have created a new project in Android Studio 3.2. Without making any changes I am not able to run all the unit tests in the group. I receive and error saying No tests were found
I am attempting to run the tests by right clicking on the group and clicking Run Tests.
I can run the unit tests if I open up the file and click on the run button next to the class declaration. I can also run the unit tests if I open the project in Android Studio 3.1.4. I can right click on the group and run all the unit tests with no error.
Looking at this bug report, I think it should be fixed in 3.2.1: https://issuetracker.google.com/issues/115708445#comment12
After spending an entire day trying resolve this while writing some unit tests I found one workaround that works for my project.
Basically what my workaround is to add Build to the Before launch options.
After adding this option I'll occasionally get the No tests found error message but simply rerunning the test worked every time after adding that setting.
I'm not sure if it will help with everyone's problem but it seems to have mitigated the issue with my project. Hopefully this works for someone else as well.
The workaround until the bug is fixed is running tests in terminal, just type:
./gradlew test
or
./gradlew testDebugUnitTest. If you are using Windows replace ./gradlew with gradlew.bat. You can also download Android Studio 3.3 Canary from here https://developer.android.com/studio/preview/ – there aren't problems with running tests via this version.

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

Android: How to Install the Instrumentation APK onto an AVD in a Headless Host

I'm trying to integrate Android JUnit tests with our Bamboo Ant builds. I've already tested the setup locally, and I'm able to run tests.
But when I tried the same setup in our Bamboo server, I get the following error when running the tests:
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.synapticstuff.guitartabs/pl.polidea.instrumentation.PolideaInstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.synapticstuff.guitartabs/pl.polidea.instrumentation.PolideaInstrumentationTestRunner
Note that I used a custom library for Instrumentation (http://code.google.com/p/the-missing-android-xml-junit-test-runner/), so that I can pull the JUNit xml test results and feed it to Bamboo.
Also, the build agent used in creating the build is an Ubuntu VM, which doesn't have any GUI so I need to do everything via command line. An AVD is already running on that VM.
I did encounter the same problem when I first ran the adb shell am instrument .. <snip> command locally, and I found it weird it worked after running the test from Eclipse.
Looking up the error log, the Android Instrumentation Framework article tells me that
"It's possible that the
instrumentation apk isn't installed on
your device or that the package name
is incorrect in the Manifest file."
so it must be that the instrumentation apk isn't installed.
So, how do I install the instrumentation apk onto the AVD?
Thanks!
There are probably multiple situations that can cause this error, but I got it from not having an instrumented version of the test package installed on the emulator. Documentation is a bit scant, but I don't think there's an "instrumentation APK" that you install onto the emulator (at least I couldn't find such a thing); you build your test application with instrumentation included. Anyway, this is how I run tests from the command line using ant, which now works:
ant instrument install test
and this also works running on Jenkins using the Android emulator plugin (https://wiki.jenkins-ci.org/display/JENKINS/Android+Emulator+Plugin). I found the instrument task in the command line build documentation here: http://developer.android.com/tools/building/building-cmdline.html.
There is another possible reason: the package name is incorrect in the Manifest file.
Check the package name in your unit test project's manifest file first!
I am also one of the few people who downloaded that custom InstrumentationTestRunner (http://code.google.com/p/the-missing-android-xml-junit-test-runner/) and I am currently stuck on this issue! I get the same error message, unfortunately I do not know the answer right now, but will update when I discover something helpful.
In the meantime I looked for other options like this person's Test Runner: http://www.alittlemadness.com/2010/07/14/android-testing-xml-reports-for-continuous-integration/
There is also another discussion related to continuous integration: How to Generate Android Testing Report in HTML Automatically
you can maybe solve this, if you add to your build script:
adb install -r testproject/bin/testproject.apk
checkout first how the apk-file is being named, because i still didn't figure out, how ant is going to do that exactly, but it's being created usually in bin directory.
See also the official android documentation.
I ran into this problem when running my tests against an old Android 1.5 virtual device. After switching to a 2.3.3 virtual device the problem went away. I don't know if the problem is due to the virtual device OS being old or the virtual device being created with old Android SDK tools.
I was running into the exact same problem with running an android build under Bamboo. I found that for some reason the android:targetPackage element in my test project manifest was incorrect. It was set to be the package of my main project, not the test project. For some reason running when running the tests in eclipse eveything worked fine, but it died when I tried to run the ant test at the command line. The instrumentation instrumentation tag read:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.blah" />
This was automatically generated by eclipse when I created the project. I changed it to read:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.blah.test" />
and suddenly everything ran fine.

Categories

Resources