Configuring Support Unit Test Engineering
Select "Unit Tests" in the test artifact in the build Variant window
Open the project's Build.gradle (Module:app) file, add JUNIT4 dependencies, and click the Gradle Sync button.
Build.gradle
dependencies { compile fileTree(dir‘libs‘, include: [‘*.jar‘]) ‘com.android.support:appcompat-v7:22.1.1‘ ‘junit:junit:4.12‘}
Creating unit Tests
Example
Creating the Calculator class
Select the class name and right-click the following:
Check Test
The corresponding test class is then generated:
Public class calculatortest { PrivateCalculator Mcalculator;@Before Public void setUp()throwsException {mcalculator =NewCalculator (); }@Test Public void Testsum()throwsException {assert.assertequals (6D, Mcalculator.sum (1D5D),0); }@Test Public void testsubstract()throwsException {assert.assertequals (1D, Mcalculator.substract (5D4D),0); }@Test Public void Testdivide()throwsException {assert.assertequals (4D, Mcalculator.divide ( -D5D),0); }@Test Public void testmultiply()throwsException {assert.assertequals (TenD, Mcalculator.multiply (2D5D),0); }}
Run unit Tests
Check the test class name and right-click the following:
Click Run calculatortest
The above is the wrong result.
The following are the correct results.
Welcome to follow the public number: Wwjblog
Android Studio Series-Introduction to Unit Testing