python pytest 测试实战 1

https://gitee.com/wuxiheng/wuxiheng-python-pytest-zuoye

pytest实战1课后作业_计算器测试

gitee仓库链接:https://gitee.com/zhang1027452886/zsy_python_job.git

  • 问题:浮点数运算,结果不准确的问题,没有解决

https://github.com/zhanghao-github-python/pythonhomework.git

https://github.com/duoha1013/hogwarts_test/tree/main/task_pytest1/testcase

https://github.com/orange3706/practice/tree/master/pythonProject/test_pytest
float、非数字类型 等tasecase 的断言问题未解决

https://github.com/7QYe/HogwartsWork/tree/master/hog_work706

整体完成的还不错,考虑的比较全面。

建议:
1、注意按照 pytest命名规范命名 文件名要以test_开头
2、尽量多添加 一些注释描述信息
3、用例过于复杂,可以将不同类型的用例拆分开管理

整体完成的还不错。

建议:
1、命名尽量规范一些,包括文件,参数的命名,便于日后维护
参考:Python风格规范 — Google 开源项目风格指南
2、注释添加 详细一些
3、注意用例的异常情况处理,需要在测试代码中体现,比如 除数为0的情况。

整体完成的不错。

建议:
1、注意目录结构划分,清晰一些,不要都放在一个文件夹下
2、用例考虑的比较全面,写的过于复杂,最好把不同类型的拆分开管理
3、注释信息描述的再详细一些
4、浮点数可以考虑使用round() 方法对精度进行处理

完成的还可以

建议:
1、注意目录结构划分,分别管理不同类型的文件
2、用例考虑的不够全面,没有处理异常情况

用例考虑的比较全面

建议:
1、用例设计过于复杂,处理的情况太多,建议拆分不同类型的用例,分别管理 。
2、添加一些注释,便于后续维护

不错,考虑到数据多的情况下,使用了yaml 文件管理测试数据。
建议:
1、添加一些异常情况的处理
2、数据文件放在一个单独的目录中管理。注意目录结构的划分

不错,考虑的情况比较全面

建议:
1、将不同的情况 的用例分别管理 ,否则用例过于复杂,不方便维护

整体完成的还不错。

建议:
1、用例考虑不够全面,异常情况未处理
2、添加一些注释

建议:
1、注意命名,不要直接使用test 这样的名字,命名规范建议参考
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/#id16
2、用例设计的不够全面
3、用例写的过于复杂,建议将不同的情况拆分处理
4、不要在用例里面创建用例

https://github.com/Ws5668/pytest_homework/tree/master/PyCharm/homework_pytest/calculator_demo

from test_pytest.pythoncode.calculator import Calculator
import logging
import pytest


'''
实现参数化
'''

class TestCalc:
    def setup_class(self):
        #setup会在每条用例执行之前分别执行
        #如果想让用例里可以用到calc这个实例,需要让这个calc变成实例变量
        #调用的时候使用self.calc
        self.calc = Calculator()
        logging.info("开始计算")

    def teardown_class(self):
        logging.info("结束计算")

    #加法
    @pytest.mark.parametrize('a, b, expect',[
        [1, 1, 2],
        [0.1, 0.1, 0.2],
        [-1, -2, -3]
    ])
    def test_add(self, a, b, expect):
        #等号右侧是实际结果,左侧是预期结果
        assert expect ==self.calc.add(a, b)

    #减法
    @pytest.mark.parametrize('a, b, expect',[
        [2, 1, 1],
        [0.2, 0.1, 0.1],
        [-2, -4, 2],
        [9, 10, -1]
    ])
    def test_sub(self, a, b, expect):
        #等号右侧是实际结果,左侧是预期结果
        assert expect ==self.calc.sub(a, b)

    #乘法
    @pytest.mark.parametrize('a, b, expect',[
        [2, 1, 2],
        [0.2, 0.1, 0.02],
        [-2, -2, 4],
        [-2, 10, -20],
        [99, 0, 0]
    ])
    def test_mul(self, a, b, expect):
        #等号右侧是实际结果,左侧是预期结果
        assert expect ==self.calc.mul(a, b)

    #除法
    @pytest.mark.parametrize('a, b, expect',[
        [2, 0, 1],
        [0.2, 0.1, 2],
        [-4, -2, 2],
        [-4, 2, -2],
        [5, 10, 0.5]
    ])
    def test_div(self, a, b, expect):
        #等号右侧是实际结果,左侧是预期结果
        if b == 0:
            logging.info("分母为0时无法计算!")
        else:
            assert expect ==self.calc.div(a, b)