一、json数据驱动
- json文件内容
{
"case1": [1,1,2],
"case2": [3,6,9],
"case3": [100,200,300]
}
- json数据驱动使用方法
import json
import pytest
def get_json():
with open("data.json") as file:
data = json.load(file)
return list(data.values())
def my_add(x, y):
result = x + y
return result
class TestWithJson:
@pytest.mark.parametrize('x,y,expected',get_json())
def test_add(self, x, y, expected):
print(f"{x} + {y} = {expected}")
assert my_add(int(x), int(y))== int(expected)
if __name__ == '__main__':
pytest.main(["-s", "test_json.py"])
- 运行结果