import pytest
import yaml
from pythoncode
.calculator
import Calculator
def get_datas():
with open ("data/data.ymal", encoding
= "utf-8") as f
:
data
= yaml
.safe_load
(f
)
return data
class TestCalc:
@pytest
.mark
.run
(order
= 2)
@pytest
.mark
.parametrize
('a,b,expect', get_datas
()['add']['data'], ids
= get_datas
()['add']['ids'])
def test_add(self
, get_calc
,a
, b
, expect
):
result
= get_calc
.add
(a
, b
)
assert result
== expect
@pytest
.mark
.run
(order
= 1)
@pytest
.mark
.parametrize
('a,b,expect',get_datas
()['div']['data'], ids
= get_datas
()['div']['ids'])
def test_div(self
,get_calc
,a
,b
,expect
):
if b
== 0:
try:
get_calc
.div
(a
, b
)
except ZeroDivisionError
as e
:
print("除数不能为0")
else:
result
= get_calc
.div
(a
, b
)
assert result
== expect
import pytest
from pythoncode
.calculator
import Calculator
@pytest
.fixture
(scope
= "module")
def get_calc():
calc
= Calculator
()
print("计算开始")
yield calc
print("计算结束")
转载请注明原文地址: https://lol.8miu.com/read-22294.html