| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 1 | page.title=Building Effective Unit Tests |
| 2 | page.tags=testing,androidjunitrunner,junit,unit test |
| 3 | |
| 4 | trainingnavtop=true |
| 5 | startpage=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 |
| 23 | tests against your code, you can easily verify that the logic of individual units is correct. |
| 24 | Running unit tests after every build helps you to |
| 25 | quickly 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 |
| 29 | could be a method, class, or component) in a repeatable way. You should build unit tests when you |
| 30 | need to verify the logic of specific code in your app. For example, if you are unit testing a |
| 31 | class, your test might check that the class is in the right state. Typically, the unit of code |
| 32 | is 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> |
| 34 | can 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 |
| 37 | complex 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 |
| 44 | compiled to run locally on the Java Virtual Machine (JVM) to minimize execution time. Use this |
| 45 | approach to run unit tests that have no dependencies on the Android framework or have dependencies |
| 46 | that can be filled by using mock objects.</li> |
| 47 | <li><strong>Instrumented tests:</strong> Unit tests that run on an Android device or emulator. |
| 48 | These 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 |
| 50 | have 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"> |
| 58 | Building 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"> |
| 61 | Building 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> |