Skip to content

第 9 课:Git + Jenkins —— 让测试自动跑起来

代码提交自动触发跑用例

一、Git 基础

bash
git clone https://github.com/xxx/api-test.git
git pull       # 拉最新代码
git add .      # 添加修改
git commit -m "新增用例"  # 提交
git push       # 推送

二、Jenkins Pipeline

groovy
pipeline {
    agent any
    stages {
        stage('Checkout') { steps { checkout scm } }
        stage('Install') { steps { sh 'pip install -r requirements.txt' } }
        stage('Test') { steps { sh 'pytest tests/ --alluredir=./reports' } }
        stage('Report') { steps { allure includeProperties: true } }
    }
    post {
        success { sh 'python notify.py --status success' }
        failure { sh 'python notify.py --status failed' }
    }
}

三、GitHub Actions(轻量替代)

yaml
name: API Test
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
      - run: pip install -r requirements.txt && pytest tests/ -v

四、飞书通知

python
def send_feishu(msg):
    url = "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
    requests.post(url, json={"msg_type": "text", "content": {"text": msg}})
💬 给清秀留言