I'm trying to add instrumented test using Espresso in my android project.
I've added all necessary libs, besides adding AndroidJUnitRunner as the testInstrumentationRunner:
But for some reason, the imports are not working in instrumented test file:
But the local test file is getting the imports properly:
I've also added junit as androidTestImplementation, but makes no difference:
androidTestImplementation 'junit:junit:4.12'
This is why I cannot run the test file.
If I run the file anyway, I get this error:
Process finished with exit code 1
Class not found: "com.mcp.shippax.MainActivityEspressoTest"Empty test suite.
I cannot understand why this is happening (numerous grade syncs/invalidate caches/restarts), when the setup is so straight and simple.
I don't remember making any project changes other than converting most of my source files to Kotlin, including the test files. But later I reverted the test files back to java again.
I had the same issue. I've had various build types for dev and prod environments and what helped me, was to specify testBuildType like in this answer https://stackoverflow.com/a/34778780/9736105.
So something like this:
android {
...
testBuildType "<my-build-type>"
buildTypes {
<my-build-type> {
...
}
....
}
}
After I did that and set my build variant to that build type, I was able to resolve all dependecies.
Related
When I've decided to add instrumentation tests, I found that androidTestImplementation dependencies are not recognized. In my test file related classes like RunWith were displayed as Unresolved symbols.
When I've tried to run the test anyway, in run configuration there was a message: Error: Instrumentation runner class not specified.". I've checked, instrumentation runner class was explicitly specified in build.gradle.
I use custom build types like debugDev, debugProd.
It turns out that if non-standard build types are used, it's needed to select a build config for running instrumented tests. It can be done by using testBuildType "nameOfBuildType" in android {...} in a module's build.gradle.
I'm trying to share some code between my unit tests and my instrumentation tests. I've implemented something like the following in my build.gradle:
sourceSets {
String sharedTestDir = 'src/sharedTest/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
This seems to work just fine for normal code. However, code that is generated from an annotation seems to produce problems in Android Studio only. (The code runs fine, but it produces red squiggles in Android Studio) Basically, Android Studio can't seem to find the generated classes for code that lives in src/sharedTest - even though the actual build is fine.
In case it's helpful, the generated classes are coming from Dagger (as I share a component/module between the two types of tests). I've got a custom Application object that lives in sharedTest which is where I notice the Android Studio error:
createApplicationComponent() {
return DaggerMockApplicationComponent.create();
}
I've also added the apt command to both test and androidTest:
dependencies {
testApt 'com.google.dagger:dagger-compiler:2.7'
androidTestApt 'com.google.dagger:dagger-compiler:2.7'
}
I'm clearly missing something in my understanding around source sets here. Is there a cleaner way to share the code between test and androidTest? Is there a way to get Android Studio to see this generated code? Or, worst case, can I make Android Studio at least ignore this error more easily?
I have added a folder for unit testing in my android studio project. The default folder is andoidTest but I have added a new folder and name in test instead. (like robolectric sample tests)
When I add test Dependency in my build.gradle under module such as
testCompile("junit:junit:${junitVersion}")
testCompile ("org.robolectric:robolectric:${robolectricVersion}")
They do not get added to external libraries under project, but when I use the default configuration and use androidTestCompile, it can added external libraries.
Then I thought that maybe I should setRoot for tests in gradle, so I used following in android tag in build.gradle:
sourceSets {
androidTest.setRoot('src/test')
}
But still problem remained. I can run tests using gradlew, but imports in classes in test folder do not apply as well as no external library for test purpose is visible.
Anyone have any solution for this issue?
I was searching and didn't find answer that I thought already covered this. So decided to create new one for the future.
Answer
Android Studio is not picking up unit tests automatically right now. I know it is planned for 1.3 version.
So you have to change test artifact value from Android Instrumental Tests to Unit Tests in Build Variants tool window:
Almost fine your Gradle script but try do that:
sourceSets {
androidTest.setRoot('src/test')
androidTest {
java.srcDirs = ['src/test/java']
}
}
So ultimately I'm trying to separate my integration tests from the unit tests in an Android Studio project. I've found a few resources on the subject:
http://selimober.com/blog/2014/01/24/separate-unit-and-integration-tests-using-gradle/
https://blog.safaribooksonline.com/2013/08/22/gradle-test-organization/
Separating integration tests from unit tests in Android Studio
All these seem to indicate that the way to go is to create a new sourceSet for the integration tests, and then to create a new test task which builds and runs the tests in that source set. I can't get past the first step of creating a source set which is recognized by Android Studio.
Here's what I have within app/build.gradle, which builds without errors, but does not result in an integrationTest source root I can add classes to:
android{
...
sourceSets{
integrationTest {
java.srcDir('src/integrationTest/java')
}
}
}
My questions are:
Where precisely do I have to add the sourceSets block? In build.gradle? in app/build.gradle? In app/build.gradle inside the android block?
Once I've added my source set in he right place using the correct syntax, is this sufficient for Android Studio to detect and present it in the UI along side the main and test sources, or are there additional steps?
edit:
I've attempted to follow the instructions in marius' answer, but integrationTest isn't showing up in my build variants. Here's what I'm seeing:
This is enough:
android{
...
productFlavors{
integrationTest {
}
}
}
Regarding your 1st question: The productFlavors block should be in your app/build.gradle, inside android block.
Regarding your 2nd question: Once you add this to your build.gradle file, you also need to create your folders /src/integrationTest and /src/integrationTest/java . Once that is done, sync your gradle files and choose your new Build Variant from the Build Variant window, in order for the IDE to detect it as the active source folder.
I have a test suite within my Android studio that has the following dir structure:
-MainProject
-src
--com
--tests
--java.xx.xxx.xx.test
Within my AndroidManifest, I have the following:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="tests.java.xxx.xxx.xxx.test" />
When I run my tests from the command line using: ./gradlew connectedInstrumentTest, I simply get:
Tests on Nexus 5 - 4.4.2 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
When I run the tests from inside Android Studio I get:
Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{tests.xxx.xxx.xxx.xxx.test/android.test.InstrumentationTestRunner}
Empty test suite.
I also have the testPackageName specified in my defaultConfig in the build.gradle file. Am running Android Studio 0.4.6 on Ubuntu 12.0.4.
Any ideas?
I had the same problem and could not find anything on the net. The answer was pretty simple: You have to edit the RunConfiguration of your test so that it is not a Android Test but a JUnit test.
So basically I did the following:
Clicked "Edit Configuration"
Clicked "Add new Configuration" and chose JUnit
Inserted a name and the and chose "All in package" at Test kind.
That's it.
I had the same problem, what happened to me was an error with the configurations of tests.
Be sure that you have the correct base configurations for testing like those below in the image:
If you don't have an Android Instrumented Tests configuration, you can create one from the yellow arrow.
PS: Moreover check that classes referenced by configurations really
exists and the package name is correct
I also get error like this and I added the following line in build.gradle file as shown in https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html and it's working. I hope it will also help you :-)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Basically three things to cross check:
Make sure your have added testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
in your defaultconfig of build.gradle of that project/ module (in case of sub module)
Make sure you have added following test compile dependencies:
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support:support-annotations:25.2.0'
androidTestCompile 'com.android.support.test:runner:0.5'
Make sure you are adding your test files to the AndroidTest folders and not just to test folder.
I tried various methods but none fixed the issue for me. What I did is simply make a copy of the java test file into the same exact folder but with a different name and deleted the original.
I believe this issue was originally caused by moving my test class from the project test folder into the androidTest folder.
I removed the entry android.support.test.runner.AndroidJUnitRunner from the Android Test configuration and it started to run the tests for me