'How to limit pytest code coverage calculation to only specific files?

I am trying to setup continues integration in github using actions. I setup a action to get the code coverage automatically, this part works great, however it's calculating the code coverage for every file in my src directory, this is not preferred, I would like it to calculate the code coverage for only the files modified, how do I do that?

name: Pytest Coverage
on:
  pull_request:
    branches: [ main, dev ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.10
      uses: actions/setup-python@v2
      with:
        python-version: "3.10"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest pytest-cov
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Changed Files Exporter
      id: files
      uses: umani/[email protected]
      with:
        repo-token: ${{ github.token }}
        pattern: '^src.*\.(py)$'
        result-encoding: 'string'
    - name: Build coverage file
      run: |
        echo "${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}"
        pytest --cache-clear --cov-branch --cov=src > pytest-coverage.txt
    - name: Comment coverage
      uses: coroo/[email protected]
    - name: Get Coverage %
      run: |
        LAST_LINE=$(tail -4 pytest-coverage.txt)
        LAST_LINE=$(head -n 1 <<< "$LAST_LINE")
        COVERAGE=$(sed 's/.*[[:space:]]\([0-9]\+\)%/\1/' <<< "$LAST_LINE")
        echo "Overall code coverage is $COVERAGE"
        echo 'SCORE<<EOF' >> $GITHUB_ENV
        echo  "$SCORE"  >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV

The important 2 lines are here:

    echo "${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}"
    pytest --cache-clear --cov-branch --cov=src > pytest-coverage.txt

The echo print out the correct files:

src/scripts/testing_script.py src/server.py

How do I change this line so it ONLY calculate the total coverage of thoese 2 modified files?

pytest --cache-clear --cov-branch --cov=src > pytest-coverage.txt


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source