Data encapsulation of Excel test cases in Python

Little brother said the test 2021-11-25 11:45:44
data encapsulation excel test cases

å¨è¿éæå¥å¾çæè¿°

 python involves excel The test case data package has several third-party libraries . such as xlrd、xlutils、xlwt, this 3 Relationship between people

1. encapsulation excel Test case read function

encapsulation 《excel The test case 》 Read function operation steps
1. Open the workbook
workBook =xlrd.open_workbook( File path )
2. Get worksheet
workBook_sheet1=workBook.get_sheet( Indexes )
workBook_sheet2=workBook.sheet_by_name(' Worksheet name ')
workBook_sheet3=workBook.sheet_by_index( Indexes )
3. Get the number of whole rows
workBook_sheet1.row_values( Line number )
workBook_sheet3.nrows

 Instance of a
import xlrd,pprint
#1. Open the workbook , Open the workbook of the file path according to the path , Get 【workBook 】 object
workBook =xlrd.open_workbook(r'C:\Users\hlhs_cqb\Downloads\test- The test case V1.2.xls')
#2. From Workbook , obtain 【workBook_sheet*】 object
# workBook_sheet1=workBook.get_sheet(1)
workBook_sheet4=workBook.sheets()[1]
workBook_sheet2=workBook.sheet_by_name('test')
workBook_sheet3=workBook.sheet_by_index(0)
#3. Get the whole row of data , Get the value or number of rows
pprint.pprint(workBook_sheet2.row_values(1))
pprint.pprint(workBook_sheet3.nrows)
pprint.pprint(workBook_sheet4.row_values(1))

2. call excel Execute test case

call excel Execute test case operation steps
1. Read excel Test cases in , return python list ( We encapsulate a function )
2. Copy a new excel
3. For the read test case list , Make a circular send request , Return the request result in dictionary format ( We encapsulate a function )
4. The result returned for the request , and excel The expected results in , If the same , Pass the test , If it's not consistent , The test fails , The results of the comparison are written back to 【 A copy of 】excel in
5. Save the copied excel

import xlrd,json,time
from xlutils.copy import copy
from lib.caseLib import CaseManage
#1-1 open Excel, obtain 【workBook】 object
workBook=xlrd.open_workbook(r'../../data/test- The test case .xls',formatting_info=True)
#1-2 From Workbook , obtain 【workSheet】 object
workSheet=workBook.sheet_by_index(0)
#2- Object instantiation ,CaseManage It is mainly used for requests for interfaces .
cam=CaseManage()
cam.login('test','123456')
#3-1 Copy a workbook
workBookNew=copy(workBook)
#3-2 Open sheet
workSheetNew=workBookNew.sheet_by_name('test')
#1-3 Cycle the worksheet
for i in range(1,workSheet.nrows):
# 1-4 Get a specific line of data in the loop
row = workSheet.row_values(i)
#1-5 Get the number 1、5、6、7、 The value of the column .
if row[4]=='add':
# Get the fifth column of data , Convert to dictionary
data=json.loads(row[5])
name=data['name']# Get the use case name attribute from the dictionary
# Variable of use case name , Replace with a timestamp
name=name.replace('{
{caseName}}',str(int(time.time()*1000)))
# Call the use case to add an interface
dictBody = cm.add(name, data['desc'], data['display_idx'])
test = json.loads(row[6])
if (dictBody["retcode"]==test["code"]):
print(">>>>> The test passed , Use case number :",row[4],row[0])
workSheetNew.write(i,7,'PASS')
else:
print(">>>>> The test failed , Use case number :",row[4], row[0],dictBody["reason"])
workSheetNew.write(i, 7, 'FAIL')
# workSheetNew.write(i, 8, dictBody['reason'])
elif row[4]=='delete':
data = json.loads(row[5])
dictBody = cm.delete(data['id'])
test = json.loads(row[6])
if (dictBody["retcode"] == test["code"]):
print(">>>>> The test passed , Use case number :",row[4], row[0])
workSheetNew.write(i, 7, 'PASS')
else:
print(">>>>> The test failed , Use case number :",row[4], row[0])
workSheetNew.write(i, 7, 'FAIL')
elif row[4]=='list':
# Get the fifth column of data , Convert to dictionary
data=json.loads(row[5])
#excel Parameters in
dictBody=cm.list(data["pagenum"],data["pagesize"])
# Get the seventh column data assertion
test=json.loads(row[6])
if(dictBody["retcode"]==test["code"]):
print(">>>>> The test passed , Use case number :",row[4], row[0])
workSheetNew.write(i, 7, 'PASS')
else:
print(">>>>> The test failed , Use case number :",row[4], row[0])
workSheetNew.write(i, 7, 'FAIL')
# workSheetNew.write(i, 8, dictBody['reason'])
elif row[4] == 'modify':
pass
else:
print(' Undefined :'+row[4])
workBookNew.save(r'../../report/ test result .xls')

The following is the supporting information , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !

å¨è¿éæå¥å¾çæè¿°
Last : It can be in the official account : Programmer Xiaohao  ! Get a free copy of 216 Page software testing engineer interview guide document information . And the corresponding video learning tutorial is free to share !, It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .

Learn not to fight alone , It's best to keep warm , Achieve each other and grow together , The effect of mass effect is very powerful , Let's learn together , Punch in together , Will be more motivated to learn , And you can stick to it . You can join our testing technology exchange group :310355728( There are various software testing resources and technical discussions )

Friends who like software testing , If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One button, three links !

 

版权声明
本文为[Little brother said the test]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/11/20211109010518144c.html

  1. Utilisez Python pour proposer l'année de la colonne de date dans les deux CSV, faire une nouvelle colonne, puis combiner les deux tableaux CSV en un seul tableau avec la colonne de date et le numéro d'identification.
  2. 关于#python#的问题,请各位专家解答!
  3. ***
  4. ***
  5. 關於#python#的問題,請各比特專家解答!
  6. S'il vous plaît répondre aux questions de Python!
  7. About the import of Python class
  8. Magic Python property decorator: 1 line of code makes Python methods become properties in seconds
  9. Python 音频调整音量(附代码) | Python工具
  10. Python programming ideas [series of articles]
  11. Python crawler programming idea (67): modify nodes using pyquery
  12. Python crawler programming idea (66): using pyquery to obtain node information
  13. Python crawler programming idea (65): find nodes using pyquery
  14. Python crawler programming idea (64): using CSS selectors in pyquery
  15. Python crawler programming idea (63): basic knowledge of pyquery
  16. Python crawler programming idea (62): project practice: capturing cool dog online red song list
  17. Python crawler programming idea (61): project practice: capturing rental information
  18. Python crawler programming idea (60): get CSS selector code through browser
  19. Python爬虫编程思想(85):在Python中使用非关系型数据库
  20. Volume de réglage audio Python (avec Code) | outils Python
  21. Python crawler programming idea (59): get attribute value and text with beautiful soup CSS selector
  22. Python crawler programming idea (58): nested selection nodes with beautiful soup CSS selectors
  23. Python crawler programming idea (57): basic usage of CSS selector in beautiful soup
  24. Python crawler programming idea (56): find method of beautiful soup method selector
  25. Python crawler programming idea (55): find of beautiful soup method selector_ All method
  26. Python crawler programming idea (54): use beautiful soup to select sibling nodes
  27. Python crawler programming idea (53): use beautiful soup to select the parent node
  28. Django3.0 solves the problem of error reporting in reverse parsing
  29. Precautions for Python crawler
  30. Python 3 crawler series (1) -- climbing blind date websites
  31. Python到底是什么?为什么要学Python?
  32. #yyds干货盘点#Pandas数据清洗实用指南
  33. Python打包exe文件无法运行
  34. Two common ways to save files in Python
  35. #yyds幹貨盤點#Pandas數據清洗實用指南
  36. Yyds Dry Inventory pandas Data Cleaning Practical Guide
  37. PYTHON用LSTM长短期记忆神经网络的参数优化方法预测时间序列洗发水销售数据
  38. Python集成学习:自己编写构建AdaBoost分类模型可视化决策边界及sklearn包调用比较
  39. Python 3 makes a search software
  40. Python 3 simulated microblog login
  41. Using Python 3 to make practical software for drawing modification
  42. About HTML (acceptable to Python)
  43. Python集成學習:自己編寫構建AdaBoost分類模型可視化决策邊界及sklearn包調用比較
  44. PYTHON用LSTM長短期記憶神經網絡的參數優化方法預測時間序列洗發水銷售數據
  45. Python Integrated Learning: Writing and Constructing adaboost Classification Model Visualized decision Boundary and sklearn package Calling Comparison
  46. Python prédit les données de vente de shampooing de séries chronologiques en utilisant la méthode d'optimisation des paramètres du réseau neuronal de mémoire à court et à long terme lstm
  47. [zero basics of Python to introduction] a prerequisite for Python preparatory knowledge -- basic coding specification of Python
  48. OpenCV对比度亮度变换竟能用来去水印(附Python/C++源码)
  49. [zero basics of Python to getting started] a prerequisite for Python preparatory knowledge -- installing the visualization tool pycharm
  50. The test modifies main.py in micro python
  51. Microphoton experimental circuit board based on mm32f3273 - does not work normally
  52. Run micropathon on mm32f3273 to test performance
  53. Design mm32f3277 micro Python experimental board with SD card
  54. Mm32f3277 corresponding interface files during microphoton migration
  55. Mm32f3277 microphoton experimental board design and software testing
  56. Making and testing mm32f3277 microphoton minimum circuit board
  57. Download mm32-link program automatically with Python simulated mouse
  58. A curriculum of "artificial intelligence Python machine learning and deep learning"
  59. Test the basic functions of mm32 microphoton test circuit board
  60. Test the basic functions of the mm32f3277 micro Python development board flying one by one