As the current stage of Android development to agile development, coupled with the domestic large and small Internet companies are doing the app, resulting in a lot of this will be a series, so if you read this article, please see the following articles:
Developers have no basic concept of unit testing, but this blog post will not explain what unit testing, functional testing, but rather how to use Gradle in Android studio using the current popular test framework.
To ensure the quality of the app or library, it is important to have a complete test. For a long time, Android development tools lacked support for integrity testing, but recently Google has done a lot of work to make it easier for developers to test, some old frameworks have been updated, and new frameworks have been added. Not only can we run these tests in Android studio now, we can even execute them directly from the command line with Gradle.
So based on this, we'll introduce several ways to test Android apps, and we'll delve into why Gradle can help test automation.
In this chapter I will follow the following elements:
- Unit Test
- Functional Testing
- Test coverage
Unit Test
Believing that everyone has the concept of unit testing, good unit testing not only ensures the quality of the app, but also makes the new code easier to develop. The Android Studio and Gradle Android plugins support unit testing by default, but you still need to configure it before you use it.
Junit
JUnit survived for several years and is very popular in the testing community. It makes the test code easy to write and maintain, but remember that JUnit can only test the logic code, and that the code associated with the Android SDK is no more than an egg.
Before you start writing junit tests, you need to create a new directory for them. Normally, this will be called Test, which will be the same as your main folder.
Java │ │ com. Example. app│└───com. Example. App
You can create a test class in the tests directory.
I recommend that you use JUnit 4, which you can add as a dependency to your dependent library.
dependencies { ' junit:junit:4.12 '}
Notice that you are using testcompile, which means that the jar package will only import the APK when you test.
If you have other builds, and you just want to add the jar for a specific version, you just need to do this:
dependencies { ' junit:junit:4.12 '}
When everything is OK, it's time to start writing the test code. The following is a simple test code:
Import Org.junit.Test;import static org.junit.Assert.assertEquals; public class logictest { @Test public void addingnegativenumbershouldsubtract () {Logic logic = new Logic (); Assertequals (" 6 +-2 must be 4 ", 4, Logic.add (6,-2)"); Assertequals ( "2 +-5 must be-3",-3, Logic.add (2,-5)); }}
So how to run up, also very simple, run Gradlew test. If you just want to run in a specific version, add a chant gradlew testdebug. If the test fails, Gradle will print a related error, and if all tests pass successfully, the build successful will be displayed.
You might say that a single test case causes the whole test to fail, so it's not good, if you want to run the whole test case all over again, that's easy:
$ gradlew Test--continue
Performing a test task is more than just running through all the tests, but it also creates a test report for you and you can find it app/build/reports/tests/debug/index.html. This report allows you to find problems faster, and I think the most important thing is that when you automate your tests, this can be very useful, and Gradle will create a test report for each build. If you perform the test successfully, your test report looks like this:
Having said so much about the original approach, then look at how Android studio runs the test. Right-click the project or select the Start button ... This is too basic to say that the success of the operation is like this:
Well, the JUnit test is done, isn't it simple.
If you want to test your affiliate Android SDK code what to do, unit testing is not a good idea, fortunately, there are multiple dependencies for you to choose, the most famous of which is Robolectric, which makes it easier for you to test Android features and not run on devices or emulators.
Robolectric
By using Robolectrie, you can write test classes that can use the Android SDK and resource files, and, of course, run on the JVM, which will allow you to test the app more quickly.
Before you start using Robolectrie, you need to add dependencies. Note that in addition to robolectric dependencies, you need to add a JUnit package.
' Org.robolectric ' dependencies { ' libs ', include: [' *.jar ']) ' Com.android.support: appcompat-v7:22.2.0 ' testcompiletestcompile' org.robolectric:shadows-support:3.0 '}
The Robolectrie test class also needs to be written under the test folder, for example:
@RunWith (robolectrictestrunner.class) @Config (manifest = "app/src/main/androidmanifest.xml", SDK = 18) public class mainactivitytest { @Test public void clickingbuttonshouldchangetext< Span class= "Hljs-params" > () {appcompatactivity activity = robolectric.buildactivity (Mainactivity.class). Create (). Get (); Button button = (button) Activity.findviewbyid (R.id.button); TextView TextView = (TextView) Activity.findviewbyid (R.id.label); Button.performclick (); Assertthat (Textview.gettext (). toString (), Equalto (activity.getstring (r.string.hello_robolectric))); } }
Functional Testing
God Horse is a functional test, which is used to test whether multiple modules of an app work properly. For a chestnut, you can create a functional test to make sure that you have a new activity when you click on a button. Still, we will have many frameworks. But here, I recommend espresso.
Espresso
The purpose of Google's creation of espresso is to simplify the writing of functional test cases by developers. This package is provided by Android support Repository, so you can use it with SDK Manager.
Before you run the test case, you need to define a runner. Google offers the Androidjunitrunner Test Runner, which will help you run the unit test on your phone. Test Runner can help you install APK and a test apk, perform all tests, and generate test reports.
If you download the support library package, you need to define this:
defaultconfig { testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner" }
Of course you need to add some dependency packages:
dependencies { ' Libs ', include: [' *.jar ']) ' com.android.support:appcompat-v7:22.2.0 ' ' com.android.support.test.espresso:espresso-contrib:2.2 '}
Note that these dependent packages use Androidtestcompile, which differs from Testcompile. When you run directly, you get an error:
of APK app-androidtest.apk in archive:LICENSE.txt 1: .../hamcrest-library-1.1.jar 2: .../JUNIT-DEP-4.10.jar
Its meaning is also very clear, because multiple files are caused, and you can simply handle the following:
Android { packagingoptions { ' LICENSE.txt ' }}
Note: The functional test needs to be placed in the Androidtest directory, the following is the test case:
@RunWith (androidjunit4.class) @SmallTest testhelloworldisshown() {Onview (Withtext (" Hello world! ")). Check (Matches (isdisplayed ())); } }
Functional testing also has a test report, and when executed correctly, it should be:
Finally, there may be friends asked, in the Android studio to perform the test, then the drawings it:
Test coverage
Once you have used the test in your project, you will definitely want to know your test coverage. Very real, there are still a lot of test coverage tools, I recommend the Jacoco.
Jacoco
There is a coverage report, very simple. You only need to configure:
buildtypes { debug { true }}
When you're done with the build, you can find it in app/build/outputs/reports/coverage/debug/index.html, and each version will have a report. The test coverage would be like this:
You can even see what line of code is being tested by clicking to see more information.
Summarize
In this chapter, we learned how to test, we learned simple unit tests, and robolectric tests. We learned about functional testing and learned how to use espresso. Best of all, we learned how to view test coverage reports.
In the next chapter, I'll bring up the most important chapter, the custom build process, the creation of custom tasks and plugins. Of course, we'll start by introducing groovy syntax, and understanding its syntax makes it easier to understand how gradle works.
"Reprint" Gradle for Android Sixth (test)