Test example of Android unit test on activity
The previous article has introduced the function and simple examples of unit testing. If you don't know, you can read the previous Android unit testing - function and simple examples first.
This article mainly introduces testing in common activities.
Test of acitivity
For activity, we have roughly two test requirements:
1. After the activity starts normally, check whether the interface layout is correct, including the click events of view.
2. You need to complete the deployment of various data before the activity starts, and then view the effect of the activity.
For these two requirements, the author gives two examples:
1. Check whether the button and textview in a layout are correct.
2. The string is dynamically obtained from the network and displayed on the activity interface, and the URL of the picture is passed by intent.
Environment deployment
First, import the package of expresso core, as follows:
Of course, in the current project architecture, this package has been automatically imported, so you don't need to import it yourself. The packages automatically imported in the author's project are as follows:
The project structure is as follows:
Test of layout view:
Main test logic:
1. First, use activitytestrule to initialize the activity you want to test.
2. Write a test method to test whether the view is what we expect.
The logic of the two test methods is as follows:
textViewtest():
Find TV with ID in activity_ simple_ View of view and check whether its text is "111".
buttontest():
Find BTN with ID in activity_ simple_ View of view and check whether its text is "222". Then execute the click event. The logic of the click event is set in oncreate of activity, and the text of textview is set to 777. After the click event is executed, continue to test whether the text of textview is "777" in the test method.
Readers may read that they are very unfamiliar with the test of view. Don't worry. Here we mainly need to understand the logic of the test. The author will explain various test methods of view in detail in the next article.
Get activity test of network string:
Don't forget to add network permissions in androidmanifest.
The main logic of this activity is to receive the intent, then get the URL, and then get the string of the URL through the network and display it on the textview.
Main test logic:
First, define the activitytestrule to determine which activity to use.
Different from the previous example, we will override the two methods of activitytestrule, getactivityintent () and beforeactivitylaunched (). As the name suggests, one is to set the intent obtained by the activity, and the other is to set the preparations before the activity starts.
Here, the author sets the URL to be passed in getactivityintent() and the network acquisition method set in beforeactivitylaunched().
Some readers may wonder why the method of network acquisition is not set by default, but set through setclient()?
This makes it easier for us to test. In a formal project, we may need to add log and other operations to the code, but we generally do not modify the formal code, but we can inherit it, rewrite some methods, and then put it where the test needs to be.
Here, we can inherit the httpurlconnectionclient class, and then use setclient () as the network acquisition method for the inherited subclass.
summary
The usage of activity is roughly the same. If you have more needs, you can check the reference of the official activitytestrule.
The link is as follows: https://developer.android.google.cn/reference/android/support/test/rule/ActivityTestRule.html
In the first use method, the test of view is designed. Due to the large space, this article cannot be described in detail. The author will explain it in the next article.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.