Operations 9 min read

Setting Up GitHub Actions for Python Code Quality with CodeFactor, wemake‑python‑styleguide, and Codecov

This tutorial walks through configuring GitHub Actions for a Python project to enforce code style, run unit tests, and generate coverage reports using CodeFactor, the wemake‑python‑styleguide action, and Codecov, complete with step‑by‑step instructions and workflow examples.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Setting Up GitHub Actions for Python Code Quality with CodeFactor, wemake‑python‑styleguide, and Codecov

In this tutorial the author explains how to set up a GitHub Actions workflow for a Python project to ensure code quality, style compliance, and test coverage.

First, the article introduces CodeFactor, a free tool for static analysis of public repositories. It describes how to sign in with GitHub, add a repository, and view the list of detected issues.

Next, it shows how to configure the wemake-python-styleguide action in a pull‑request workflow. The workflow file .github/workflows/workflow-pr.yaml is provided, containing four steps: checkout, set up Python 3.8, run unit tests with pytest , and run the styleguide action with the github-pr-review reporter.

name: Python Pull Request Workflow
on: [pull_request]
jobs:
  qa:
    name: Quality check
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python
        uses: actions/setup-python@master
        with:
          python-version: 3.8
      - name: Run unit tests
        run: |
          pip install pytest
          pytest
      - name: Wemake Python Stylguide
        uses: wemake-services/[email protected]
        continue-on-error: true
        with:
          reporter: 'github-pr-review'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The article then explains how to add Codecov to generate a coverage report. After creating a Codecov account and obtaining a token, a second workflow .github/workflows/workflow-master.yaml is defined, which runs on pushes to the master branch, installs pytest and pytest‑cov , generates an XML coverage report, and uploads it using the codecov/codecov-action@v1 action.

name: Python Master Workflow
on:
  push:
    branches:
      - 'master'
jobs:
  codecov:
    name: Codecov Workflow
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python
        uses: actions/setup-python@master
        with:
          python-version: 3.8
      - name: Generate coverage report
        run: |
          pip install pytest
          pip install pytest-cov
          pytest --cov=./ --cov-report=xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./coverage.xml
          flags: unittests

Finally, the author advises testing the workflows by creating a branch, opening a pull request, and checking the “Actions” tab for successful runs, as well as viewing the coverage badge on Codecov. The conclusion reminds readers that many other tools (SonarCloud, Pylint, Coveralls, DeepSource) are also available for improving Python code quality.

PythonCI/CDcode qualityGitHub ActionscodecovCodeFactorwemake-python-styleguide
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.