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
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
- 测试代码有些冗余
-
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"]
from pythonCode.calculator import Calculator
import pytest
class TestCalculator:
def teardown_class(self):
print("结束测试")
def setup_method(self):
print("开始计算")
def teardown_method(self):
print("结束计算")
@pytest.mark.parametrize("first, second, excepted", [
pytest.param(1, 1, 2, marks=pytest.mark.p0),
pytest.param(-0.01, 0.02, 0.01, marks=pytest.mark.p0),
pytest.param(10, 0.02, 10.02, marks=pytest.mark.p0),
pytest.param(98.99, 99, 197.99, marks=pytest.mark.p1),
pytest.param(99, 98.99, 197.99, marks=pytest.mark.p1),
pytest.param(-98.99, -99, -197.99, marks=pytest.mark.p1),
pytest.param(-99, -98.99, -197.99, marks=pytest.mark.p1),
pytest.param(99.01, 0, "参数大小超出范围", marks=pytest.mark.p1),
], ids=["整数+整数", "负浮点数+正浮点数", "正整数+浮点数", "浮点数+整数", "整数+浮点数", "负浮点数+负整数", "负整数+负浮点数", "参数超出范围"])
def test_add(self, first, second, excepted):
calc = Calculator()
assert calc.add(first, second) == excepted
在此处键入或粘贴代码
练习1:
被测试代码为:
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
用例设计为:
from pythoncode.calculator import Calculator
import pytest
class TestCalculator:
def setup_class(self):
print(‘创建实例化Calculator对象’)
self.calc = Calculator()
def setup(self):
print('开始计算')
def teardown(self):
print('结束计算')
def teardown_class(self):
print('测试结束')
#冒烟测试用例
@pytest.mark.parametrize('a,b,expected',[[1,1,2],[-0.01,0.02,0.01],[10,0.02,10.02]],ids=['整数相加','浮点数相加','整数和浮点数相加'])
@pytest.mark.p0
def test_add0(self,a,b,expected):
result = self.calc.add(a, b)
print(result)
assert result == expected
#有效边界值和无效边界值
@pytest.mark.parametrize('a,b,expected', [[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,'参数大小超出范围']],
ids=['有效边界值1','有效边界值2','有效边界值3','有效边界值4','参数大小超出范围1','参数大小超出范围2','参数大小超出范围3','参数大小超出范围4'])
@pytest.mark.p1
def test_add1(self,a,b,expected):
result=self.calc.add(a,b)
print(result)
assert result==expected
# def test_add2(self):
# try:
# result=self.calc.add('中',9.3)
# except TypeError as e:
# print('异常信息:')
# print(e)
# def test_add2(self):
# with pytest.raises(TypeError) as e:
# result = self.calc.add('中', 9.3)
# print('异常信息:')
# print(e) #为什么需要对齐with才可以打印呢?---放在with里面是执行try里面的代码,print(e)应该是在捕获了之后才能打印
#异常情况的处理
@pytest.mark.parametrize('a,b,excepted',[['中',9.3,TypeError],['9.3','文',TypeError]],ids=['中文1','中文2'])
@pytest.mark.p2
def test_add2(self,a,b,excepted):
with pytest.raises(excepted) as e:
result = self.calc.add(a, b)
assert result==excepted
-- coding: utf-8 --
@Time : 2022-04-11 10:45
@File : test_aa.py
import yaml
import pytest
from pythoncode.Calculator import Calculator
def get_data():
‘’‘获取yaml中的测试数据’’’
with open(’./data.yaml’,‘r’,encoding=‘utf-8’) as f:
datas = yaml.safe_load(f)
print(datas)
return datas
class TestCalculator():
def setup(self):
print(“开始计算”)
def teardown(self):
print(“结束计算”)
def setup_class(self):
self.call = Calculator()
def teardown_class(self):
print(“结束测试”)
@pytest.mark.P0
@pytest.mark.parametrize(‘a,b,expect’,get_data().get(“P0”).get(“data”),
ids=get_data().get(“P0”).get(“ids”))
def test_add00(self,a,b,expect):
res = self.call.add(a,b)
assert res == expect
@pytest.mark.P1
@pytest.mark.parametrize(‘a,b,expect’,get_data().get(“P1”).get(“data”),ids=get_data().get(“P1”).get(“ids”))
def test_add01(self,a,b,expect):
res = self.call.add(a,b)
assert res == expect
@pytest.mark.P2
@pytest.mark.parametrize(‘a,b,expect’,get_data().get(“P2”).get(“data”),ids=get_data().get(“P2”).get(“ids”))
def test_add02(self,a,b,expect):
with pytest.raises(eval(expect)) as e:
res = self.call.add(a,b)
assert res == expect
练习2:
from pythoncode.calculator import Calculator
import pytest
import yaml
def get_datas(level):
with open(’./datas.yml’,encoding=‘utf-8’) as f:
result=yaml.safe_load(f)
print(result)
add_datas=result.get(‘add’).get(level).get(‘datas’)
add_ids = result.get(‘add’).get(level).get(‘ids’)
print(f"{level}级别的datas为: {add_datas}")
print(f"{level}级别的ids为: {add_ids}")
return[add_datas,add_ids]
class TestCalculator:
add_p0_datas=get_datas(‘P0’)[0]
add_p0_ids=get_datas(‘P0’)[1]
print(add_p0_datas)
add_p1_datas = get_datas(‘P1’)[0]
add_p1_ids = get_datas(‘P1’)[1]
add_p2_datas = get_datas(‘P2’)[0]
add_p2_ids = get_datas(‘P2’)[1]
def setup_class(self):
print(‘创建实例化Calculator对象’)
self.calc = Calculator()
def setup(self):
print('开始计算')
def teardown(self):
print('结束计算')
def teardown_class(self):
print('测试结束')
#冒烟测试用例
@pytest.mark.parametrize('a,b,expected',add_p0_datas,ids=add_p0_ids)
@pytest.mark.p0
def test_add0(self,a,b,expected):
result = self.calc.add(a, b)
print(result)
assert result == expected
#有效边界值和无效边界值
@pytest.mark.parametrize('a,b,expected', add_p1_datas,
ids=add_p1_ids)
@pytest.mark.p1
def test_add1(self,a,b,expected):
result=self.calc.add(a,b)
print(result)
assert result==expected
#异常情况的处理
@pytest.mark.parametrize('a,b,excepted',add_p2_datas,ids=add_p2_ids)
@pytest.mark.p2
def test_add2(self,a,b,excepted):
with pytest.raises(eval(excepted)) as e:#eval让字符串变成一个表达式,完成运算,把它转成了一个类型
result = self.calc.add(a, b)
assert result==excepted