Android Development: "Gradle Recipes for Android" reading notes (translation) 5.1--Unit test

Source: Internet
Author: User

Problem:

You want to test the non-Android part of the app.

Solution:

You can use the added unit test support and Android Gradle plugin in Android Studio1.1.

Discuss:

The ADT plugin only supports integration testing and requires the developer to create a separate test project. One of the benefits of converting to Android studio and Gradle is that it supports testing within Android projects.

Before AndroidStudio1.1 and related Gradle plugins, these tests are still limited to integration testing, meaning that you need a simulator or connected device to test. Integration testing can be very powerful and useful, and is part of sections 5.3 and 5.4.

This section discusses the real unit tests, which are on the JVM that is being developed locally on the machine. Unlike integration testing, the Androidtest resource folder is used, and unit tests are under the Src/test/java directory.

When you create a new Android project using Android Studio, a sample unit test has been provided for you. It is under the Src/test/java tree, but not below classpath:

The resulting tests are as follows:

Such tests should be familiar to any Java developer who has used JUnit. The @test annotation of JUNIT4 indicates that the Addition_iscorrect method is one and the test method. The Assertequals method is a static method inside the Assert class (note that using static import imports all static methods), the first parameter is the correct answer, and the second parameter is the actual test value.

In order to run the test, what you need to do is to select Test Artifact under the build variants view:

By selecting "Unit Tests", the Src/test/java directory tree is recognized by Android Studio for inclusion of test resources, and the number of Com/oreilly/helloworld is included under the package.

The last step before you perform a unit test. You need to make sure that JUnit is included in the Testcompile. As follows:

You can now run the test with the Gradle test:

Run a test for each variant and generate HTML under the App/build/reports/tests directory:

Open the index.html file under the directory and report such as:

You can drill down into the Exampleunittest class to see the exact result:

You can use the--tests identity limit test to test only one variant, or even one Test class:

The variant is still built, but only the specified one, and only the tests inside the Exampleunittest class are run.

You can also choose to right-click the test and run it inside Android studio. But it's only for the current variant, providing a nice result to show:

The only problem is that it doesn't run anything meaningful. That's the point. When using JUnit support, you can't test anything that relies on the Android SDK. JUnit tests only support pure Java portions of those applications.

In section 4.5, the library connects to the Network service, downloads JSON data, parses, and updates the value of the TextView. If you like, you can test part of the process:

The good news is that unit tests are very fast, at least relative to integration testing, because they don't need to be deployed to a real machine or emulator. If you have Java classes that don't depend on Android classes, unit testing is a great way to make sure they work well. Test-driven development has not been accepted in the mobile world as in the conventional Java world, but it is a good start.

Add:

The Robolectric project is designed to run integration tests without using an emulator or a connection device, which behaves like a mock of an Android SDK. A lot of people don't agree with it.

Android Development: "Gradle Recipes for Android" reading notes (translation) 5.1--Unit test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.