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