Home | History | Annotate | Download | only in unit-testing
      1 page.title=Building Effective Unit Tests
      2 page.tags=testing,androidjunitrunner,junit,unit test
      3 page.image=images/testing/hwtest_junit_success.png
      4 
      5 trainingnavtop=true
      6 startpage=true
      7 
      8 @jd:body
      9 
     10 <div id="tb-wrapper">
     11 <div id="tb">
     12         <h2>
     13           You should also read
     14         </h2>
     15         <ul>
     16           <li>
     17             <a href="{@docRoot}tools/testing-support-library/index.html">Testing Support Library</a>
     18           </li>
     19         </ul>
     20 </div>
     21 </div>
     22 
     23 <p>Unit tests are the fundamental tests in your app testing strategy. By creating and running unit
     24 tests against your code, you can easily verify that the logic of individual units is correct.
     25 Running unit tests after every build helps you to
     26 quickly catch and fix software regressions introduced by code changes to your app.
     27 </p>
     28 
     29 <p>A unit test generally exercises the functionality of the smallest possible unit of code (which
     30 could be a method, class, or component) in a repeatable way. You should build unit tests when you
     31 need to verify the logic of specific code in your app. For example, if you are unit testing a
     32 class, your test might check that the class is in the right state. Typically, the unit of code
     33 is tested in isolation; your test affects and monitors changes to that unit only. A
     34 <a href="http://en.wikipedia.org/wiki/Mock_object" class="external-link">mocking framework</a>
     35 can be used to isolate your unit from its dependencies.</p>
     36 
     37 <p class="note"><strong>Note:</strong> Unit tests are not suitable for testing
     38 complex UI interaction events. Instead, you should use the UI testing frameworks, as described in
     39 <a href="{@docRoot}training/testing/ui-testing/index.html">Automating UI Tests</a>.</p>
     40 
     41 <p>For testing Android apps, you typically create these types of automated unit tests:</p>
     42 
     43 <ul>
     44 <li><strong>Local tests:</strong> Unit tests that run on your local machine only. These tests are
     45 compiled to run locally on the Java Virtual Machine (JVM) to minimize execution time. Use this
     46 approach to run unit tests that have no dependencies on the Android framework or have dependencies
     47 that can be filled by using mock objects.</li>
     48 <li><strong>Instrumented tests:</strong> Unit tests that run on an Android device or emulator.
     49 These tests have access to instrumentation information, such as the
     50 {@link android.content.Context} for the app under test. Use this approach to run unit tests that
     51 have Android dependencies which cannot be easily filled by using mock objects.</li>
     52 </ul>
     53 
     54 <p>The lessons in this class teach you how to build these types of automated unit tests.</p>
     55 
     56 <h2>Lessons</h2>
     57 <dl>
     58   <dt><strong><a href="local-unit-tests.html">
     59 Building Local Unit Tests</a></strong></dt>
     60     <dd>Learn how to build unit tests that run on your local machine.</dd>
     61   <dt><strong><a href="instrumented-unit-tests.html">
     62 Building Instrumented Unit Tests</a></strong></dt>
     63     <dd>Learn how to build unit tests that run on an Android device or emulator.</dd>
     64 </dl>