What is the list of tasks that ConnectedAndroidTest performs?

I want to learn more about the ConnectedAndroidTest Gradle task. I see that it is used to install the application and test apks and run tests.

But what are the individual steps that he takes? (gradle tasks, if any)

"gradle build" seems to be creating an apk application. What task does apk test generate? And how does this (ConnectedAndroidTest) install the application and check apk? And how to start the tests?

Many thanks.

+7
android android testing
source share
2 answers

My first answer, please be careful;)

But what are the individual steps that he takes? (gradle tasks, if any)

So, if you want those tasks to be displayed at a high level ConnectedAndroidTest, just starting ./gradlew connectedAndroidTest or ./gradlew cAT (without the -q option) will display the name of each task that cAT depends on how it is executed. The task itself cannot have other tasks in it, but it can depend on others that are ahead of it.

From this answer, the gradle build task is actually Java related and not responsible for creating the apk test. Instead, it is the assembleAndroidTest task, which runs immediately before the connectedAndroidTest that does this. You are correct in connectedAndroidTest , although it actually installs and runs the apk test. But I will come to how little. The rest of my answer goes in more detail than is necessary for the effective use of the task, but is useful if you want to understand how it works.

Some background
Like many other Android gradle plugins, connectedAndroidTest actually gathers at some point in the execution phase due to different build options (debugging, release, taste 1, taste 2, etc.). Thus, connectedAndroidTest not available to you at the configuration stage (when most of your script build logic is executed). Instead, after creating it, it is set as the connectedInstrumentTest property (basically a field) of the testVariants property in the android object.

As an example for clarification, if you want to access this task in order to somehow handle it (maybe add an Action to the end), you can do something like this in your build.gradle file:

 android { testVariants.all { variant -> variant.connectedInstrumentTest.doLast { println "This will be executed right after our connectedInstrumentTest!" println "The name of the test type: $connectedInstrumentTest.name" println "The type of test $connectedInstrumentTest.class" } } } 

And then run ./gradlew -q cAT

So here I am adding an action to the end of any task that has been built and assigned to the connectedInstrumentTest property, which is deep enough embedded in the android object. This task will probably be connectedDebugAndroidTest or something similar.

What does the task do?
Now, from the type property placed in the last println, we see that the task class is actually com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask_Decorated . Honestly, I'm not sure yet that this part of _Decorated coming, but a google search for the rest of the class name gives us the source code for the base class of the task.

The main Action task is called runTests() and shows you more or less how the task does what it does. If you come from the source code a little, you will find that the adb pm install command will be used to install apk.

Although I could not find it, I suspect that somewhere else the adb command adb shell am instrument -w com.package.name/android.support.test.runner.AndroidJUnitRunner used for the final testing of tests.

Therefore, I hope that this was not too confusing - I learned most of this recently, so some things may not be 100%. I would suggest working with gradle docs, in particular how to create a custom plugin and custom task, and also check out Android gradle in the tool documentation .

+17
source share

To answer a more general question: β€œWhat is the list of tasks that the <taskName> task performs?”, There are two easy ways to find this for any given task.

First:

 ./gradlew tasks --all | grep <taskName> 

where <taskName> should be replaced with any task you care about. For example, ./gradlew tasks --all | grep connectedDebugAndroidTest ./gradlew tasks --all | grep connectedDebugAndroidTest . Please note that I am paving the way through grep to save myself the trouble of manually sifting through the list of all tasks.

Second:

Use the task-tree plugin. After application, the use is as follows:

 ./gradlew <taskName> taskTree 

Or, as I usually prefer:

 ./gradlew <taskName> taskTree --no-repeat -quiet 

The last option makes the output a little less cluttered.

+2
source share

All Articles