# Add Python Test Coverage to CI/CD pipeline

Before proceeding further:

  • Go to Settings page of the repository dashboard in DeepSource and copy the DSN from the Reporting section.

    WARNING

    Do not add the DEEPSOURCE_DSN variable as part of any publicly visible configuration file. It contains sensitive information.

  • Make sure that "Test Coverage" Analyzer is added to .deepsource.toml.

  • Ensure that you are running tests with coverage or pytest-cov for pytest projects.

# With Travis CI

  1. On Travis CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

  2. Add this to .travis.yml:

after_success:
  # Generate coverage report in xml format
  - coverage xml
   
  # Install deepsource CLI
  - curl https://deepsource.io/cli | sh
   
  # From the root directory, run the report coverage command
  - ./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

# With Circle CI

  1. On Circle CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

  2. Add the following step in .circleci/config.yml:

- run:
  name: Report results to DeepSource
  command: |
    # Generate coverage report in xml format
    coverage xml
  
    # Install deepsource CLI
    curl https://deepsource.io/cli | sh
  
    # From the root directory, run the report coverage command
    ./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

# With GitHub Actions

  1. On GitHub, navigate to the main page of the repository. Under your repository name, click "Settings". In the left sidebar, click Secrets.

    • Type DEEPSOURCE_DSN in the "Name" input box.
    • Add the value copied above.
  2. When you checkout code, ensure that you use pull request HEAD commit instead of merge commit:

  - uses: actions/checkout@v2
    with:
      ref: ${{ github.event.pull_request.head.sha }}
  1. Add the following step in .github/workflows/main.yml:
  - name: Report results to DeepSource
    run: |
    # Generate coverage report in xml format
      coverage xml
   
    # Install deepsource CLI
      curl https://deepsource.io/cli | sh
   
    # From the root directory, run the report coverage command
      ./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml
      
    env: 
      DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}

# With GitLab CI

  1. Navigate to the project page of the repository on GitLab. Under project settings, in the sidebar, click on "CI/CD". Expand the variable section, and add the following:

    • Type: "Variable"`
    • Key: DEEPSOURCE_DSN
    • Value: The DSN value copied above
    • State: Protected (Yes)
    • Masked: No
    • Scope: All Environments
  2. Add the following under the test job in .gitlab-ci.yml:

test:
  script:
    # Install dependencies
    - pip install coverage pytest pytest-cov

    # Run tests
    - pytest --cov

    # Generate coverage report in XML format
    - coverage xml

    # Install deepsource CLI
    - curl https://deepsource.io/cli | sh

    # From the root directory, run the report coverage command
    - ./bin/deepsource report --analyzer test-coverage --key python --value file ./coverage.xml
Last Updated: 7/8/2020, 10:03:47 AM