DEV Community

Cover image for How to write tests for an android app?
Sdu
Sdu

Posted on

How to write tests for an android app?

I have an app that I built recently the repo. Its even live on the play store but I'm worried that there's not even a single test written for it. Even unit tests will bring peace to my heart and the problem is...ever since I've started android development, I've never had to write tests, web dev included.

In my defence: I'm self taught, I have about 4 years of experience in both web and android development and with most resources I use, its about solving a problem, and rerunning a piece of code always made me feel like I'm testing it already and I pass.

This is a very bad habit apparently and it makes me doubt myself as a developer. I'd really appreciate a heads up of any sort, whether a reply here, link to resources or a pull request as a jump start. Thank you

Latest comments (4)

Collapse
 
subbramanil profile image
Subbu Lakshmanan • Edited

Hey Buddy,

I wanted to get the 'Hacktoberfest' t-shirt, but haven't spent enough time on PRs.

Since I have already submitted one PR to your repo earlier this month, I put my focus on your repo to submit a few more PRs to complete the challenge.

My Plan is,

  • [x] PR-2: CircleCI integration
  • [x] PR-3: Fix Warnings using Codacy and Android Lint
  • [x] PR-4: Espresso UI Testing
  • [ ] PR-5: Firebase Test Lab/Code Coverage
  • Bonus: Fix Readme
    • Adds Circle CI Build status CircleCI in the Readme. You can update this once you create a project in CircleCI.

Still deciding on which one to do for the PR-5. Do let me know if you have a preference. I will try to do that.

Collapse
 
sduduzog profile image
Sdu

Hi again,

I've seen the PRs, I'm overwhelmed and impressed. Not really sure about my preference, as I'm just being introduced to some concepts i.e. CircleCI. Best to do for me here is to is to walk on your footsteps and read up on what I'm behind on. What I like about tests is that the concept of it starts to become similar with different tech stacks. And I've always wondered how repos had those status labels on their READMEs

Collapse
 
subbramanil profile image
Subbu Lakshmanan • Edited

Hi,

Submitted PR-5: Grid View for the apps on Home screen.

P.S: I spent quite a much amount of time in integrating Firebase Test lab with CircleCI, but it requires much more work in the Google Cloud Console and Firebase Console outside of code. I'm about to finish it, save for an annoying error which I think is related to billing. I may get back once I have some more time.

And also it may not help you unless testing on different devices on the cloud is a priority for you.

Collapse
 
subbramanil profile image
Subbu Lakshmanan • Edited

Hey Buddy,

First of all, Don't doubt yourself as a developer just because you haven't written test cases yet. IMO not many people start their career writing test cases for each of their projects. Most of them start their testing the app by hand as you did. I did the same thing when I started. Although JUnit test cases are available for quite a long time, I haven't used them in my initial projects. It took some time for me to understand the concepts, practice and implement in my projects. i.e., how to write proper test cases.

Writing test cases differs from project to projects. Some people would be perfectly happy with no test cases on their personal projects since there's nobody to enforce them. Some people religiously write test cases in their personal projects, since it may be the only opportunity to learn and apply testing concepts.

Also, you would be surprised by how many apps & services running in the real world without enough test cases. Most people won't accept, but in my experience, I have seen projects that don't have a single test case running successfully in the real world for a number of years.

I'm not advocating here, it's okay not to write test cases. I'm just saying don't beat yourself up since it's a new concept to you. Also don't be afraid to take the first steps into writing test cases. Initially, it will suck, but over time you will gain an understanding of having good test cases.

Initially, you may not understand why to write a test case that you know for sure it will work. And probably much easier to test by re-running the code. That's where most people stop. IMO, the value of test cases comes when you try to refactor the code. Running the test cases will give you confidence that you didn't introduce any new bugs.

You can find a number of articles on Android Testing. And you can browse through the Google GitHub sample projects to understand how to do.

I have browsed through your code, ran it on the simulator. Your code looks pretty neat. Kudos to you!! One thing I can recommend is to review the Android lint warnings.

In Android Studio, you can see all the lint warnings from the menu Analyze --> Inspect Code.

Also, your app gave me a good idea for one of my side-projects. As thanks, I have submitted a PR. Check it out.