As you learn more about Android and JUnit, sort out the classes of Android and JUnit as follows:
- Test-testcase-Androidtestcase
- Test-testcase-Instrumentationtestcase
- Test-testsuite-Instrumentationtestsuite
- Testlistener -- basetestrunner-Androidtestrunner
- Instrumentation-Instrumentationtestrunner
The above five routes are also a process of continuous learning. We feel that we have clearly resolved the first four routes, and the last one does not seem to be clear, later, I checked a lot of such information and explained the instrumentation again.
Each Android Application runs in its own process. instrumentation kills the current application and restarts the application (restarts the process with instrumentation ). Instrumentation provides us with a handle in the application context. With this handle, we can gain insight into the application to verify the test assertions, we can also use it to write test cases that are more underlying than interface tests. It should be emphasized that instrumentation cannot capture the bugs in the UI.
Android has extended three Instrumentation-related classes based on JUnit:
Class |
Description |
| Instrumentationtestcase |
It extends the tastcase in JUnit and provides an interface getinstrumentation () to obtain the instrumentation class. This class can be extended according to your own needs. For example, in the test, you may start an avtivity and send a key event. To complete the test, instrumentation needs to inject it into tastcase. |
| Instrumentationtestrunner |
It is extended based on Instrumentation. It injects itself into each test case itself, and the test cases need to be grouped into an appropriate instrumentationtestrunner to run. |
| Instrumentationtestsuite |
It extends JUnit testsuite. Its main function is to ensure that before each testcase runs, instrumentation can be injected into testcase, and instrumentationtestrunner must use instrumentationtestsuite. |
The above instructions are from the web page instrumentation testing (in English). We recommend you read them here.
JUnit's usage experience
JUnit adopts the test-driven development method. That is to say, you should write the test code before development. It is mainly used to describe how the tested code is used and handle errors. Then you can write the code, and gradually test the code in the test code until it passes completely in the test code.
I checked whether I felt that some of them were not in line with the programmer's thinking habits (first writing code and then debugging). It is true that JUnit is a "Subversion" of the programmer's thinking habits ". Here I also feel that it is difficult to do so. Why? Before a "horse" is completely designed, how should we determine how the horse will run in the future? And even if we define how the "horse" will "run" in the future, what should we do if the "horse" is changed during implementation? After all, a person cannot have two roles at the same time, otherwise he may not know which role he is currently playing!
Here, I will explain myself.How to Use JUnit "error"This may be in conflict with the purpose of JUnit test-driven development, but it can effectively reduce bugs. At its core, JUnit completely separates the source code from the test code and uses the test code as a separate program. The methods described earlier integrate the source code with the test code. Because the importance of the source code is greater than that of the test code, the test code is often incomplete and the structure is unclear, in this way, unit testing for programmers is incomplete. JUnit is used for complete unit tests to test the running results of some of the current Code in each "environment.
Postscript
After half a month of study, I finally completed in-depth analysis of Android and JUnit, and gave a comprehensive introduction to all aspects of using JUnit in Android. During the blog writing process, I also try my best to share with you every problem I encountered here and try my best to resolve it clearly. If you have read about the deficiencies, errors, and omissions, you are welcome to leave a message on your blog.