Android Studio: How to run tests in Firebase with Orchestrator enabled? - android

Is there a way to run instrumentation tests in Firebase Test Lab from Android Studio with Android Test Orchestrator enabled?
In my build.gradle I have all of the required statements:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROID_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
}
When I run tests on my own physical device, Orchestrator is working.
I was able to create a Run/Debug Configuration in Android Studio to run tests in Firebase Test Lab and they run successfully, but with no Orchestrator. Is there an option to enable Orchestrator in Android Studio so that it would also work in Firebase runs?

Related

Run Android instrumented tests fail

I tried to implement some unit and instrumented tests for my Android application (Java), my unit tests is working fine but instrumented tests are failing:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ExampleInstrumentedTest {
#Rule
public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class);
#Test
public void checkIfCategoriesIsNotEmpty() {
onView(withId(R.id.header_left_layout)).perform(click());
onView(withId(R.id.list_view)).check(new ViewAssertion() {
#Override
public void check(View view, NoMatchingViewException noViewFoundException) {
ListView list_categories = (ListView) view;
ListAdapter adapter = list_categories.getAdapter();
Assert.assertTrue(adapter.getCount() > 0);
}
});
}
}
When I try to run my test I got this error :
"Run Android instrumented tests using Gradle" option was ignored because this module type is not supported yet.
My Implementations in build.config file are :
// UNIT TEST
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
// INSTRUMENTED TEST
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
I just ran into this problem, too. I found the solution in the Android Studio Bumblebee 2021.1.1 release notes under the "Android testing" section.
It basically amounts to unchecking the box next to Run Android instrumented tests using Gradle in the testing settings. This worked for me.
the solution is to exclude module: protobuf-lite :
androidTestImplementation('androidx.test.espresso:espresso-contrib:3.4.0') {
exclude module: "protobuf-lite"
}
As mentioned here
Disable the unified Gradle test runner
By default Android Studio Bumblebee uses Gradle to run its instrumentation tests. If you're experiencing issues, you can disable this behavior as follows:
Select File > Settings > Build, Execution, Deployment > Testing (or Android Studio > Preferences > Build, Execution, Deployment > Testing on MacOS.)
Uncheck the box next to Run Android instrumented tests using Gradle and click OK.
So you can just disable the unified Gradle test runner.
For me, the issue was that I was running on Xiaomi Redmi note 7. Once I switched to the emulator. everything worked fine.

Android Studio: Can not find Run test choice

When I try to run the unit test in a class, I can not find a Run choice
But I can see it on my teammate laptop in the same project and when clicking on the arrow beside the function and the class name it displays "Nothing here"
Here are some steps you can try to avoid reinstalling android studio, with examples from a project having working instrumented tests.
In general, if you also have a pc where the tests works, I advise you to compare all the following configurations between the two environments.
Also try to start the tests with a preview version of Android Studio.
Enable test plugins
Go to Android Studio -> Preferences... -> Plugins and check if the test plugins are enabled (i.e. JUnit)
Update Gradle version
Go to File > Project Structure... > Project and update to the latest Gradle version.
Update Kotlin and Gradle plugin versions
Check if you're using the latest versions in your build.gradle (project level)
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
Update your test dependencies
In your build.gradle (module level)
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dependencies {
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
...
Remove JUnit group exclusion
In the same file, if present, remove the following exclusion
exclude group: "org.junit.platform" //remove this line

Test Orchestrator Sample

Is anyone aware of a sample project that shows how to get test orchestrator working? I checked the google samples and there doesn't seem to be a good sample project that shows test orchestrator.
https://github.com/googlesamples/android-testing
I have been attempting to get android tests running in the test orchestrator but have been struggling to get it to work correctly. I tried both running the tests through Android Studio (latest 3.2.1) as well as the command line (https://developer.android.com/training/testing/junit-runner#ato-command-line). I used the Android developer document for reference.
https://developer.android.com/training/testing/junit-runner
Here are the steps I followed.
1) Create an empty activity application using the wizard in Android
Studio
2) Enable the test orchestrator using the steps provided here
(https://developer.android.com/training/testing/junit-runner).
3) Run the unit tests from within the IDE and from the command line.
When I do this, I get an error indicating that my "test suite is empty". I get the same error running from command-line.
Note that if I run the test without test orchestrator, then the test runs successfully.
Also note that I am using the latest test orchestrator versions
test-orchestrator (https://maven.google.com/androidx/test/orchestrator/1.1.0/orchestrator-1.1.0.apk)
test-services (https://maven.google.com/androidx/test/services/test-services/1.1.0/test-services-1.1.0.apk)
The complete configuration to run test orchestrator:
Add dependencies:
androidTestImplementation "androidx.test:runner:$testRunner"
androidTestUtil "androidx.test:orchestrator:$testOrchestrator"
Add clear package instruction (within defaultConfig in app's build.gradle):
//allows run all tests on an isolated way. If we need to debug a test, should disable this and the orchestrator
testInstrumentationRunnerArguments clearPackageData: 'true'
Add to testOptions Android/AndroidX orchestrator:
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}

Integration test and Cucumber test

I have a project where I'm running android instrumentation tests with an AndroidJunitRunner.
I'm now adding UI automated tests using Cucumber. How can I keep both Cucumber tests that use a runner that extends MonitoringInstrumentation and the other instrumentation tests that use a runner that extends `AndroidJunitRunner?
In the build.gradle I used to have
testInstrumentationRunner "com.packagename.packagename2.MockedTestRunner"
now I have:
testApplicationId "com.packagename.packagename2.test"
testInstrumentationRunner "com.packagename.packagename2.test.Instrumentation"
Do I need to create a second module to run the cucumber tests?
To enable Android Test Orchestrator using the Gradle command-line tool, complete these steps:
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
Run Android Test Orchestrator by executing the
./gradlew connectedCheck
Enable from Android Studio
Support for Android Test Orchestrator is available with Android Studio 3.0 and higher. To enable Android Test Orchestrator in Android Studio, add the statements shown in Enable from Gradle to your app's build.gradle file.

Espresso testing in separate module

I have the android application and I want to use Espresso framework to create test automation tools and tests. But I don't want to create something in my app and want separate module with Espresso that will start my app and test it using Espresso. I use Android Studio. So.. Have you any ideas how to solve this problem?
You can use a library module specialised for instrumentation tests. Basically just create a library module but use a different plugin:
apply plugin: 'com.android.test' // A plugin used for test-only-modules
Also you need to changep your android part in your build.gradle file in your test module a little:
android {
[...]
defaultConfig {
[...]
testApplicationId "com.yourcompany.yourapp.test"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
[...]
// Set the target app project.
// The module specified here should contain the
// production code test should run against.
targetProjectPath ':app'
}
Then add your app and all your espresso libraries etc. in your test module's dependencies:
dependencies {
implementation project(':app')
// All your test dependencies etc.
implementation "androidx.test.espresso:espresso-core:$espresso_version"
}
Using a gradle sync and run tests you can then execute your tests locally. You'll also get all your normal gradle tasks for that module which you can use to assemble your test APK etc. if you need it to test externally e.g. in Firebase Test Labs.

Categories

Resources