1.下載 DatabaseLibrary 庫
- pip install robotframework-databaselibrary
2.下載 pymysql 庫(作為中間件)
- pip install pymysql
3.工程中導入 DatabaseLibrary 庫
4.數據測試常用關鍵字
- Connect To Database 【dbapiModuleName=dbapiModuleName | dbName=dbName | dbUsername=dbUsername | dbPassword=dbPassword | dbHost=dbHost | dbPort=dbPort】
- 連接數據庫
- Table Must Exist 【tableName】
- 判斷表存在
- Check If Exists In Database 【sql】
- 判斷查詢結果的數據存在
- Check If Not Exists In Database【sql】
- 判斷查詢結果的數據不存在
- Delete All Rows From Table【tableName】
- 刪除表中所有數據
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 Table Must Exist student #判斷student存在 Check If Exists In Database select * from student where name = "zhangsan" #判斷查詢結果數據存在 Check If Not Exists In Database select * from student where name = "zhengying" #判斷查詢結果數據不存在 Delete All Rows From Table student_copy #刪除student_copy表中所有數據
- Disconnect From Database
- 斷開數據庫連接
- Execute Sql Script 【sqlScriptFileName 】
- 執行 sql 腳本文件
- 注意中文編碼問題,解決辦法如下
- Execute Sql String 【sql】
- 執行 sql 語句
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 Execute Sql Script C:/Users/Administrator/Desktop/sql.txt #通過腳本文件進行執行sql(注意轉義) Execute Sql String INSERT INTO student(name,age,class)VALUES("qian",23,3),("sun",20,1) #執行sql語句
- Query 【sql】
- 返回查詢語句的結果
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 @{result} Query select * from student where name = "lisi" #查詢數據且返回查詢結果 Log Many ${result} #結果如下 20200312 15:20:10.798 : INFO : [(2, 'lisi', 19, '3')]
- Row Count 【sql】
- 返回查詢結果的總行數
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 ${count} Row Count select * from student #返回查詢結果的總行數 log ${count}
- Row Count Is 0 【sql】
- 判斷查詢結果為 0 行
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 Row Count Is 0 select * from student where name = "lisi" #判斷查詢結果為 0 行數據,如果結果為 0 行數據則 pass,否則fail
- Row Count Is Equal To X 【sql | numrows】
- 判斷查詢結果數據 =X 行
- Row Count Is Greater Than X 【sql | numrows】
- 判斷查詢結果數據 >X 行
- Row Count Is Less Than X 【sql | numrows】
- 判斷查詢結果數據 <X 行
Connect To Database pymysql study root 123456 192.168.1.110 3306 #連接study數據庫 Row Count Is Equal To X select * from student where name = "lisi" 1 #判斷查詢結果數據為1行 Row Count Is Greater Than X select * from student where class = "2" 1 #判斷查詢結果數據大於1行 Row Count Is Less Than X select * from student where class = "2" 3 #判斷查詢結果數據小於3行