In this article, I will explain the best practices for writing test cases based on my actual experience. In this article, I will use Espresso coding, but they can be used for unit tests and instrumentation tests. For the above purpose, let's study a news application. The following content is purely fictional, any similarity is purely coincidental:P A news app should have the following activities.
This app is functional enough, let's delve into the test case written for the news list activity. This is the code I wrote the first time.
Think carefully about what to test In the first test case testClickOnAnyNewsItem(), if the server does not return a success message, the test case will return a failure because the recycler view is not visible. But the purpose of this test case is not like this. Regardless of whether the case is PASS or FAIL, its top requirement is that the recycler view is always visible. If for some reason, the recycler view is not visible, then the test case should not be considered FAILED. The correct test code should look like the following.
A test case should be complete in itself When I start testing, I usually test the activities in the following order:
Because I tested the language selection activity first, before testing the NewsList activity, one of the languages was always selected. But when I tested the News List activity first, the test cases started returning errors. The reason is simple - without selecting a language, the recycler view is not displayed. Note, the order in which the test cases are executed must not affect the test results. Therefore, before running the test case, the language selection must be saved in the shared preferences. In this case, the test case is independent of the test of the language selection activity.
Avoid conditional code in test cases Now in the second test case testChangeLanguageFeature(), we get the number of languages selected by the user, and based on this number, we write if-else conditions to test. But if-else conditions should be written in your code, not in the test code. Each condition should be tested separately. Therefore, in this case, instead of writing only one test case, you need to write two test cases as shown below.
Test cases should be independent of external factors In most applications, we interact with external networks or databases. When a test case is run, it can send a request to the server and get a response of success or failure. But it cannot be considered that the test case failed because of the failure information obtained from the server. Think of it this way - if the test case fails, then we modify the client code so that the test case passes. But in this case, do we need to make any changes on the client side? - NO. But you should not be able to completely avoid testing network requests and responses. Since the server is an external proxy, we can imagine a scenario where it sends some wrong responses that may cause the program to crash. Therefore, the test cases you write should cover all possible responses from the server, even responses that the server will never send. This will cover all the code and ensure that the application can handle all responses without crashing. Writing test cases correctly is just as important as writing those test codes. Thanks for reading this article. I hope it helps you write better test cases. You can contact me on LinkedIn. You can also read my other articles here. For more information, please follow us and you will be notified when we publish new articles. |
<<: Understanding Android security mechanisms
>>: 7 ways to express your love to developers on Valentine's Day
On the one hand, the media is constantly promotin...
On the 23rd, the reporter learned from the Sichua...
As mobile phone functions become increasingly pow...
There is an ancient myth that the Monkey King was...
Previously, the resignation of LeTV Auto co-found...
In the cartoon "Wilder Robots", the rob...
On the evening of April 15, the first online conc...
Since the new Land Management Law was implemented...
Audit expert: Zheng Yuanpan, professor at Zhengzh...
Is Oumuamua an alien spaceship? Scientists have p...
Starbucks, which has been doing bad things for a ...
It has been more than 5 years since the mini prog...
Recently the tree has turned green again, and its...
The so-called points system is based on points. B...
I must state in advance that this is just my pers...