Switching of multiple data sources in Java automated testing (example explanation)

Data driven is a very important concept in automated testing. When the data is separated from the script, in the face of a vast amount of data, managing data has become a big problem, and the data source may face multiple, just as in the development process, sometimes it is necessary to connect to MySQL and sometimes SQL server. How to achieve rapid switching? In the following example, we will start with a data source and demonstrate it step by step:

I Basic writing method of data-driven with external files

1.1 when we are data-driven, we store the data in the Java attribute file: data properties

1.2 parsing the properties file

1.3 write a testbase class, which is used to store the DataProvider of TestNG

It can be seen that as long as I have a class that can provide a data object with data type: List < map < string, string > >, it can be converted into a two-dimensional array of object [] [] and can be provided to the test method to run.

1.4 a propertiesdata class appears in 1.3. Now let's implement this class

1.5 in the above, there are data analysis classes, data loading classes and basic classes provided by data. Therefore, we combine the three basic classes with the test method to form an external file as a complete example of data source:

II Implementation of changing attribute file into TXT file

2.1 if there are multiple data sources, I want to use txt to store data. TXT stores a JSON string: data txt

2.2 read out the txt file

2.3 parse the read JSON string (a jar package, gson. Jar, is needed here)

2.4 use txtdata class, that is, replace the place where propertiesdata class is used in testbase class with txtdata class

As like as two peas, 2.5 run the TestDemo test class and find the result is exactly the same as the result of the PropertiesData class.

III Implement with interface

3.1 for the above two data sources, after loading the contents in the data source and the loaded data type is: List < map < string, string > >, we only need to replace one of the data source loading classes in testbase class. In this way, we can use the interface in Java to reconstruct our code. Of course, we must have an interface first

3.2 of course, our propertiesdata class and txtdata class should implement this interface

3.3 then there will be some changes in testbase, that is, the class object generating data loading will be changed. We add a new method in testbase (this is a new way to generate class objects)

3.4 the gettestdata () method in testbase class will be changed again

3.5 run testdemo again and you will find that the result is still the same. Therefore, at this time, you only need to change the path of the data loading class.

IV Configures the path of the data loading class

4.1 at this time, we can think of writing the path of the data loading class in the configuration file properties

4.2 loading config file

4.3 improve the gettestdata () method in testbase:

4.4 run the testdemo class again, and the result is still the same. So far, we have implemented to change the content in the configuration file to select the loading data source.

V Switching of multiple data sources

5.1 if there are two test methods in a test class, after configuring the data source in the configuration file, it means that the two test methods will load the same data source. But what if we want one test method to use the data source of the property file and the other method to use the data source of TXT? That is, after global configuration, local data sources can be selected again. I will use the annotations in Java to implement it. So let's define a datasource annotation first

5.2 parsing the annotation

5.3 use of this note

5.4 if the gettestdata () method in testbase class is changed again, the value parsed by the above annotation should be used

As you can see from this code, if the test method is labeled with datasource, the annotated annotation value will prevail; otherwise, the globally configured value will prevail.

5.5 add an additional test method in testdemo to show the difference

In the above test class, there are two test methods. One uses the global configuration data source value and the other uses the annotation data source value. You can run and see the results.

Vi Engineering structure drawing:

The above article on switching multiple data sources in Java automation testing (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>