UI automation testing practice (2) | Data-driven test data

【Abstract】Data-driven is to drive the execution of automated tests through data changes, and ultimately cause changes in test results. Simply put, it is the application of parameterization in automated testing. The advantages of using data-driven in the test process are mainly reflected in the following points: 1. Improve the code reuse rate. The same test logic only needs to write a test case, and it can be reused by multiple test data, which improves the test code reuse rate. Utilization rate, while improving the efficiency of writing test code. 2. The efficiency of abnormal troubleshooting is high. The test framework generates a test case for each piece of data based on the test data,...


Data-driven is to drive the execution of automated tests through data changes, and ultimately cause changes in test results. Simply put, it is the application of parameterization in automated testing.
The advantages of using data-driven in the test process are mainly reflected in the following points:
1. Improve the code reuse rate. The same test logic only needs to write one test case, and it can be reused by multiple test data, which improves the test code reuse rate. Utilization rate, while improving the efficiency of writing test code.
2. The efficiency of abnormal troubleshooting is high. The test framework generates a test case for each piece of data based on the test data, and the execution process of the test cases is isolated from each other. If one of them fails, it will not affect other test cases.
3. The code maintainability is high, and the clear test framework is easy for other test engineers to read, improving the maintainability of the code.
Test cases with a small amount of data can use the parameterization of the code to achieve data-driven. In the case of a large amount of data, it is recommended that you use a structured file (such as YAML, JSON, etc.) to store the data, and then in the test case read the data.
Pytest provides the @pytest.mark.parametrize decorator for parameterization, which can be used to achieve data-driven. The code is as follows:
The above code first uses the @pytest.mark.parametrize decorator to pass two sets of data. The test results show that two test cases are executed instead of one test case. That is, Pytest will automatically generate two corresponding test cases from two sets of test data and execute them, generating two test results.
When the amount of test data is large, you can consider storing the data in structured files. Read the data in the format required in the code from the file, and pass it to the test method for execution. It is recommended that you use YAML type files to store test data. YAML is structured using dynamic fields, it is data-centric, and is more suitable for data-driven than Excel, CSV, JSON, XML, etc.
Next, we store the two sets of parameterized data above in a YAML file and create a data/searchdata.yml file with the following code: The
above code defines a data file searchdata.yml in yaml format, and a list is defined in the file , there are two sets of data in the list, and the final generated data format is: [[“alibaba”, “BABA”, 200],[“JD”, “JD”, 20]] .
The next step is to transform the parameterized data in the test case to be read from the searchdata.yml file. The code is as follows: the
above code only needs to use the yaml.safe_load() method to read the data in the searchdata.yml file. They are respectively passed to the use case test_search() method to complete data input and result verification. However, if you use Excel or CSV file format for data storage, you need to read the data from the Excel file first, and then parse it into the required format. Using YAML eliminates this process entirely.
The above, about the data drive of other links, will be shared in subsequent chapters.

 

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131047917