python学习第八天:unittest框架运行部分测试用例及excel取数

目录

1. unittest框架运行部分测试用例

2. 读取excel的数据


1. unittest框架运行部分测试用例

import unittest
import os

from XX.xx import test_1, test_2

loader = unittest.TestLoader()

# 获取测试用例目录的路径
dir_path = os.path.dirname(os.path.abspath(__file__))
case_path = os.path.join(dir_path, '测试用例模块名')

# 使用 loader 收集所有的测试用例
test_suit = loader.discover(case_path)

# 只加载注册的和登录的用例
suite_1 = loader.loadTestsFromModule(test_1)
suite_2 = loader.loadTestsFromModule(test_2)

# 初始化一个 suite
suit_all = unittest.TestSuite()
suit_all.addTests([suite_1, suite_2])


# 执行测试用例
runner = unittest.TextTestRunner()
runner.run(test_suit)

#生成测试报告
with open("test_report.txt", 'w', encoding='utf-8') as f:
    runner = unittest.TextTestRunner(f)
    runner.run(test_suit)

2. 读取excel的数据

# 导入 openpyxl
import openpyxl
from openpyxl.worksheet.worksheet import Worksheet

# 打开文件
# 如果table_1.xlsx和当前的py文件没在同一目录,需要获取table_1.xlsx的文件路径
workbook = openpyxl.load_workbook('table_1.xlsx')

# 获取表单
sheet: Worksheet = workbook['Sheet1']  #此处用法为:(变量:变量类型 = 值)

# 获取某一行,某一列的单元格
# cell() 方法得到的是一个单元格对象,不是单元格的值
table_cell = sheet.cell(row=2, column=2)

# 获取单元格中的值
print(table_cell.value)

#获取所有单元格的值
rows = list(sheet.rows)
# row为 行数据
for row in rows:
    for cell in row:
        print(cell.value)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Semih93

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值