1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
<p>
This sample contains a test package that demonstrates
<a href="../../../reference/android/content/ContentProvider.html">ContentProvider</a> testing
and <a href="../../../reference/android/app/Activity.html">Activity</a> testing. It is a test
of the <a href="../NotePad/index.html">Note Pad</a> sample application.
</p>
<h2>Test Package</h2>
<p>
The test package project is located in the <code>tests/</code> subdirectory of the
Note Pad project. It provides an example of the structure of a test package project,
and it contains a example of a <a href="AndroidManifest.html">
<code>AndroidManifest.xml</code></a> file for a test package.
The package uses <a href="../../../reference/android/test/InstrumentationTestRunner.html">
<code>InstrumentationTestRunner</code></a> as its test runner.
</p>
<p>
The <a href="AndroidManifest.html">manifest</a> for this test package contains an
<code><instrumentation></code> element that links the test package
with the application under test. Specifically, the element's <code>android:name</code>
attribute specifies that Android should use <code>InstrumentationTestRunner</code> to
control the classes in this package. The <code>android:targetPackage</code> attribute
targets <code>com.android.example.notepad</code> as the Android package that contains the
application under test.
</p>
<h2>ContentProvider Test</h2>
<p>
The ContentProvider test suite is contained in the single test case class
<a href="src/com/example/android/notepad/NotePadProviderTest.html">
<code>NotePadProviderTest</code></a>. This class extends
<a href="../../../reference/android/test/ProviderTestCase2.html"><code>ProviderTestCase2</code>
</a>, which extends the JUnit
<a href="../../../reference/junit/framework/TestCase.html"><code>TestCase</code></a> class and
also provides methods and objects that facilitate ContentProvider testing.
</p>
<p>
Although the sample test suite for ContentProvider testing uses a single test case class,
you may use as many test case classes as you wish, and each class can contain as many
test methods as you wish.
</p>
<h3>ContentProvider test methods</h3>
<p>
<code>NotePadProviderTest</code></a> contains the following methods:
</p>
<ul>
<li>
Test setup: The <code>setUp()</code> method re-initializes the test environment
(the test fixutre) before each test is run. The call to <code>super.setUp()</code>
creates a test context and test resolver, and creates a local instance of the
provider under test. By creating the provider, the super constructor also tests the
provider's <code>onCreate()</code> method, which is called during the provider's
initialization.
<p>
The <code>setUp()</code> method then gets a handle to the local provider's
underlying database. This is used to set up test data in succeeding tests.
</p>
</li>
<li>
Test tear down: The <code>tearDown()</code> method calls <code>super.tearDown()</code>,
which cleans up left-over object references to prevent memory leaks.
</li>
<li>
Test database creation: The <code>insertData()</code> helper method adds test data
to the local provider. Individual test methods call <code>insertData()</code>
when they need to set up a test fixture that contains valid data. This data is not
inserted during <code>setUp()</code>, because some tests expect an empty database.
</li>
<li>
Content provider unit tests: The test package shows how to unit
test a content provider's getType, query, insert, delete, and update
methods against all the content Uris that the provider offers.
Only a subset of all the possible unit tests is included.
</li>
</ul>
<h2>Activity Test</h2>
<p>
The Activity test suite is contained in the single test case class
<a href="src/com/example/android/notepad/NotePadActivityTest.html">
<code>NotePadActivityTest</code></a>. This class extends
<a href="../../../reference/android/test/ActivityInstrumentationTestCase2.html">
<code>ActivityInstrumentationTestCase2</code>, which provides the JUnit test framework and
also provides instrumentation methods that facilitate Activity testing.
</p>
<p>
The <code>NotePadActivityTest</code> test class contains a single assertion,
<code>testActivityTestCaseSetUpProperly()</code>. This assertion calls
<a href="../../../reference/android/test/ActivityInstrumentationTestCase2.html#getActivity()">
<code>Activity.getActivity()</code> and then fails if the result is <code>null</code>,
indicating that the test was unable to start the Activity under test.
</p>
<img
src="../images/sample_note.png"
alt="The user interface for a single note in the Note Pad sample app"
style="height:230px"/>
<img
src="../images/sample_notepadtest_junit.png"
alt="Results of a successful run of the NotePadTest test package in Eclipse"
style="height:230px"/>
|