Building instrumented Unit Tests

Source: Internet
Author: User

This article is translated from: Building instrumented unit Tests
Level limited oneself feel many places express not in place, but can't find the better expression way, if you think have better expression way, help me to improve!

Building instrumented Unit Tests

The instrumented unit test runs on your physical device or emulator, not the JVM in your local machine. If you need to get instrumentation information (such as the context of the target app) or you need an Android framework component (such as parcelable or sharedpreferences object) You need to create a instrumented unit test. Using instrumented unit tests can also help reduce the effort to write and maintain mock code. You still have the freedom to use the mocking framework, and if you choose the framework, it can be used to simulate the relationship of build and dependency. Instrumented unit tests can take advantage of the Android framework's API and supporting APIs, such as Android testing support Library.

Set up your test environment

Before you build the instrumented unit test, make sure your project configures your test code catalog and engineering dependencies as described in starting your test.

Create a instrumented unit test class

Your instrumented unit test class should be written as a JUNIT4 test class. Learn more about creating JUNIT4 test classes and using JUNIT4 for assertions and annotations, see Building local unit tests.

In order to create a instrumented JUNIT4 test class, add annotations before you begin defining your test class: @RunWith (Androidjunit4.class). You also need to specifically state that the Android testing support library provides the Androidjunitrunner class as your default Test runner. For a more detailed description of this step, see: Start your test.

The following example shows you how to write a instrumented unit test to verify that the Loghistory class is implementing the Parcelable interface correctly:

ImportAndroid.os.Parcel;ImportANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;ImportAndroid.util.Pair;ImportOrg.junit.Test;ImportOrg.junit.runner.RunWith;ImportJava.util.List;Import Staticorg.hamcrest.Matchers.is;Import StaticOrg.junit.Assert.assertThat;@RunWith(Androidjunit4.class)@SmallTest Public  class loghistoryandroidunittest {     Public Static FinalString test_string ="This is a string"; Public Static Final LongTest_long =12345678LPrivateLoghistory mloghistory;@Before     Public void createloghistory() {mloghistory =NewLoghistory (); }@Test     Public void Loghistory_parcelablewriteread() {//Set up the Parcelable object to send and receive.Mloghistory.addentry (test_string, Test_long);//Write the data.Parcel Parcel = Parcel.obtain (); Mloghistory.writetoparcel (Parcel, mloghistory.describecontents ());//After you ' re-do with writing, you need-reset the parcel for reading.Parcel.setdataposition (0);//Read the data.Loghistory Createdfromparcel = LogHistory.CREATOR.createFromParcel (parcel); list<pair<string, long>> createdfromparceldata = Createdfromparcel.getdata ();//Verify The received data is correct.Assertthat (Createdfromparceldata.size (), is (1)); Assertthat (Createdfromparceldata.get (0). First, is (test_string)); Assertthat (Createdfromparceldata.get (0). Second, is (Test_long)); }}
Create a test suite

To organize the execution of your instrumented unit tests, you can collect these test classes to organize them into a test suite and then run the tests together. Test suites can be nested with each other, and your test suite can contain other test packages and then run these test classes together.

A test suite is included in a test package similar to the main application's package. Typically, the name of a test package is usually terminated with a. Suite extension (for example: Com.example.android.testing.mysample.suite)

When you create a test suite for your unit tests, import the JUnit Runwith and Suite classes. In your test suite, add @RunWith (Suite.class) and @suite.suitclasses () annotations. In the @suite.suiteclasses () note, separate test classes or test sets are listed as parameters.

The following example shows how to implement a test suite named Unittestsuite, It organizes the Calculatorinstrumentationtest and calculatoraddparameterizedtest test classes together to run them.

Importcom. Example. Android. Testing. Mysample. Calculatoraddparameterizedtest;Importcom. Example. Android. Testing. Mysample. Calculatorinstrumentationtest;import org. JUnit. Runner. Runwith;import org. JUnit. Runners. Suite;Runs all unit tests. @RunWith (Suite. Class) @Suite. Suiteclasses({calculatorinstrumentationtest. Class, Calculatoraddparameterizedtest. Class}) public class Unittestsuite {}
Running instrumented unit tests

Run your instrumented unit tests as described in starting your test.

Building instrumented Unit Tests

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.