In the process of automation, we often need to connect different databases and perform many different operations on the databases. In the robot framework, the database library is provided to operate the database. We can install the database library according to the instructions on the official website and access it in the browser http://franz-see.github.io/Robotframework-Database-Library/ Page, you can see the installation instructions and API introduction of the library.
When using the database library, you need to import the database in advance in the test suite before you can use the keywords in the database library in the use case. Here, taking MySQL database as an example, this paper describes the use of the database library
To link to MySQL, in addition to importing the databaselibrary library, you need to install the pure Python MySQL client library https://github.com/PyMySQL/PyMySQL Download the library and install it, or install it directly through the CMD command line PIP install pymysql
1.1.1 how to connect to the database
1. You can link a MySQL database through the connect to database keyword in the databaselibrary library. Here, take linking the native MySQL library as an example
Database user name: root
Database password: root
Mysql database port: 3306
Database name: World
Connect To Database pymysql world root root localhost 3306
2. You can also connect to the MySQL database by using the connect to database using custom params keyword
Connect To Database Using Custom Params pymysql database=‘world‘, user=‘root‘, password=‘root‘, host=‘localhost‘, port=3306
1.1.2 how to disconnect database
You can disconnect the database through the keyword disconnect from database. When we operate the database, we must not forget to disconnect the database after the operation is completed
Connect To Database pymysql world root root localhost 3306
Disconnect From Database
2.1.3 how to query tables in database
The query keyword can be used to query the tables in the database. Here, taking the query of a table in the MySQL database as an example, we execute select * from city limit 5 in the world database
Let's first look at the query results in the SQL window
Then we use the query keyword to query
Connect To Database Using Custom Params pymysql database=‘world‘, user=‘root‘, password=‘root‘, host=‘localhost‘, port=3306
@{result} Query SELECT * FROM city LIMIT 5;
Log Many @{result}
Disconnect From Database
2.1.4 how to insert and delete data
You can use the execute SQL string keyword to perform database insert and delete operations
1. First of all, let's look at how to insert data into the database. Here, take inserting a record into the table city as an example, and execute into city (name, country code, District, population) values ("Beijing", "zh", "China", 217100) is executed through the keyword execute SQL string
Connect To Database Using Custom Params pymysql database=‘world‘, user=‘root‘, password=‘root‘, host=‘localhost‘, port=3306
Execute Sql String INSERT INTO city(NAME,countrycode,district,population) VALUES(‘beijing‘ ,‘ZH‘,‘China‘,217100)
Disconnect From Database
In the SQL window, query whether the inset statement just executed is successful.
We can see that the data has been inserted successfully.
2. Then let's take a look at how to delete the data in the table. We delete the data of "Beijing", "zh", "China" and 217100 inserted above from the database
Connect To Database Using Custom Params pymysql database=‘world‘, user=‘root‘, password=‘root‘, host=‘localhost‘, port=3306
Execute Sql String delete from city where NAME=‘beijing‘
Disconnect From Database
In the SQL window, check whether the data has been deleted successfully
From the query results, we can see that the data has been successfully deleted
2.1.5 how to execute database script file
In automatic testing, we often need to construct data or initialize the data in the library. However, if we write the database script to be executed in the use case every time, it will be very difficult to maintain. Therefore, we need to directly execute the database script file. In the database library, we can use execute SQL Script keyword to execute the database script file.
Here to execute the script.sql For example, in the script.sql In the script, put the statement that needs to be executed
Connect To Database Using Custom Params pymysql database=‘world‘,user=‘root‘, password=‘root‘, host=‘localhost‘, port=3306
Execute Sql Script f:/ script.sql
Disconnect From Database
After successful execution, query the database and find that the script has been successfully executed and the data has been successfully inserted
2.1.6 Database Other Operations keywords
Key words |
Usage description |
Check If Exists in Database |
Check that the database query has a return result, and if there is a return result, the use case executes successfully or the execution fails, as an example:
Check If Exists in Database |
SELECT * from city WHERE name= ' Beijing ' OR name= ' Shanghai ' |
|
Check If not Exists in Database |
Check whether the database query has a return result, if there is a return result, the use case execution fails, otherwise the execution succeeds, example:
Check If not Exists in Database |
SELECT * from city WHERE name= ' Beijing ' and Name= ' Shanghai ' |
|
Delete all Rows from Table |
Delete all data from a table in a database, example:
Delete all Rows from Table |
World |
|
Description |
Describes the query results for the database, example: tr>
@{result} |
Description |
SELECT * from city WHERE name= ' Beijing ' or name= ' Shanghai ' |
Log many |
< p>@{result} |
|
|
Row Count |
Statistics SQL query returns the number of records, example:
${rowcount} |
Ro W Count |
Select * from the city WHERE name= ' Beijing ' or name= ' Shanghai ' /p> |
Log |
${rowcount} |
|
&N BSP; |
Row Count is 0 |
Check that the number of records returned by the SQL query is 0, example: tbody>
Row Count is 0 |
SELECT * from city WHERE name= ' Beijing ' or name= ' Shanghai ' |
|
Row Count is Equal to X |
Check if the number of records returned by the SQL query equals a value, example:
Row Count is Equal to X |
SELECT * from the city WHERE NAM E= ' Beijing ' or name= ' Shanghai ' |
1 |
|
Row Count is Greater Than X |
Check if the number of records returned by the SQL query is greater than a certain value, example:
Row Count is Greater Than X |
SELECT * from City WHERE Name= ' Beijing ' or name= ' Shanghai ' |
1 |
|
Row Count is less Than X |
Check that the number of records returned by the SQL query is less than a certain value, example:
Row Count is less Than X |
SELECT * from city WHERE name= ' Beijing ' or name= ' Shanghai ' |
1 |
|
Table must Exist |
To determine if a table exists in the database, example:
|
"The original is owned by the author, welcome reprint, but retain the copyright, and reproduced, need to indicate the source "
Robotframework Automated test framework-databaselibrary use of libraries (operations on databases)