######################test_calc_new.py##########################
class TestCalc:
def test_add(self,get_calc,get_datas):
result = None
try:
result = get_calc.add(get_datas[0],get_datas[1])
#print(result)
if isinstance(result,float):
result = round(result,2)
except Exception as e:
print(e)
assert result == get_datas[2]
########################conftest.py#########################
yaml_file_path = os.path.dirname(__file__) + "/cal.yml"
with open(yaml_file_path,'rb') as f:
datas = yaml.safe_load(f)
print(datas)
add_datas = datas["add_datas"]
add_ids = datas["add_ids"]
@pytest.fixture(params=add_datas, ids=add_ids)
def get_datas(request):
print("开始计算")
data = request.param
print(f"request.param是:{data}")
yield data
print("结束计算")
#####################cal.yml里的########################
add_datas:
- [3,5,8]
- [-1,-2,-3]
- [10000,10000,20000]
- [0.1,0.2,0.3]
- ["", 5, "请输入数字"]
add_ids:
- "int"
- "minus"
- "bigint"
- "float"
- "exceip"
########################### calculator.py####################
class Calculator:
def add(self,a,b):
return a+b
def sub(self,a,b):
return a-b
def mul(self,a,b):
return a*b
def div(self,a,b):
return a/b