Android Studio Series-Introduction to Unit Testing

Source: Internet
Author: User

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

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.