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