Improve Code Quality With Gitlab

1 minute read

Published:

代码质量检测在大型工程项目中扮演着十分重要的角色, 通过高效可靠的代码质量检测可以大幅提升代码健壮性以及可阅读性. 常规的代码检测可以人工审查和基于工具的自动检测.

人工审查:

  1. 对现有代码提出Issue

  2. committer push 代码至repository中

  3. community中的member 对于该repository中提交的PR(Pull Request, or Merge Request in GitLab) 进行code review
    1. 全面审查代码 line by line
    2. 对照信息来源(原始代码, Paper)
    3. 查看CI/CD自动审查报告
  4. Approval/Merge 只有经过review的code才允许被合并进入master分支当中
  • General/Approvals:

Approvals

  • Repository/Protected Branches:

Protected Branches

基于工具的自动检测

CI/CD配置

Gitlab为可持续开发提供了良好的环境, 会自动运行CI/CD在每次push code以及merge request之后.

一个包含Pylint测试以及unittest的.gitlab-ci.yml配置如下:

image:
  name: your image
  entrypoint: [""]

stages:
  - unittest

lint:
  stage: unittest
  script:
    - python -m pip install --upgrade pip
    - python -m pip install pylint -U
    # 使用自定义的规则进行代码检查, 得分低于 8 分则失败, 不再执行之后的 stage
    - python -m pylint --rcfile=.pylintrc --fail-under=8 src scripts

unittest:
  stage: unittest
  script:
    - python -m pip install --upgrade pip
    - python -m pip install setuptools pytest -U
    - python -m pip install -U -e .[dev]
    - export $(grep -v '^#' .env | xargs)
    - python -m pytest -vv tests

使用only指定场景触发,e.g. merge_requests or master,详见docs

isort

isort可以按字母顺序对导入的文件进行排序,并自动按类型自动分成三部分:

  1. 标准库

  2. 第三方库

  3. 自项目

Pycharm External Tools配置(单个文件):

image-20210301211118352

常用配置

isort -m=2 -l=120

Autopep8

Autopep8可以自动格式化你指定代码文件, 修复一些常规代码格式不规范: 修复缩进,删除无关的空格,并重构常见错误

检查单个文件:

autopep8 --in-place --aggressive --aggressive --max-line-length=120  xxx.py

Pycharm External Tools配置:

image-20210301212027430

Pylint

Pylint是一个检查Python代码中错误的工具,尝试执行编码标准并寻找代码异味。它还可以查找特定类型的错误,可以就如何重构特定块提出建议,并提供有关代码复杂性的详细信息

代码风格规范: Google Python Style Guide

安装:

pip install pylint

配置文件: pylintrc file

检查单个文件:

pylint --rcfile=.pylintrc xxx.py

检查单个文件夹/Module

python -m pylint --rcfile=.pylintrc src

带得分的代码检查, 得分低于 8 分则失败

pylint --rcfile=.pylintrc --fail-under=8 src scripts

Pylint with Pycharm

  1. External Tools (recommend):

image-20210301211004143

  1. 集成插件: Pylint Plugin

image-20210301213052766

unittest

将自定义配置文件pytest.ini放置于项目根目录下

Example:

# -*- coding:utf-8 -*-
# @Time    : 2020/9/29 16:14
# @Author  : zheyuye
# @File    : test_attention_cell.py

import pytest

@pytest.mark.parametrize('num_heads', [1, 2, 3])
@pytest.mark.parametrize('do_return_2d_tensor', [False, True])
@pytest.mark.parametrize('scaled', [False, True])
def test_multi_head_attention_cell(num_heads, do_return_2d_tensor, scaled):
	pass

运行所有单元测试文件进行:

python3 -m pytest .

检查某个特定文件, 以 test_attention_cell.py为例

python3 -m pytest test_attention_cell.py

特定检查某个函数:

python3 -m pytest test_attention_cell.py::test_multi_head_attention_cell

使用特定参数检查某个特定函数:

python3 -m pytest test_attention_cell.py::test_multi_head_attention_cell[False-False-1]

更多用法, 详见 official guide of pytest

Code Coverage

\[\text{Coverage} = \frac{\text{hits}}{\text{hits}+\text{partial }+\text{miss }}\]
  • hit 指被测试套件执行到的代码。
  • partial 指代码没有被测试套件完整执行(有没有执行到的分支)
  • miss 就是源代码没有被测试套件执行

Coverage and pytest-cov

同样地, 将自定义配置文件.coveragerc放置于项目根目录下配置文件:

根据Test Coverage Visualization in GitLab的指示, 重新配置.gitlab-ci.ymlpytest相关命令:

unittest:
  stage: unittest
  script:
    - python -m pip install --upgrade pip
    - python -m pip install setuptools pytest pytest-cov -U
    - python -m pip install -U -e .[dev]
    - export $(grep -v '^#' .env | xargs)
    - python -m pytest -vv tests --cov-config=./.coveragerc --cov=./ --cov-report=xml
  artifacts:
      reports:
        cobertura: coverage.xml

覆盖率报告: image-20210302123900347

添加代码质量标识(badge)至首页:

image-20210302130259868

Schedules

Weekly or Daily 定时检测, 跟踪项目质量.

image-20210302155300281