写一个程序使用python创建excel,一共有5列分别是Test ID,Testname,steps,Testdata,except result,使用Faker库生成100条测试用例,并插入对应的excel表格中
时间: 2023-07-16 11:14:47 浏览: 66
test_python_excel_
5星 · 资源好评率100%
以下是使用Python创建Excel并生成100条测试用例的代码,其中使用了Faker库来生成虚假数据:
```python
import openpyxl
from faker import Faker
# Create a workbook and select the active worksheet
wb = openpyxl.Workbook()
ws = wb.active
# Add headers to the worksheet
ws['A1'] = 'Test ID'
ws['B1'] = 'Testname'
ws['C1'] = 'steps'
ws['D1'] = 'Testdata'
ws['E1'] = 'except result'
# Generate 100 test cases using Faker library
fake = Faker()
for i in range(2, 102):
ws.cell(row=i, column=1).value = i - 1
ws.cell(row=i, column=2).value = fake.sentence(nb_words=3)
ws.cell(row=i, column=3).value = fake.text(max_nb_chars=200)
ws.cell(row=i, column=4).value = fake.date_between(start_date='-1y', end_date='today')
ws.cell(row=i, column=5).value = fake.random_element(elements=('Pass', 'Fail'))
# Save the workbook as an Excel file
wb.save('test_cases.xlsx')
```
运行以上代码后,会在当前目录下生成一个名为 "test_cases.xlsx" 的Excel文件,其中包含100条测试用例。
阅读全文