Android instrumentation does not start on a second device - android

Since some time, when I run an instrumentation test on a device (emulator) and try to start another test on a second device in parallel, I get the following exception:
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':app:connectedDebugAndroidTest'.
java.nio.file.FileSystemException: D:\Android\MyApp\app\build\outputs\androidTest-results\connected\name_of_the_first_device\logcat-MyApp.TestClassName-TestName.txt:
The process cannot access the file because it is being used by another process
The problem seems to be, that the build process tries to access a file from the first test device, which is already blocked. But I'm using a different device and try to run a different test.
The reason why I do that is because some of the tests are very large and therefore I try to spread them to several devices, so I get my results faster. The machine I run the tests on, has a hell of performance, so it can't be a problem of the resources ;-).
When I did the described actions some months ago, it worked properly, so I guess it's due to some updates (probably in Gradle or whatever). But I installed several updates since then, so it can't just be a problem of the current version.
Update - Two more inputs:
I recently switcht to using hilt and therefore created my own TestRunner, because an Application annotated with #HiltApplication cannot be used for tests, so I had to create a custom one for testing which needs to be run in a custom TestRunner as described here.
When I run the same tests on multiple devices using "Select Multiple Devices" dropdown in Android Studio it works properly.

The error message indicates a conflict over a build-and-run output file on your development computer, not on the Android test device. That is, it's trying to do another build-and-run in the same build output directory that the running test is using for purposes like the test's logcat output.
My first thought is to try duplicating androidTest to another build target. Same contents, different target name, different output directory. That workaround seems likely to get past this limitation (but with new limitations) and to help debug the problem. But the name_of_the_first_device subdirectory suggests that it can keep a logcat output file per test device, so the real question is how to get the test runner to use a separate subdirectory per test device.
Second thought is to point you to the the docs, Run Tests with Multiple Devices and Shard Tests.
With a custom TestRunner, see Write a Sharded IRemoteTest Test Runner (and other pages in the "Tradefed" section) on making it a "shardable" test runner which enables the test infrastructure to distribute the full test execution over several devices.

Related

Android: set package name depending on device running on

I have functional autotests for some SDK (as aar-libs) and TeamCity CI server with several Android devices and emulators connected on.
If I use gradle to run my tests (./gradlew connectedDebugAndroidTest), all tests will run on each device, but each next results will overwrite previous ones and TeamCity will show only the last results.
I have to distinct these results and should know which results from what device/emulator.
Any ideas?
Decided to create additional build configuration on Team City, which will parse all connected devices from adb devices and trigger all other configurations passing device name as parameter

Running unit test via Android Studio works first time then fails

When running a unit test from within Android Studio on a mac, I can manually run it 1 time, then it fails with the error "Could not execute build using Gradle distribution 'https://services.gadles.org/distributions/gradle-4.10-all.zip'".
I can run it again by deleting my .gradle folder and restarting Android Studio, but this isn't sustainable for testing.
When running the unit test manually with ./gradlew, it works every time but that doesn't lend itself well to debugging.
This applies to even the most simple of tests.
Wanted to add specific error message from the Build tab. This happens on a simple test, with the first time running it working, and the second time running it not working:
#Test
fun testTrue(){
assertTrue(true)
}
FAILURE: Build failed with an exception.
* What went wrong:
java.lang.NullPointerException (no error message)
So, I have a suspect that could be the cause, and a hack that makes it work in the short term.
First, the hack. As many other people have answered, deleting the .gradle folder resolves the issue for me temporarily. However, that is a bit over overkill and forces a complete build every time you run it.
You can get around this partially by deleting just the fileHashes.bin and fileHashes.lock file in .gradle/4.7/fileHashes folder. I wrote a quick script to delete those two files and edited the "Android JUnit" Template in "Run/Debug Configurations" to run that script before every junit run. See screenshot for details, but you'll need to make sure it is the first thing that runs. (Drag it to the first)
The .bsh file is an executable which just deletes those two files.
#!/bin/bash
echo This script deletes the .gradle files necessary for my android studio to work
cd wealth-android/.gradle/4.7/fileHashes
rm fileHashes.*
Now, the real problem may be that I could have multiple implementations of gradle on my machine. I'll look into that more and update the answer.

Robotium (and Robotium Recorder) checking default application and location of test results

I am doing a simple series of tests with Robotium Recorder on Android Studio. I have created my test cases and had a few questions:
One of my tests is to click a URL link and to make sure that the default application dialogue/internet browser is displayed. I don't see an option to do this from the recorder (I wouldn't expect it). I have been looking through the documentation for the solo object and do not see anything that would help me out. There has to be a way to check this.
Once a test is ran from Android Studio, where would the file containing the test results reside?
According to second question: I'm not sure that Robotium already creates any tests reports. You may use for it additional addons like Spoon or Emma.
Read: Creating Test Reports for Android with Spoon and Emma
I know also that Robotium can do a screenshots during the run of tests, but already I don't use this framework so please find on your own how to do it and where screenshots are placec if you feel a bit interesting.
Already, for creating instrumentation tests report I use Gradle. It generates quite good HTML reports. There are at least two ways to make it
in Android Studio, on the left panel, choose Gradle then app, verification, finally connectedAndroidTest
if you're familiar with Unix console go to your project location and just run the command:
./gradlew connectedAndroidTest
NOTE: On the first time it might be needed to run before chmod u+x gradlew, if console would say that you don't have enough permissions to do it.
SUGGESTION: if you make one of your test failed, in generated by Gradle test report you would find a location of HTML generated tests report.
Gradle test report would look like this if all would run successful or this, if at least one would fail.

Android gradle connectedCheck resume on failure

Is it possible to resume on failure, while running gradle connectedCheck?
I want to use it in hudson, but it stopped execution after the first failure. How can I configure it to do the next test on failure? This way, I see all failing tests at the end and not only the first one.
This may not answer the question in the way you wanted but here goes anyway. What I did to solve this problem was to use the Spoon testing framework as a wrapper around the Android test framework. It gives you lots of advantages:
You can use the regular tests you've already created
Spoon produces permanent HTML output that you can save
You can add screenshots to your test output
The test output contains the log messages for failures
And, most relevant to your question, it continues on even when a test fails.
You can find spoon at: http://square.github.io/spoon/

Android Robotium test - NoClassDefFoundError

I'm having problems with Robotium being unable to find my app at runtime.
Just to be clear, I've seen a few similar questions on SO however, they seem to revolve around the Robotium Solo class not being found at runtime (or some other 3rd party lib). However, this is slightly different as my Robotium tests can't find the project under test.
The tests are in a separate project which has a dependency on my app project. There are no problems at compile-time but when I try to run the tests I get...
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests
....
Caused by: java.lang.NoClassDefFoundError: com.myapp.mobile.activity.RegisterUserScreen
Where RegisterUserScreen is an activity in my app. This happens on the device and the emulator.
I'm building / running in IntelliJ 12.1.4, against Android 4.1.2 and I see the same result when I attempt to run the test from the command line.
Probably worth pointing out that running adb shell pm list instrumentation from the command line returns the correct instrumentation...
instrumentation:com.myapp.mobile.test.system/android.test.InstrumentationTestRunn
er (target=com.myapp.mobile)
Thanks in advance,
Neil.
In your device go to settings->apps and check whether in installed or running application list name of your testing application name is present. If you find it there then uninstall the testing app as well as your original app (which has to be tested). Many times it happens that either because of different versions/signature mismatch test-application fails to find the target application.
Create your run configuration as an "Android Tests" not as "JUnit"

Categories

Resources