Skip to content

Conversation

@dijidiji
Copy link

@dijidiji dijidiji commented May 7, 2025

Description

Code coverage will be helpful for unit testing to get an idea of how much of the codebase is being tested. This draft PR includes CMake presets for Linux/WSL and Mac, plus custom targets that perform the coverage collection process with LCOV or gcovr. Efforts to get it working with MSYS have been unsuccessful so far (any help here would be greatly appreciated!)

Expands on #106.

For anyone who'd like to try it:

  1. Install coverage tool:
    For macOS install gcovr with homebrew: brew install gcovr.
    For Linux/WSL use your package manager to install lcov, e.g. apt: apt install lcov
  2. Move to the cmake project directory cd projects/cmake
  3. Select the preset applicable to your platform cmake --preset [LinuxCoverage | macOSCoverage]
  4. Build the unit test executable cmake --build build/ --target skunit_tests

Capture coverage (macOS):

  1. Run the test executable to generate coverage data ../../bin/skunit_tests
  2. Build the capture-coverage target to capture the generated coverage data cmake --build build/ --target --gcovr-capture-coverage
  3. Generate a report in HTML format genhtml build/lcov.info -o coverage-report
  4. Open the index.html generated in build/coverage-report

Capture coverage (Linux):

  1. Build the init-coverage target to collect baseline coverage data cmake --build build/ --target lcov-init-coverage
  2. Run the test executable to generate coverage data ../../bin/skunit_tests
  3. Build the capture-coverage target to capture the generated coverage data and combine with baseline coverage cmake --build build/ --target --lcov-capture-coverage
  4. Generate a report in HTML format genhtml build/lcov.info -o coverage-report
  5. Open the index.html generated in build/coverage-report

The CMake Tools extension for VS Code can integrate the code coverage report and automatically execute the coverage targets before and after testing (instructions for that are WIP)

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Coverage has been tested on Linux/WSL and generates reports successfully. Mac hasn't been tested.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have requested a review from ... on the Pull Request

Copy link

@JPF2209 JPF2209 left a comment

Choose a reason for hiding this comment

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

Here are some things I would suggest adding before this is finalised

  • For step 1 of the guide, in linux, this command worked "sudo apt install lcov" where the original didn't
  • For step 7 of the guide, this command workds "cmake --build build/ --target capture-coverage" where the original didn't
  • For step 9 of the guide, to view the html where the file was made successfully, I needed run sudo apt update && sudo apt install wslu before running wslview index.html but I would just suggest that for this, unless you want to provide explicit instructions say that to view this, you need to install other libraries and use different methods to do this yourself.

Type of Change

  • New feature

Code Quality

  • Repository: Is this Pull Request is made to the correct repository? (Thoth-Tech NOT SplashKit)
  • Readability: Is the code easy to read and follow? If not are there comments to help understand
    the code?
  • Maintainability: Can this code be easily maintained or extended in the future?

Functionality

  • Correctness: Does the code meet the requirements of the task?
  • Impact on Existing Functionality: Has the impact on existing functionality been considered and
    tested?

Testing

  • Test Coverage: Are unit tests provided for new or modified code?
  • Test Results: Have all tests passed?

Documentation

  • Documentation: Are both inline and applicable external documentation updated and clear?

Pull Request Details

  • PR Description: Is the problem being solved clearly described?
  • Checklist Completion: Have all relevant checklist items been reviewed and completed?

@dijidiji dijidiji marked this pull request as ready for review June 17, 2025 05:35
@omckeon omckeon changed the base branch from main to unit-test-coverage July 4, 2025 04:18
@omckeon
Copy link

omckeon commented Jul 4, 2025

Hi @dijidiji - I haven't been able to test this fully, so I am merging it into the "unit-test-coverage" branch that I've just created (and will merge the other related PR as well). This way you can continue with this, and make any changes/improvements to this based on how things go in T2.

@omckeon omckeon merged commit 0d040e2 into thoth-tech:unit-test-coverage Jul 4, 2025
3 checks passed
@dijidiji dijidiji mentioned this pull request Jul 20, 2025
9 tasks
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