Android Studio Unit Test

Source: Internet
Author: User

Android Studio Unit Test flow

Android Studio Unit test new project Engineering

New Moudle, File-New

When a project is created, it is sometimes reported that a creation error or other cause is not available for the created project. This is due to the Android studio build environment, which can be removed by the rebuild.

New project does not need to be configured with default support JUNIT3 style test

First, JUNIT3 style test application

While creating a new project, create a default test file under Android Studio or under a test package:

Applicationtest.java

public class ApplicationTest extends ApplicationTestCase<Application> {     public ApplicationTest() {        super(Application.class);    }}

Right-click to run, ok!!

Test activity

Set up a test class for Test mainactivity:

Mainactivitytestjunit3.java

public class MainActivityTestJunit3 extends ActivityInstrumentationTestCase2<MainActivity> {    public MainActivityTestJunit3() {        super(MainActivity.class);    }    public void testGetActivity() {        assertNotNull(getActivity());    }}   

Right-click to run, ok!

Test Service Test ContentProvider

Follow-up supplement ....

Second, JUNIT4 style

Configure the JUNIT4 test environment: The main configuration testInstrumentationRunner properties, as follows:

Build.gradle

defaultConfig {    applicationId "com.demo.myapp"    minSdkVersion 16    targetSdkVersion 23    versionCode 1    versionName "1.0"    testInstrumentationRunner ‘android.support.test.runner.AndroidJUnitRunner‘}
Test activity

New Test class

Mainactivitytestjunit4.java

@RunWith(AndroidJUnit4.class)public class MainActivityTestJunit4 {@Rulepublic ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);    @Test    public void getName() {        Log.d("TT", mActivityRule.getActivity().getLocalClassName());    }}

Introduce dependent packages, compile. Right-click to run: ok!

Test application Test Service test ContentProvider

Android Studio 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.