2022.4.10 Python Pytest 实战训练营

PPT

https://pdf.ceshiren.com/jy2/jy2_pytest

代码地址

测试用例

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

梳理

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

allure 安装

  • allure 配置环境 变量(依赖 Java1.8)
  • allure-pytest 安装 第三方插件

import pytest
import yaml

from pythoncode.calculator import Calculator

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

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

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

    @pytest.mark.parametrize("value1, value2, reslut", yaml.safe_load(open("./test.yaml")))
    def test_add0(self, value1, value2, reslut):
        Calculator().add(value1, value2)
        assert Calculator().add(value1, value2) == reslut


yaml
image

import pytest

from testcode.calculator import Calculator
@pytest.mark.parametrize("a,b,expected",[(1,1,2),(-0.01,0.02,0.01),(10,0.02,10.02),(98.99,99,197.99),(99,98.99,197.99),(-98.99,-99,-197.99),(-99,-98.99,-197.99),(99.01,0,"参数大小超出范围")])
class TestCalculator:
    def setup(self):
        print("开始计算")
    def teardown(self):
        print("结束计算")
    def teardown_class(self):
        print("结束测试")

    def test_add0(self,a,b,expected):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(a, b)
        # 实际结果 对比 预期结果
        assert result == expected

1 Like
from course1.calculator import Calculator
import pytest

class TestCalculator:

    def setup(self):
        print('开始计算')

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

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

    @pytest.mark.p0
    @pytest.mark.parametrize('a,b,result',
                             [
                                 (1,1,2),
                                 (-0.01,0.02,0.01),
                                 (10,0.02,10.02),
                             ])
    def test_add_p0(self,a,b,result):
        # print(a,b,result)
        cal=Calculator()
        assert cal.add(a,b) == result


    @pytest.mark.p1
    @pytest.mark.parametrize('a,b,result',
                             [
                                 (98.99,99,197.99),
                                 (99,98.99,197.99),
                                 (-98.99,-99,-197.99),
                                 (-99,-98.99,-197.99),
                                 (99.01,0,'参数大小超出范围'),
                                 (-99.01, -1, '参数大小超出范围'),
                                 (2, 99.01, '参数大小超出范围'),
                                 (1, -99.01, '参数大小超出范围'),
                             ])
    def test_add_p1(self,a,b,result):
        # print(a,b,result)
        cal=Calculator()
        assert cal.add(a,b) == result
3 Likes
from pythoncode.calculator import Calculator
import pytest

class TestCalculator:
    def setup(self):
        pass

    def teardown(self):
        pass

    expection  = [(1,1,2),(-0.01,0.02,0.01),(10,0.02,10.02),(98.99,99,197.99),
                  (99,98.99,197.99),(-98.99,-99,-197.99),(-99,-98.99,-197.99),
                  (99.01,0,'参数大小超出范围'),(-99.01,-1,'参数大小超出范围'),(2,99.01,'参数大小超出范围'),
                  (1,-99.01,'参数大小超出范围')
                  ]
    @pytest.mark.parametrize("first_num,two_num,expected",expection)
    def test_add0(self,first_num,two_num,expected):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(first_num, two_num)
        print(result)
        # 实际结果 对比 预期结果
        assert result == expected
import sys
import pytest

sys.path.append('..')
from pythoncode.calculator import Calculator


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

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

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

    @pytest.mark.p1
    @pytest.mark.parametrize("x,y,expect", [
        (1, 1, 2),
        (-0.01, 0.02, 0.01),
        (10, 0.02, 10.02),
        (98.99, 99, 197.99),
        (99.01, 0, "参数大小超出范围")
    ])
    def test_add(self, x, y, expect):
        # 测试相加方法
        calc = Calculator()
        actual_result = calc.add(x, y)
        # print(result)
        # 实际结果 对比 预期结果
        assert expect == actual_result

import pytest

class TestCalculator:

def setup(self):
	self.cal = Calculator()
	print("开始计算---------")

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

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

@pytest.mark.case_p0
def test_add_p0_01(self):
	result = self.cal.add(1,1)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == 2

@pytest.mark.case_p0
def test_add_p0_02(self):
	result = self.cal.add(-0.01,0.02)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == 0.01

@pytest.mark.case_p0
def test_add_p0_03(self):
	result = self.cal.add(10,0.02)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == 10.02

@pytest.mark.case_p1
def test_add_p1_01(self):
	result = self.cal.add(99,98.99)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == 197.99

@pytest.mark.case_p1
def test_add_p1_02(self):
	result = self.cal.add(-99,98.99)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == -0.01

@pytest.mark.case_p1
def test_add_p1_03(self):
	result = self.cal.add(98,99)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == 197

@pytest.mark.case_p1
def test_add_p1_04(self):
	result = self.cal.add(-98.99,-99)
	print("result---", result)

	# 实际结果 与 预期结果 对比
	assert result == -197.99

image


data:
- - 1
  - 1
  - 2
- - -0.01
  - 0.02
  - 0.01
- - 10
  - 0.02
  - 10.02
- - 98.99
  - 99
  - 197.99
- - 99
  - 98.99
  - 197.99
- - -98.99
  - -99
  - -197.99
- - 99.01
  - 0
  - 参数大小超出范围

新加data目录 下面有data.yaml

import pytest
import yaml

from pythoncode.calculator import Calculator

@pytest.fixture(scope='function',autouse=True)
def calculate():
    print('开始计算')
    yield
    print('结束计算')

class TestCalculator:
    def setup(self):
        pass

    def teardown(self):
        pass

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

    @pytest.mark.p0
    @pytest.mark.parametrize('x,y,excepted',yaml.safe_load(open('../data/data.yaml',encoding='utf-8'))['data'])
    def test_add0(self,x,y,excepted):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(x,y)
        print(result)
        # 实际结果 对比 预期结果
        assert result ==excepted
import pytest

# 测试模块
from pythoncode.calculator import Calculator


class TestCalculator:

    @pytest.fixture(autouse=True)
    def start_count(self):
        print("【开始计算】")
        yield
        print("【结束计算】")

    @pytest.fixture(scope="class", autouse=True)
    def end_testing(self):
        yield
        print("【结束测试】")

    test_cases = [(1, 1, 2), (-0.01, 0.02, 0.01), (10, 0.02, 10.02)]

    @pytest.mark.p0
    @pytest.mark.parametrize("a, b, sum", test_cases)
    def test_add0(self, a, b, sum):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(a, b)
        print(result)
        # 实际结果 对比 预期结果
        assert result == sum

from day1_20220410_my.pythoncode.calculator import Calculator
import pytest

def teardown_module():
print("===== 结束测试 =====")

class TestCalculator:

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

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

@pytest.mark.p0
@pytest.mark.parametrize("a,b,c", [(1, 1, 2), (-0.01, 0.02, 0.01), (10, 0.02, 10.02)])
def test_add0(self,a,b,c):
    # 测试相加方法
    calc = Calculator()  #实例化
    result = calc.add(a,b)
    print(f"本次相加结果为{result}")
    # 实际结果 比对 预期结果
    assert result == c

@pytest.mark.p1
@pytest.mark.parametrize("a,b,c", [(99, 98.99, 197.99), (-98.99, -99, -197.99), (-99, -98.99, -197.99),
                                   (99.01,0,"")])
def test_add1(self, a, b, c):
    # 测试相加方法
    calc = Calculator()  # 实例化
    result = calc.add(a, b)
    print(f"本次相加结果为{result}")
    # 实际结果 比对 预期结果
    assert result == c
  1. 测试代码有些冗余
  2. Calculator()放在setup

没有添加标签

未添加标签

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""

# 测试模块
import pytest

from pythoncode.calculator import Calculator


class TestCalculator:
    def setup_class(self):
        self.calc = Calculator()

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

    def setup(self):
        print("【开始计算】")

    def tear_down(self):
        print("【结束计算】")

    @pytest.mark.parametrize("a, b, rez", [(1, 1, 2), (-0.01, 0.02, 0.01), (10, 0.02, 10.02)])
    @pytest.mark.P0
    def test_add0(self, a, b, rez):
        # 实际结果 对比 预期结果
        assert rez == self.calc.add(a, b)

    @pytest.mark.parametrize("a, b", [(99.01, 0), (-99.01, -1), (2, 99.01), (1, -99.01)])
    @pytest.mark.P1
    def test_add1(self, a, b):
        # 实际结果 对比 预期结果
        print(self.calc.add(a, b))
        assert self.calc.add(a, b) == "参数大小超出范围"

    @pytest.mark.parametrize("a, b",
                             [("文", 9.3), (4, "字"), ("nu", 0.2), (30, "t"), ("*&", 0.2), (21.45, "@"), ("", 20.93),
                              (-3, ""), (" ", 3.14), (-90, " ")])
    @pytest.mark.P1
    def test_add2(self, a, b):
        with pytest.raises(TypeError) as e:
            # 实际结果 对比 预期结果
            self.calc.add(a, b)
        assert e.type is TypeError
import pytest
# 测试模块
from pythoncode.calculator import Calculator


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

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

    base_biz_content_1 = [
        ([1, 1], 2),
        ([-0.01, 0.02], 0.01),
        ([10, 0.02], 10.02)
    ]
    base_biz_content_2 = [
        ([98.99, 99], 197.99),
        ([99, 98.99], 197.99),
        ([-98.99, -99], -197.99),
        ([-99, -98.99], -197.99),
        ([99.01, 0], "参数大小超出范围"),
        ([-99.01, 0], "参数大小超出范围"),
        ([2, 99.01], "参数大小超出范围"),
        ([1, -99.01], "参数大小超出范围")
    ]
    @pytest.mark.p0
    @pytest.mark.parametrize('arg,expeted', base_biz_content_1)
    def test_add0(self,arg,expeted):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(arg[0], arg[1])
        # 实际结果 对比 预期结果
        assert result == expeted

    @pytest.mark.p1
    @pytest.mark.parametrize('arg,expeted', base_biz_content_2)
    def test_add1(self,arg,expeted):
        # 测试相加方法
        calc = Calculator()
        result = calc.add(arg[0], arg[1])
        # 实际结果 对比 预期结果
        assert result == expeted

测试模块

from pythoncode.calculator import Calculator
import pytest

class TestCalculator:

def setup(self):
    self.calc = Calculator()
    print("开始计算")

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

@pytest.mark.run(order=1)
@pytest.mark.parametrize('a,b,c',[(1, 1, 2),(1.1, -2.2, -1.1),(-1.1,2.2,1.1),(99,-99.01,-0.01),(-99,99.01,0.01)])
def test_add_ture(self, a, b, c):
    self.calc.add(a,b)
    assert Calculator().add(a,b) == c

@pytest.mark.run(order=2)
@pytest.mark.parametrize('a,b',[(99.01,0),(-99.01,-1),('abc',2),(22,'今天')])
def test_add_false(self, a, b):
    self.calc.add(a, b)
    assert Calculator().add(a,b) == "参数大小超出范围"
from pythoncode.calculator import Calculator
import pytest

success_data = [{"num1": 1, "num2": 1, "res": 2},
                {"num1": -0.01, "num2": 0.02, "res": 0.01},
                {"num1": 10, "num2": 0.02, "res": 10.02}
                ]
data2= [
    {"num1": 98.99, "num2": 99, "res": 197.99},
    {"num1": 99, "num2": 98.99, "res": 197.99},
    {"num1": -98.99, "num2": -99, "res": -197.99},
    {"num1": -99, "num2": -98.99, "res": -197.99},
    {"num1": 99.01, "num2": 0, "res": "参数大小超出范围"},
    {"num1": -99.01, "num2": -1, "res": "参数大小超出范围"},
    {"num1": 2, "num2": 99.01, "res": "参数大小超出范围"},
    {"num1": 1, "num2": -99.01, "res": "参数大小超出范围"},
    {"num1": "文", "num2": 9.3, "res": "参数大小超出范围"},
    {"num1": 4, "num2": "字", "res": "参数大小超出范围"},
    {"num1": 2, "num2": 99.01, "res": "参数大小超出范围"},
    {"num1": 1, "num2": -99.01, "res": "参数大小超出范围"}
]
data3 = [
    {"num1": "", "num2": 20.93, "res": "参数大小超出范围"},
    {"num1": -3, "num2": "", "res": "参数大小超出范围"}
]

class TestCalculator:
    def setup(self):
        # 测试相加方法
        self.calc = Calculator()
        print("开始计算")

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


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

    @pytest.mark.p0
    @pytest.mark.parametrize("data", success_data)
    def test_add0(self, data):
        result = self.calc.add(data["num1"], data["num2"])
        assert result == data["res"]

    @pytest.mark.p1
    @pytest.mark.parametrize("data", data2)
    def test_add1(self, data):
        result = self.calc.add(data["num1"], data["num2"])
        assert result == data["res"]

    @pytest.mark.p2
    @pytest.mark.parametrize("data", data3)
    def test_add2(self, data):
        result = self.calc.add(data["num1"], data["num2"])
        assert result == data["res"]