韦奇_pytest_L3_计算器实战

L3_计算器实战

概述

  • 调整了工程结构
  • 增加了标签、Foxture
  • 配置了 pytest.ini 、conftest.py

项目工程结构

    |-- 霍格沃兹魔法棍
        |-- allure-report  # allure 报告
        |-- allure-results # case执行结果集
        |-- case   # 存放case路径                
            |-- test_add.py
            |-- test_div.py
            |-- test_mark_skip.py
            |-- test_mark_xfail.py
        |-- config   # 配置文件
            |-- config.py
            |-- read_file.py
        |-- dev_code # 被测软件代码
            |-- Calculator.py
        |-- yaml  #  存放yaml文件处
            |-- data.yaml
        |-- conftest.py  #  pytest特有的配置组件,里边存放全局方法,配合case
        |-- pytest.ini     #  pytest配置文件,执行命令可在此文件做配置
        |-- run_pytest.py     #  执行入口文件

image

代码展示

#  case/test_add.py-----------------------------------------------
import os
import allure
import yaml
import pytest
from config.config import  yaml_path
from dev_code.Calculator import Calculator
from config.read_file import read_yaml

'''
项目名称:@allure.epic(‘项目名称’)
模块名称:@allure.feature(‘模块名称’)在类的上方
接口名称: @allure.story(‘用例名称:第一条小白1’) 再用用例上面
测试用例标题:@allure.title(‘测试用例标题第一种方式,打印信息小白’)
            allure.dynamic.title(‘测试用例标题第二种方式,打印信息小白’)
标记为跳过: @pytest.mark.skip(reason="不需要执行test_01")
标记为预期失败:@pytest.mark.xfail(reason="bug待修复")

'''


'''执行yaml用例'''
@allure.epic('霍格沃兹')
@allure.feature('加除运算模块')
class TestParams:
    shili = Calculator()
    # 路径拼接
    @pytest.mark.parametrize("case", read_yaml(yaml_path('yaml/data.yaml')))
    @allure.story('add')
    @allure.title('加法运算')
    def test_Calculatoradd(self,case,fixturetest):
        # '''验证add方法'''
        assert self.shili.add(case[1], case[2]) == case[3]

# config/config.py-----------------------------------------------------------------
import os.path
import platform

def yaml_path(paths):
    if platform.system().lower() == 'windows':
        yaml_pth = "D:/rj/python_code/霍格沃兹魔法棍/"
    else:
        yaml_pth = "/opt/tomcat9/webapps/ROOT/"
    return os.path.join(yaml_pth,paths)

# config/read_file.py-------------------------------------------------------------------
import yaml

'''读取yaml用例'''
def read_yaml(path):
    with open(f"{ str(path) }", "r" , encoding='utf-8', errors='ignore') as file:
        data = yaml.safe_load(file)
    return data

# dev_code/Calculator.py-------------------------------------------------------------------
'''被测类'''
class Calculator:
    def add(self, a, b):
        if a > 99 or a < -99 or b > 99 or b < -99:
            # print("请输入范围为【-99, 99】的整数或浮点数")
            return "参数大小超出范围"
        return a + b

    def div(self, a, b):
        if a > 99 or a < -99 or b > 99 or b < -99:
            # print("请输入范围为【-99, 99】的整数或浮点数")
            return "参数大小超出范围"
        return a / b

# yaml/data.yaml-------------------------------------------------------------------
-
  - add-正确入参整数测试-等价类
  - 10
  - 20
  - 30
  - 0.5
-
  - add-正确入参小数测试-等价类
  - 10.6
  - 20.87
  - 31.47
  - 0.5079060852898898


- - add-输入字符入参测试-等价类
  - ands
  - ands
  - 不可输入字符串
  - 不可输入字符串

- - add-输入特殊字符入参测试-错误推测法
  - ands
  - ands
  - 不可输入特殊字符
  - 不可输入特殊字符

- - add-a为100入参测试-边界值
  - 100
  - 20
  - 参数大小超出范围
  - 参数大小超出范围

- - add-a为99入参测试-边界值
  - 99
  - 20
  - 119
  - 4.95

- - add-a为-99入参测试-边界值
  - -99
  - 20
  - -79
  - -4.95

- - add-a为-100入参测试-边界值
  - -100
  - 20
  - 参数大小超出范围
  - 参数大小超出范围



- - add-b为100入参测试-边界值
  - 20
  - 100
  - 参数大小超出范围
  - 参数大小超出范围

- - add-b为99入参测试-边界值
  - 20
  - 99
  - 119
  - 0.20202020202020202

- - add-b为-99入参测试-边界值
  - 20
  - -99
  - -79
  - -0.20202020202020202

- - add-b为-100入参测试-边界值
  - 20
  - -100
  - 参数大小超出范围
  - 参数大小超出范围

# conftest.py-------------------------------------------------------------------
# -*- coding: utf-8 -*-
import pytest

'''
session 会话级别:
每个session只运行一次,session级别的fixture需要定义到conftest.py中

module 模块级别:
模块里所有的用例执行前执行一次module级别的fixture

class 类级别 :
每个类执行前都会执行一次class级别的fixture

function :
前面实例已经说了,这个模式是默认的模式,函数级别的,每个测试用例执行前都会执行一次function级别的fixture
'''
@pytest.fixture(scope='session',autouse=True)  # 多个文件调用一次
def fixturetest():
    print('----------开始-------------')
    yield
    print('----------结束-------------')

# pytest.ini-------------------------------------------------------------------
[pytest]
addopts = -v

# run_pytest.py-------------------------------------------------------------------

import os
import pytest

if __name__ == '__main__':
    '''
        -v:pytest -v 说明:可以输出用例更加详细的执行信息
        -s:pytest -s 说明:输入我们用例中的调式信息
        -m:pytest -m ”标记“ 说明:执行特定的测试用例
        -q:pytest -q 说明:简化控制台的输出
        -k:pytest -k “关键字” 说明:执行用例包含“关键字”的用例
        –tb=style:屏蔽测试用例执行输出的回溯信息
        '''
    pytest.main(['-s', '-q',
                 'case',
                 '--clean-alluredir',
                 '--alluredir=allure-results'])
    os.system(r"allure generate -c -o allure-report")

执行结果