Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #1497: Set up code coverage for Robolectric tests #2466

Closed
wants to merge 10 commits into from
Closed

Fix issue #1497: Set up code coverage for Robolectric tests #2466

wants to merge 10 commits into from

Conversation

peculiaruc
Copy link
Contributor

@peculiaruc peculiaruc commented Jan 12, 2021

Explanation

Code Coverage SetUp: Configure the build,gradle file to use Jacoco to generate Code coverage report.
Choose coverage runner as Jacoco in Edit Configuration in Android Studio

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

@BenHenning
Copy link
Sponsor Member

@peculiaruc thanks for uploading this. A couple of things to note:

  1. When creating PRs that aren't yet ready for review/production, we should mark them as 'drafts'
  2. When giving instructions, try to list them as exact steps in a bullet or numbered list, e.g. 1. Do X, 2. Navigate to menu Y and click Z, etc. to keep things as deterministic as possible.

Going to try to run the code coverage now and see how it goes.

@anandwana001
Copy link
Contributor

Adding a screenshot of what I got after running jacocoTestDebugUnitTestResult gradle task.

Screenshot 2021-01-12 at 23 22 46

@peculiaruc
Copy link
Contributor Author

@peculiaruc thanks for uploading this. A couple of things to note:

1. When creating PRs that aren't yet ready for review/production, we should mark them as 'drafts'

2. When giving instructions, try to list them as exact steps in a bullet or numbered list, e.g. 1. Do X, 2. Navigate to menu Y and click Z, etc. to keep things as deterministic as possible.

Going to try to run the code coverage now and see how it goes.

Noted. Thank

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Jan 13, 2021

Adding a screenshot of what I got after running jacocoTestDebugUnitTestResult gradle task.

Screenshot 2021-01-12 at 23 22 46

This shows TestDebugUnitTestResult. it didn't give the result i am looking for which is showing the percentage of code covered. I used createDebugCoverageReport, the HTML results this

image

@anandwana001
Copy link
Contributor

Screenshot 2021-01-13 at 17 09 04

@peculiaruc
Copy link
Contributor Author

Screenshot 2021-01-13 at 17 09 04

Yes this is the result i am looking for. Thanks

@anandwana001
Copy link
Contributor

Just an update, this result is not from this pr, I tried something else a bit. Will try this pr also.

@peculiaruc
Copy link
Contributor Author

Just an update, this result is not from this pr, I tried something else a bit. Will try this pr also.

I see, that thing you tried is what i am missing, i think

@BenHenning
Copy link
Sponsor Member

Thanks for following up on this @anandwana001. Haven't had a chance to run this myself.

@BenHenning
Copy link
Sponsor Member

To capture some of my findings when looking into this today:

  • I couldn't get Jacoco to actually report within Android Studio correct results. However, I made a few changes to your configuration and I was able to get generated HTML results using the CLI command: ./gradlew :domain:jacocoTestReport. Note that I needed to change the jacocoTestReport to use a mainSrc of "$project.projectDir/src/main/java" (since that matches our directory structure), and I commented out the fileFilter list for now to avoid filtering anything out. This works well for CLI, but not for AS itself.
  • Running code coverage from within AS using Idea instead of JaCoCo does produce results
  • The results from JaCoCo seem incomplete: they aren't covering suspend functions correctly, so the coverage is under-reporting
  • I started looking into trying to get coverage to work with Bazel instead since this is starting to feel extremely Gradle-specific and we're actively moving off of Gradle, but unfortunately it appears that Bazel does not support coverage for android_local_test and the team is actively working to introduce new Starlark rules for Android but we may need to wait a few months

This leads us to getting a bit stuck. I think the correct way to do this is to build code coverage support into Bazel while we wait for the new rules (especially since the new rules won't support coverage out of the box), but I just don't have the bandwidth to do that right now. Given that, I think we can still use JaCoCo in the interim:

  • I suggest using the CLI version since that does generate a report
  • I suggest proceeding with writing up the documentation for helping other team members run code coverage reports, interpret them, and turn the missing coverage into good tests by mapping lines of code to behaviors

That second bit is the most important thing that we can still create. While we may change the means by which we generate the reports, being able to generate them is still a fundamental first step.

Does this sound good to you @peculiaruc?

@BenHenning BenHenning assigned peculiaruc and unassigned BenHenning Jan 13, 2021
@peculiaruc
Copy link
Contributor Author

To capture some of my findings when looking into this today:

  • I couldn't get Jacoco to actually report within Android Studio correct results. However, I made a few changes to your configuration and I was able to get generated HTML results using the CLI command: ./gradlew :domain:jacocoTestReport. Note that I needed to change the jacocoTestReport to use a mainSrc of "$project.projectDir/src/main/java" (since that matches our directory structure), and I commented out the fileFilter list for now to avoid filtering anything out. This works well for CLI, but not for AS itself.
  • Running code coverage from within AS using Idea instead of JaCoCo does produce results
  • The results from JaCoCo seem incomplete: they aren't covering suspend functions correctly, so the coverage is under-reporting
  • I started looking into trying to get coverage to work with Bazel instead since this is starting to feel extremely Gradle-specific and we're actively moving off of Gradle, but unfortunately it appears that Bazel does not support coverage for android_local_test and the team is actively working to introduce new Starlark rules for Android but we may need to wait a few months

This leads us to getting a bit stuck. I think the correct way to do this is to build code coverage support into Bazel while we wait for the new rules (especially since the new rules won't support coverage out of the box), but I just don't have the bandwidth to do that right now. Given that, I think we can still use JaCoCo in the interim:

  • I suggest using the CLI version since that does generate a report
  • I suggest proceeding with writing up the documentation for helping other team members run code coverage reports, interpret them, and turn the missing coverage into good tests by mapping lines of code to behaviors

That second bit is the most important thing that we can still create. While we may change the means by which we generate the reports, being able to generate them is still a fundamental first step.

Does this sound good to you @peculiaruc?

Yes please. Thanks. I going to implement the changes, then finish up the documentation

@peculiaruc
Copy link
Contributor Author

Just an update, this result is not from this pr, I tried something else a bit. Will try this pr also.

I see, that thing you tried is what i am missing, i think

@peculiaruc peculiaruc closed this Jan 15, 2021
@peculiaruc
Copy link
Contributor Author

peculiaruc commented Jan 15, 2021

Please i mistakenly closed the Pr before. sorry about that

@peculiaruc peculiaruc reopened this Jan 15, 2021
@BenHenning
Copy link
Sponsor Member

BenHenning commented Jan 16, 2021

Ack thanks @peculiaruc. Assigning this back over to you since it seems like per your last response the next steps here are on your plate. Please let me know if you need my input on anything or run into any problems.

Also, I think it would be really helpful if you compiled your findings & analyses in a document & shared that in this PR since it will help bring context to future team members.

@BenHenning BenHenning removed their assignment Jan 16, 2021
@peculiaruc
Copy link
Contributor Author

Ack thanks @peculiaruc. Assigning this back over to you since it seems like per your last response the next steps here are on your plate. Please let me know if you need my input on anything or run into any problems.

Also, I think it would be really helpful if you compiled your findings & analyses in a document & shared that in this PR since it will help bring context to future team members.

Noted . I am on it

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Jan 20, 2021

1.The document below contains my findings about running code coverages to determine the percentage of the app code covered by each unit test.
2. It also contain steps to configure the android studio to get code coverage reports.

https://docs.google.com/document/d/1oSIY-12lGl28MVgGxs0mbuJFTORkjGqypeiOGq95jLc/edit

It also await review from reviewers.

@BenHenning
Copy link
Sponsor Member

Thanks @peculiaruc! @anandwana001 can you take a first pass on the document?

@BenHenning
Copy link
Sponsor Member

BenHenning commented Jan 29, 2021

Thanks for clarifying @anandwana001. Your plan SGTM to confirm that code coverage is working. Deferring to you to drive this PR to completion (please loop me in if needed). @peculiaruc feel free to continue to reach out to me if you have questions, otherwise Akshay will probably be your main point of contact as part of finishing this PR.

@BenHenning BenHenning removed their assignment Jan 29, 2021
@BenHenning
Copy link
Sponsor Member

@peculiaruc are the next steps clear to you from Akshay's comment above?

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Jan 29, 2021 via email

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Feb 1, 2021 via email

@anandwana001
Copy link
Contributor

@peculiaruc
Copy link
Contributor Author

Ack thanks @peculiaruc. Assigning this back over to you since it seems like per your last response the next steps here are on your plate. Please let me know if you need my input on anything or run into any problems.

Also, I think it would be really helpful if you compiled your findings & analyses in a document & shared that in this PR since it will help bring context to future team members.

Done

@peculiaruc
Copy link
Contributor Author

1. Match the code coverage result of `jacoco` and android studio. Are we getting the same result or not.
2. In the configuration (Android Studio), we also need to mention the package in the code coverage section. Else we will get the 0% code covered.
3. Added a screenshot showing 20-25% lines are not getting covered.
4. Red and Green colour in the editor showing what has covered and what not.
5. According to these result, we need to update those lines which are in red.
6. Need to figure out a way, how to do this without using Android Studio if `jacoco` doesn't give the same result.

Describing step by step why we need to do this now and what can be the multiple parts of the code coverage:

1. From point 1 above - We need to confirm the result produced from `jacoco` and android studio code coverage tool so that we have a strong confirmation that we are on right track in implementing the `jacoco`.

2. From point 2 above - just an android studio code coverage set up steps, we need this in documentation

3. From point 5 above - Updating/Fixing those red lines for a single file and checking if those lines are getting covered in code coverage or not will help us to understand that our code coverage set up is working correctly. or we can say it like this `Our code coverage set up will give the correct result for both the line of code which is covered and not covered.`

4. From point 6 above - If the code coverage result of `jacoco` and Android Studio is different then we need to check either we have mistaken any set up while setting `jacoco` or does Android Studio works in a different way for covering these line.

The single goal of this PR:

Code coverage setup correctly and giving correct results for both the cases when line are covered and when lines are not covered.

Future Goal after merging this PR:

1. Fix all the code which is not covered and reach up to 90% code coverage
   
   1. We do need documentation work for this on how to fix
   2. Once documentation is done, file multiple issues to reach 90% result
   3. How can we block code coverage to run over auto-generated files

2. How we can do the code coverage in GitHub actions (CI).

3. How Bazel can do the code coverage for us.
   
   1. Set up code coverage using Bazel and remove Gradle

Done.

PTAL
https://gist.github.com/peculiaruc/2e63953249c1af8d731246f737fec588

@anandwana001
Copy link
Contributor

anandwana001 commented Feb 3, 2021

@peculiaruc have you checked my comment here - https://gist.github.com/peculiaruc/2e63953249c1af8d731246f737fec588#gistcomment-3616265

Also, include the AudioTest file mentioned in the gist if you are fixing that file for code coverage.
Also, please remove the .idea/gradle.xml from this PR as we don't change that file.
Please check why the GitHub actions are not running, I guess something is blocking to run the GitHub actions.

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Feb 4, 2021 via email

@anandwana001
Copy link
Contributor

Making a draft PR doesn't stop the GitHub actions to run.

  1. I suggest remove the changes made in .idea/gradle.xml
  2. The possible reason I see here is that we are having the update in the Gradle file, this somehow breaks our actions.

@anandwana001 anandwana001 marked this pull request as ready for review February 8, 2021 04:28
@anandwana001
Copy link
Contributor

There are some conflicts in this PR, please resolve them and see if GitHub actions run.

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Feb 8, 2021 via email

.idea/gradle.xml Outdated
@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont' delete this file, just remove it from this PR only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted

@peculiaruc
Copy link
Contributor Author

There are some conflicts in this PR, please resolve them and see if GitHub actions run.

Conflicts are resolved

@anandwana001
Copy link
Contributor

Looks like pr has a lot of unwanted changes, it's showing 4k files.

@peculiaruc
Copy link
Contributor Author

Looks like pr has a lot of unwanted changes, it's showing 4k files.

Yes this happened when i merge develop into my branch. Now my branch is up to date but static checks didn't still pass

@BenHenning
Copy link
Sponsor Member

@peculiaruc it looks like you copied in an entirely other project into your branch. None of these files seem relevant to Oppia Android. Did you accidentally merge another project with your Oppia Android repository then push that?

@peculiaruc
Copy link
Contributor Author

peculiaruc commented Feb 11, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants