之前我们讲到了读取excel操作,不知道的可以查看一下
https://www.cnblogs.com/yushengaqingzhijiao/p/15575765.html
我们将读取出来的数据 进行打印 查看一下是否能够正常读取
- 在测试类测试读取数据是否正常
import unittest import os from ddt import ddt, data from test_demo.class_2021_11_15 import login_check from class_excel.handle_excel import HandleExcel
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'login_cases.xlsx') exc = HandleExcel(file_path, "login") cases = exc.read_all_data() exc.close_file() print(cases)
=================================================================
C:\Users\admin\AppData\Local\Programs\Python\Python37-32\python.exe D:/pythonProject/class_excel/test_login.py
[{'user': 'test', 'password': '123456', 'check': '{"code": 0, "msg": "登录成功"}'}, {'user': 'test', 'password': 1234561212, 'check': '{"code": 1, "msg": "账号或密码不正确"}'}, {'user': 'test12221', 'password': 123456, 'check': '{"code": 1, "msg": "账号或密码不正确"}'}]
Process finished with exit code 0
- 引入测试类,并进行参数化操作
import unittest import os from ddt import ddt, data from test_demo.class_2021_11_15 import login_check from class_excel.handle_excel import HandleExcel file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'login_cases.xlsx') exc = HandleExcel(file_path, "login") cases = exc.read_all_data() exc.close_file() print(cases) @ddt class TestLogin(unittest.TestCase): @data(*cases) # * 表示需要解包,传入参数化的变量 def test_login(self, case): # 定义参数化接收的值,随便定义 # 1.测试数据 2.测试步骤 res = login_check(case['user'], case['password']) # 引入 case['user'] 即是 test 引入 case['password'] 即是 123456 由于我们有三组数据,则会运行三次 # 3.断言 self.assertEqual(res, eval(case['check'])) if __name__ == '__main__': unittest.main()
测试类代码: 文件名 : class_2021_11_15
def login_check(username=None, password=None): if username != None and password != None: if username == "test" and password == "123456": return {"code": 0, 'msg': "登录成功"} else: return {"code": 1, 'msg': "账号或密码不正确"} else: return {"code": 1, "msg": "所有数据不能为空"}
测试文件: login_cases.xlsx
表名: login