Python 标准项目实践

last modify

GitHub Actions

项目发布

pypa/gh-action-pypi-publish: GitHub Action, for publishing distribution files to PyPI

代码覆盖测试

codecov/codecov-action: GitHub Action that uploads coverage to Codecov

  • 虽然文档说明公开仓库不需要申请申请密钥,但是可能会报错,详见:Error: failed to properly upload

  • 申请 CODECOV_TOKEN

    • 登录 Codecov.io(关联 Github);

    • 查看 Not yet setup 一栏(默认显示 Enabled),选择需要测试的仓库;

    • CODECOV_TOKEN 添加到 Actions secrets

示例

steps:
- name: Checkout Repo
  uses: actions/checkout@v3
- name: Setup Python
  uses: actions/setup-python@v4
  with:
    python-version: '3.10'
- name: Install dependencies
  run: |
    python -m pip install --upgrade pip
    python -m pip install flake8 pytest pytest-cov
    pip install -r requirements.txt
- name: Setup package
    run: python setup.py install
- name: Pytest with Generate coverage report
  run: pytest --cov=<package_name> --cov-report=xml:./coverage.xml  # import <package_name>
- name: Upload coverage to Codecov
  uses: codecov/codecov-action@v3
  with:
    token: ${{ secrets.CODECOV_TOKEN }}
    flags: pytest
    directory: ./coverage/reports/
    files: ./coverage.xml
    fail_ci_if_error: true
    verbose: true

参考资料

相关文档

Python 项目模板

Last updated