20220710 pytest测试框架实战

课堂 ppt

PPT地址

实战内容

pytest 学习路线

pytest 学习路线.xmind.zip (73.3 KB)

计算器功能测试用例

计算器测试用例.xlsx (14.0 KB)

代码地址

日志配置

课后调查表单

python

# name: test_add.py
# date: 2022/7/10

# 测试计算器相加的方法
import pytest

from pytest_prac.script.calculator import Calculator


def setup_method():
    print("开始计算")


def teardown_method():
    print("结束计算")


def teardown_class():
    print("结束测试")


class TestAdd:

    @pytest.mark.p0
    def test_add1(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(1, 1)
        # 预期结果
        expect = 2
        # 断言判断
        assert expect == result

    @pytest.mark.p0
    def test_add2(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-0.01, 0.02)
        # 预期结果
        expect = 0.01
        # 断言判断
        assert expect == result

    @pytest.mark.p0
    def test_add3(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(10, 0.02)
        # 预期结果
        expect = 10.02
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add4(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(98.99, 99)
        # 预期结果
        expect = 197.99
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add5(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(99, 98.99)
        # 预期结果
        expect = 197.99
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add6(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-98.99, -99)
        # 预期结果
        expect = -197.99
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add7(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-99, -98.99)
        # 预期结果
        expect = -197.99
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add8(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(99.01, 0)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add9(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-99.01, -1)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add10(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(2, 99.01)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add11(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(1, -99.01)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add12(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add("文", 9.3)
        # 预期结果
        expect = "不支持输入文字"
        # 断言判断
        assert expect == result

    @pytest.mark.p1
    def test_add13(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(4, "字")
        # 预期结果
        expect = "不支持输入文字"
        # 断言判断
        assert expect == result
# -*- coding: UTF-8 -*-
# author: joker
# perject:jy4
# name:test_add.py
# date:2022/7/10

# 测试计算器相加的方法
import pytest

from pytest_base.script.calculator import Calculator


class TestAdd:
    def setup(self):
        print("开始计算")

    def teardown(self):
        print("结束计算")

    def teardown_class(self):
        print("结束测试")

    @pytest.mark.P0
    @pytest.mark.parametrize('a,b,expect',
                             [(1,1,2),
                              (-0.01,0.02,0.01),
                              (10,0.02,10.02)
                              ])
    def test_add1(self,a,b,expect):
        cal = Calculator()
        try:
            result = cal.add(a,b)
        except:
            print(expect)
        else:
            assert expect == result

    @pytest.mark.P1
    @pytest.mark.parametrize('a,b,expect',
                             [(98.99,99,197.99),
                              (99,98.99,197.99),
                              (-98.99,-99,-197.99),
                              (-99,-98.99,-197.99)
                              ])
    def test_add2(self,a,b,expect):
        cal = Calculator()
        result = cal.add(a,b)
        assert expect == result

    @pytest.mark.P1
    @pytest.mark.parametrize('a,b,expect',
                             [(99.01,0,"参数大小超出范围"),
                              (-99.01,-1,"参数大小超出范围"),
                              (2,99.01,"参数大小超出范围"),
                              (1,-99.01,"参数大小超出范围")
                              ])
    def test_add3(self,a,b,expect):
        cal = Calculator()
        result = cal.add(a,b)
        assert expect == result

    @pytest.mark.P1
    @pytest.mark.parametrize('a,b,expect',
                             [('文',9.3,"请输入范围为【-99, 99】的整数或浮点数"),
                              (4,'字',"请输入范围为【-99, 99】的整数或浮点数")
                              ])
    def test_add4(self,a,b,expect):
        cal = Calculator()
        try:
            result = cal.add(a,b)
        except:
            print(expect)
        else:
            assert expect == result

    
1 个赞
# ~*~ codeing = utf-8 ~*~
# @Time : 2022/7/10 14:57
# @Author : nickGuo
# @File : test_add.py
# @Software : PyCharm
import pytest

from cause_20220710.script.Calculator import Calculator


class TestAdd:
    def setup(self):
        print("开始计算")

    def teardown(self):
        print("结束计算")

    def teardown_class(self):
        print("结束测试")

    @pytest.mark.P0
    def test_add1(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(1, 1)
        # 预期结果
        expect = 2
        # 断言
        assert result == expect

    @pytest.mark.P0
    def test_add2(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-0.01, 0.02)
        # 预期结果
        expect = 0.01
        # 断言
        assert result == expect

    @pytest.mark.P0
    def test_add3(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(10, 0.02)
        # 预期结果
        expect = 10.02
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add4(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(98.99, 99)
        # 预期结果
        expect = 197.99
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add5(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(99, 98.99)
        # 预期结果
        expect = 197.99
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add6(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-98.99, -99)
        # 预期结果
        expect = -197.99
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add7(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-99, -98.99)
        # 预期结果
        expect = -197.99
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add8(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(99.01, 0)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add9(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(-99.01, -1)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add10(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(2, 99.01)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add11(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(1, -99.01)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add12(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add(1, -99.01)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect

    @pytest.mark.P1
    def test_add13(self):
        # 实例化
        cal = Calculator()
        # 实际结果
        result = cal.add("文", 9.3)
        # 预期结果
        expect = "参数大小超出范围"
        # 断言
        assert result == expect


# -*- coding: UTF-8 -*-
# author: joker
# perject:jy4
# name:test_add.py
# date:2022/7/10

# 测试计算器相加的方法
import allure
import pytest
from pytest_base.script.calculator import Calculator
from pytest_base.test.bases.base import Base


class TestAdd(Base):
    def setup_class(self):
        self.cal = Calculator()

    def teardown_class(self):
        import datetime
        now = datetime.datetime.now()
        now_time = now.strftime('%Y-%m-%d')
        print(f'{now_time} [info] 所有用例执行完成')
        print("结束测试")

    @pytest.mark.P0
    @allure.title("正向验证")
    @pytest.mark.parametrize('a,b,expect',
                             [(1,1,2),
                              (-0.01,0.02,0.01),
                              (10,0.02,10.02)])
    def test_add1(self,a,b,expect):
        with allure.step("步骤1:两数相加"):
            result = self.cal.add(a,b)
        with allure.step("步骤2:断言"):
            assert expect == result

    @pytest.mark.P1
    @allure.title("有效边界值验证")
    @pytest.mark.parametrize('a,b,expect',
                             [(98.99,99,197.99),
                              (99,98.99,197.99),
                              (-98.99,-99,-197.99),
                              (-99,-98.99,-197.99)
                              ])
    def test_add2(self,a,b,expect):
        with allure.step("步骤1:两数相加"):
            result = self.cal.add(a,b)
        with allure.step("步骤2:断言"):
            assert expect == result

    @pytest.mark.P1
    @allure.title("无效边界值验证")
    @pytest.mark.parametrize('a,b,expect',
                             [(99.01,0,"参数大小超出范围"),
                              (-99.01,-1,"参数大小超出范围"),
                              (2,99.01,"参数大小超出范围"),
                              (1,-99.01,"参数大小超出范围")
                              ])
    def test_add3(self,a,b,expect):
        with allure.step("步骤1:两数相加"):
            result = self.cal.add(a,b)
        with allure.step("步骤2:断言"):
            assert expect == result

    @pytest.mark.P1
    @allure.title("中文验证")
    @pytest.mark.parametrize('a,b,expect',
                             [('文',9.3,"请输入范围为【-99, 99】的整数或浮点数"),
                              (4,'字',"请输入范围为【-99, 99】的整数或浮点数")
                              ])
    def test_add4(self,a,b,expect):
        # 捕获异常方法一
        try:
            with allure.step("步骤1:两数相加"):
                result = self.cal.add(a,b)
        except TypeError:
            print(expect)
        else:
            with allure.step("步骤2:断言"):
                assert expect == result

1 个赞