This paper is the course study notes of excellent students in Hogwarts Testing Institute. We want to communicate with advanced students at the end of the text.
** 1. What is data driven**
What is parameterization? What is data driven? Some people often don't understand their relationship. Let's talk about personal understanding. Let's first look at the two most common scenarios in the test:
Login: different user names, passwords and combinations need to be tested for login scenarios. Multiple use cases may be generated under normal arrangement and combination
Search: different search conditions produce different search results. Search is also a common test item. A single search parameter or a combination of multiple search parameters; Multiple use cases will also be generated.
The above two scenarios have one thing in common, that is, the execution steps of the test remain unchanged, but only the input test data. Then two concepts are introduced - parameterization and data-driven
Parameterization: when writing automation use cases, we have many methods. Generally, we will transfer data to methods through parameters instead of directly writing "dead" in methods. Therefore, data transfer between methods is carried out through parameterization, and parameterization is used to correspond data to variables; For example, our login account password is set in the parameter, and then the parameter is passed to the method.
public MainPage login(String username, String password) { sendKeys(inputUsername,username); sendKeys(inputPassword,password); click(loginBtn); return new MainPage();}
Data driven
: change the data source in parameterization into reading from the outside. The parameters have a place to store data, which can be used when the use case is executed; The data storage place can be the array and hashmap defined by us, or it can be read from external files (excel, csv, xml, yaml, etc.).
For example, in the above search case, we can put the search conditions into an external file. Each time we execute the search case, we can obtain data from the file and perform different search tests according to the obtained data.
- - Washing liquid- - Hat- - glove
To sum up:
Data driven
It is a design idea of automated testing framework, and parameterization is a means to realize data-driven. We use parameterization to complete data-driven, so as to separate data from script, and increase the maintainability of the framework and the reusability of script.
** 2. Why data driven**
2.1 test data
During the execution of testing, many processes need to change dynamically. If each change requires coding deployment, the whole execution process will be long;
For business test engineers, there is a certain threshold for maintaining automation code, and they need to be familiar with the programming language and the structure of test framework;
The data driver is defined and the changed data is put into the configuration file for maintenance, which is not only convenient (there is no need to find the corresponding code to modify the deployment), but also reduces the maintenance threshold (business testing only needs to modify the data in the configuration file)
2.2 test steps
The data drive of test data is roughly the same as that of test data, which is mainly to facilitate business test and maintenance, reduce the maintenance threshold and the risk of error in code modification and deployment; When modifying the configuration file, the whole business behavior and abstraction do not need to be changed. Of course, it will be "better" to use it with PO in UI automation.
2.3 dynamic generation test steps
It is difficult to record test steps manually and generate code directly. You can generate step configuration files and let the code read the configuration files to complete automatic playback; (I have only learned about this aspect for the time being, but have not yet implemented it. It can be realized in theory.)
** 3. Where to do data driven**
3.1 where not to do data-driven
**Don't do a lot of data driving in test cases:
**
The use case can clearly show the business execution scenario through the call of PO, and the business is the core of the use case; Once a large number of data drivers are used in use cases, such as calling various yaml, csv and other data files, the readability of use cases will become worse and the maintenance complexity will become higher;
3.2 where can I do data-driven
1. Data driven of test data
2. Data driven test steps
- Locator
- Behavior flow
3. Data driven assertion
** 4. How to do data driven**
4.1 selection of data format
We need to store data in files. Different files have different data formats. So what choice do we make?
- Comparison of files in different data formats
From the above comparison results, Jason and YAML have better support and writing degree for data structure; However, YAML
The writing method of is more concise and can be annotated, so the most recommended is (you can guess ~) from the position in the table)... YAML in C!
So what is YAML and how to use it? Let's have a brief look
4.2 use of yaml files
Syntax of yaml
- Case sensitive
- Use indentation to represent hierarchical relationships
- Tab key is not allowed when indenting, only spaces are allowed.
- The number of indented spaces is not important, as long as the elements of the same level are aligned to the left
- #Indicates a comment
Three data structures supported by yaml
- scalars: single, non separable values, such as numbers, strings, Booleans, etc
- Object: a collection of key value pairs, also known as mapping / hashes / dictionary
#Key value pair form key: value#Inline object person: {Name: Allen, age: 25}
-
Array: a set of values arranged in order, also known as sequence / list
#with-The beginning is represented as the value in an array- A- B- C#The array is embedded with a nested subarray, which is indented with a space -- a - AA -- B - BB
- Objects and arrays can be used together to form a composite structure
languages: - Ruby - Perl - Python websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org
4.3 data reading jackson
Now that you have a place to store data, you need to read the data. Here we will introduce another helper, the jackson Library of Java
jackson is a Java library. jackson databind and 'jackson datafOrmats' are used most-
text is used to process json and yaml ` data formats respectively. It can establish a mapping relationship between the data in the file and the objects in Java,
Associate a file data with a type and create an instance of a class. On the contrary, you can also write an object to the file.
4.3.1 jackson-databind
Let's first look at Jackson databind's operation on json files
Add maven dependency
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9.3</version></dependency>
- Write json file
1) First create a class containing variables name and age
public class TestFileSource { public String name; public int age; }
2) Create a unit test, create an ObjectMapper object, and call writeValue to write the json file
@Testvoid writeJson() throws IOException { ObjectMapper mapper = new ObjectMapper(); TestFileSource testFileSource = new TestFileSource(); mapper.writeValue(new File("..\\demo.json"),testFileSource);}
3) Get demo The result of the json file. From the result, you can see that the variables in the TestFileSource class have been written to the json file
{"name":null,"age":0}
- Read json file
1) Create a unit test, create an ObjectMapper object, and call the readValue method to read the data of the json file
@Testvoid readJson() throws IOException { ObjectMapper mapper = new ObjectMapper(); TestFileSource testFileSource = mapper.readValue(TestFileSource.class.getResourceAsStream("/demo.json"), TestFileSource.class); System.out.println(testFileSource); System.out.println(testFileSource.age);}
2) Read results
-
ApiDemos.testcase.TestFileSource@4562e04d 0
-
Output beautiful json format
**
**1) Create a unit test, create an ObjectMapper object, and call writerwithdefaultprettyprinter() The writevalueasstring method can output the specified object in json data format
@Testvoid prettyPrintJson() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); // pretty print String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new TestFileSource()); System.out.println(json);}
2) Print results
{ "name" : null, "age" : 0}
Reference link
Jackson databindgithub address:
https://github.com/FasterXML/jackson-databind
4.3.2 jackson-dataformats-text
Let's look at Jackson datafOrmats - text, which is a library that can operate on YAML, CSV, Properties and XML files. It is also the most commonly used library at present, but we only focus on its operation on YAML files here
- Add maven dependency
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.9.8</version></dependency>
- Read YAML file
To read the yaml file, the most important thing is to add new YAMLFactory() to the new ObjectMapper object, so as to successfully switch to yaml
Operation status, and then use the readValue method to complete the data reading of yaml file
1) Create YAML file
-
name: allenage: 11
2) Create ObjectMapper object and set new YAMLFactory()
@Testvoid readYaml() throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); TestFileSource testFileSource = mapper.readValue(TestFileSource.class.getResourceAsStream("/demo2.yaml"), TestFileSource.class); System.out.println(testFileSource); System.out.println(testFileSource.age);}
Print results
-
ApiDemos.testcase.TestFileSource@ba2f4ec11
In the readValue method, you can see that the first parameter is the file address, and the second parameter is the essence! We can give an object type or a two-dimensional array to generate a mapping relationship and bind the file data with our object to facilitate data reading.
As in the above example, we call the age variable through the instantiated object of TestFileSource.
- Output beautiful YAML format
The output method is basically the same as that of json. You only need to add new YAMLFactory() when you add a new ObjectMapper object
1) Create classes and class member variables that contain scalars, arrays, and hashes
public class TestFileSource { public String name = "tester"; public int age = 2; public Object[][] arr= {{1,2,3,},{"a","b","c"}}; public HashMap<String,Object> map = new HashMap<String, Object>(){ { put("name","tester"); put("sex","male"); } };}
2) Create unit tests, create ObjectMapper objects, and add new YAMLFactory()
Parameter, call writerwithdefaultprettyprinter() The writevalueasstring method can output the specified object in yaml data format
@Testvoid prettyPrintYaml() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // pretty print String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new TestFileSource()); System.out.println(json);}
3) Print results
---name: "tester"age: 2arr:- - 1 - 2 - 3- - "a" - "b" - "c"map: sex: "male" name: "tester"
Reference link Jackson datafOrmats text GitHub address:
https://github.com/FasterXML/jackson-dataformats-text
** _5.
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Video data collection: https://qrcode.testing-studio.com/f?from=CSDN&url=https://ceshiren.com/t/topic/15844
Click for more information