README说明

仓库地址:https://github.com/ptest1234/my_mock.git

别人写的再好也是来淘汰自己的,一定要手写代码才是自己的,功能简单实现了通过pytest框架,

  1. mock造数据服务
  2. 根据不同环境运行脚本
  3. 根据需要执行回归测试用例和冒烟测试用例
  4. 分布式执行,增强运行效率
  5. 通过allure报告,详细记录结果
  6. 集成github actions提交代码后,立即构建执行
# 功能列表
db.json自定义mock服务 

pytest测试github工作流持续集成

运行配置
# 运行步骤:
版本:python3.12
安装需要包:pip3 install -r requirements.txt
# pytest 分布式执行 
pytest -n 2 -v
# 根据标签执行测试
pytest -m Smoke
# 生成allure报告服务
allure serve temp
# 切换环境运行测试
pytest --env=dev

代码框架分布

.venv:虚拟环境配置

一文搞懂pytest框架,掌握接口自动化测试_python

config:文件读取,环境配置

temp:生成allure报告临时文件

tests:测试用例文件目录

conftest.py:fixture执行前数据读取

db.json mock服务返回的数据

pytest.ini 执行pytest参数配置

pytest.yml 配置github工作流

源码情况

pytest.yml内容

# 工作流安装python3.12环境, 运行pytest测试

name: Pytest API Testing

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  Pytes-API-Testing:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.12
      uses: actions/setup-python@v3
      with:
        python-version: "3.12"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        
    - name: Test with pytest
      run: |
        pytest --env=dev

    - name: Archive Pytest test report
      uses: actions/upload-artifact@v3
      with:
        name: Pytest-allure-report
        path: temp
          
    - name: Upload Pytest report to GitHub
      uses: actions/upload-artifact@v3
      with:
        name: Pytest-allure-report
        path: temp

conftest.py源码

import os

import pytest
import json


def pytest_addoption(parser):
    parser.addoption(
        "--env", action="store", default="dev", help="env:表示命令行参数内容,不填写默认输出default的值内容"
    )

@pytest.fixture(scope="session")
def env_config(request):
    # env = os.getenv("ENV", "dev")
    env = request.config.getoption("--env")
    print(f"env: {env}")
    with open(f"./config/{env}_config.json") as f:
        env_data = json.load(f)
    return env_data


@pytest.fixture(scope="session")
def env_request_data(request):
    env = request.config.getoption("--env")
    with open(f"./config/{env}_request_data.json") as f:
        request_data = json.load(f)
    return request_data


@pytest.fixture(scope="session")
def env_response_data(request):
    env = request.config.getoption("--env")
    with open(f"./config/{env}_response_data.json") as f:
        _response_data = json.load(f)
    return _response_data

test_demo_multi_environment.py内容

import time
import allure
import pytest
import requests


@allure.feature("多环境测试")
class TestDemoMultiEnvironment:

    @pytest.mark.Regression
    @allure.story("Test example get endpoint")
    @allure.title("Verify the get API")
    @allure.description("verify the get API response status code and data")
    @allure.severity("blocker")
    def test_get_demo_env(self, env_config, env_request_data, env_response_data):
        # 获取对于环境的数据
        time.sleep(2)  # 测试并发和串行区别
        base_url = env_config['host']
        get_api = env_config['getAPI']
        get_api_response_data = env_response_data['getAPI']
        # 发送请求
        response = requests.get(base_url + get_api)
        # 检查响应状态码
        assert response.status_code == 200
        # 检查响应数据
        assert response.json() == get_api_response_data

    @pytest.mark.Smoke
    @allure.story("Test example POST API")
    @allure.title("Verify the POST API")
    @allure.description("verify the POST API response status code and data")
    @allure.severity("Critical")
    def test_post_demo_env(self, env_config, env_request_data, env_response_data):
        time.sleep(2)  # 测试并发和串行区别
        base_url = env_config['host']
        post_api = env_config['postAPI']
        data = env_request_data['postApi']
        post_api_response_data = env_response_data['postAPI']
        # 发送请求
        response = requests.post(base_url + post_api, data=data)
        # 检查响应状态码
        assert response.status_code == 201
        # 检查响应数据
        print("response data is" + str(response.json()))
        assert response.json() == post_api_response_data

dev_config.json源码

{
  "host": "https://my-json-server.typicode.com/ptest1234/my_mock",
  "getAPI": "/posts/1",
  "postAPI":"/posts"
}

dev_request_data.json源码

{
  "getApi": "",
  "postApi": {
    "title": "foo",
    "body": "bar",
    "userId": 1
  }
}

dev_response_data.json源码

{
    "getAPI": {
      "id": 1,
      "title": "hello"
    },
    "postAPI":{
      "title": "foo",
      "body": "bar",
      "userId": "1",
      "id": 3
    }
}

db.json源码

{
  "posts": [
    {
      "id": 1,
      "title": "hello"
    },
    {
      "id": 2,
      "title": "world"
    }
  ],
  "get": {
    "name": "请求成功"
  },
  "delete":{
    "msg":"删除成功!"
  }
}

pytest.ini源码

[pytest]
addopts = --alluredir ./temp
;addopts = -vs -rf --html-report=./report --title='PYTEST REPORT'
markers =
    Regression: marks tests as Regression
    Smoke: marks tests as Smoke

运行

  1. 参考readme配置前提条件,和需要的包
  2. Github新建仓库,把本地代码上传到Github
  3. 根据需要执行不同命令