Bitbucket Actions Workflow

Add the following job to your bitbucket_pipeline.yaml. It triggers on pushes to the mentioned branch in the yaml file:

image: python:3.11

pipelines:
  default:
    - step:
        name: Run tests, generate coverage report and upload to CodeAnt AI
        caches:
          - pip
        script:
          - python -m venv .venv
          - source .venv/bin/activate
          - pip install uv
          - uv pip install coverage pytest
          - coverage run -m pytest tests/
          - coverage xml -o coverage.xml
          - cat coverage.xml
          - export API_BASE="https://ved6vc7dkvz3veaigrllnasa4y0kmqbw.lambda-url.ap-south-1.on.aws"
          - curl -sS -X GET "${API_BASE}/pr/analysis/coverage/script/get" --output upload_coverage.sh
          - chmod +x upload_coverage.sh
          - bash upload_coverage.sh -t ${ACCESS_TOKEN} -r ${BITBUCKET_REPO_FULL_NAME} -c ${BITBUCKET_COMMIT} -f coverage.xml -p bitbucket

Don’t forget to add your access token to the pipeline secrets under the name “ACCESS_TOKEN”. The access token should have write access to the repo.

Coverage config file

You have to create a .coveragerc file in the project’s root folder to include all the source files in the test coverage calculation.

Example:

# include every Python file under the repo root
source = .

# exclude tests, virtualenvs, build artifacts, etc.
omit =
    */tests/*
    */.venv/*
    */build/*
    */dist/*

When you assign source to ”.” , It checks for every python file in the root folder and its sub directories. You can omit some directories by placing them in the omit section of the file.

How it works

With the above configuration:

  1. coverage run -m pytest tests/ will count every .py under the workspace as “valid” lines except for those in the omitted directories.
  2. Lines actually executed by your tests are marked “covered.”
  3. coverage xml -o coverage.xml produces a Cobertura-style report reflecting true coverage over the entire codebase.
  4. Using this coverage xml, we calculate the coverage percentage and the status check will be done on every new push to the branch.