Skip to content

Add more unit tests to improve test coverage consistency#9684

Merged
alecslupu merged 14 commits intodecidim:developfrom
mainio:chore/improve-coverage-consistency
Oct 30, 2023
Merged

Add more unit tests to improve test coverage consistency#9684
alecslupu merged 14 commits intodecidim:developfrom
mainio:chore/improve-coverage-consistency

Conversation

@ahukkanen
Copy link
Copy Markdown
Contributor

@ahukkanen ahukkanen commented Aug 12, 2022

🎩 What? Why?

UPDATE: I think the root cause is with the merging of the reports and a workaround for that is provided at #9686. This is still relevant as there can be still small fluxuation with the coverage between different runs depending on the execution path (some code may not be run at all test runs due to the randomized run order).

UPDATE 2: The Codecov report can still fail due to the GitHub action failing to upload the report of some of the workflow runs to Codecov. There are some workarounds suggested at #9832 as it seems Codecov seems unwilling to add retry option to their GitHub action.

It has been a consistent issue over the years that the results of the Codecov reports cannot be trusted because they report seemingly random coverage changes for the diffs and whole PRs. This is rather bothering for the developers and can cause extra work for newcomers as they start to investigate why the coverage was changed with their PRs.

I started to investigate this issue and found this very useful piece of documentation:
https://docs.codecov.com/docs/unexpected-coverage-changes

It says the following:

There are a number of reasons that cause line coverage to change unexpectedly. These can be due to:

  1. Adding or removing tests.
  2. Failing to upload coverage reports, or a different number of reports between head and base
  3. Time-sensitive tests (e.g., one case before 2pm UTC, a different case after 2pm UTC).
  4. Missing coverage reports or failed builds.
  5. Dependencies changed resulting in a different execute plan.
  6. Encrypted variables may prevent some execution paths.
  7. In the case of changes to a PR, the base or head commits may not have had an uploaded report, or may not be the commit you expect
  8. Different error handling paths: sometimes exception handlers are tested and some are not, oftentimes the root of "flaky" tests
  9. Due to long running test builds or dynamic test selection, you are intentionally not running all tests on every commit. If this is the case, please check out our feature Carryforward Flags

Especially those bold sections are interesting regarding Decidim as we have:

  • Some functionality that can behave differently during different times
  • Randomized test running order which causes multiple different execution paths
  • Different run sets for different modules which can cause different specs to be run for different changesets

I started to investigate the coverage reports when they are failing and it seems that most of the times the reason is that some methods are not just executed during every test run while they were executed during previous runs.

So I believe we can fix this issue by increasing the unit test coverage which would ensure that all methods are executed on all test runs consistently.

Another thing I am suspecting that can happen when only a subset of the tests are run is e.g. if we execute the accountability tests but they call some methods from the core during their run, but the rest of the methods in the core class were not called during that run, it may also produce weird coverage results.

This will be a long effort but I try to work on this occasionally as we bump into these issues by adding more unit specs to those methods that are reported as not covered by Codecov.

Testing

We just need to consistently add more unit specs for those methods showing up in the coverage reports.

@ahukkanen ahukkanen added the type: internal PRs that aren't necessary to add to the CHANGELOG for implementers label Aug 12, 2022
@ahukkanen ahukkanen changed the title Improve test coverage consistency Add more unit tests Aug 14, 2022
@ahukkanen ahukkanen changed the title Add more unit tests Add more unit tests to improve test coverage consistency Aug 15, 2022
@andreslucena
Copy link
Copy Markdown
Member

@ahukkanen can we retake this PR? If you don't have the time to allocate to this let me know and me or @alecslupu can take it out of your hands 😄

@ahukkanen
Copy link
Copy Markdown
Contributor Author

@andreslucena This is kind of problematic due to the ongoing redesign process which originally put this on hold.

The problem is e.g. if we need to add more specs to the cells, but the cells are going to be changed by redesign.

So I'd keep this on hold for the time being until redesign is merged back to develop.

Note that this is not the only reason the codecov action is still occasionally failing. I have previously posted my findings on that issue at #9832.

This would only help in those runs where the diff is some tenths of a percentage, e.g. 0.20% diff. But this would not solve the problem completely.

@andreslucena
Copy link
Copy Markdown
Member

So I'd keep this on hold for the time being until redesign is merged back to develop.

The time has come :)

Since last week with the #11685 merge, we're not expecting new big PRs and the folders/file structure should be (finally!) stable again, so this might be a good time to retake this.

If you don't have the time, please let me know and I can take this over.

@ahukkanen ahukkanen marked this pull request as ready for review October 19, 2023 15:48
@ahukkanen
Copy link
Copy Markdown
Contributor Author

Since last week with the #11685 merge, we're not expecting new big PRs and the folders/file structure should be (finally!) stable again, so this might be a good time to retake this.

If you don't have the time, please let me know and I can take this over.

Go ahead and feel free to continue on this work.

I fixed the merge conflicts and specs, so you can now review this as-is or create a new branch based on this to continue the work.

To recap the idea of this PR:

  1. The original idea with this PR was to improve the unit test coverage, so that we could potentially have multiple workflows to cover the same code and reduce the fluctuation of the coverage reports. I did not get very far with this, I believe there are still many places where we could improve (and discover more bugs while doing that). A good place to start is to inspect the Codecov reports and see what is not currently covered, especially on those runs where upload failed (i.e. the Codecov workflow failed).
  2. This PR will not fix the Codecov issue, even if it was fully implemented. The main issue is that many times one (or two) of the workflows fail to upload the coverage report due to a problem at Codecov's end. This could be solved by retrying the sending of the report but Codecov does not provide this option in their GitHub action, although there are some workarounds listed for that at Code coverage uploads fail occasionally at the CI #9832.

@alecslupu alecslupu self-assigned this Oct 23, 2023
@alecslupu alecslupu requested a review from a team October 30, 2023 05:51
Copy link
Copy Markdown
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

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

LGTM

@alecslupu alecslupu merged commit bf567bb into decidim:develop Oct 30, 2023
@ahukkanen ahukkanen deleted the chore/improve-coverage-consistency branch October 30, 2023 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: internal PRs that aren't necessary to add to the CHANGELOG for implementers

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants