Java and TestNG use XML as data source to explain the data-driven example in detail

Java and TestNG use XML as data source to explain the data-driven example in detail

TestNG is very powerful. It can be data-driven by @ DataProvider. The data source files can be excel, XML, yaml, or even TXT text. Take XML as an example:

Note: the return value type of @ DataProvider can only be object [] [] and iterator < Object > []

TestData. xml:

Parse XML with Dom4j, parserxml Java file:

Then convert the parsed list into object [] [] type data and combine it in @ DataProvider.

TestData. Java file:

Then test through the test file:

TestDataProvider. Java file:

Let's go back and analyze the XML file. There are two testmethod1 nodes, one for testmethod2, one for testmethod3, and one for testmethod4, in testdataprovider In the java file, four test functions testmethod1, testmethod2, testmethod3 and testmethod4 are defined, and testmethod4 does not use DataProvider, so the final running result should be that testmethod1 runs twice, testmethod2, testmethod3 and testmethod4 run once respectively. The results are as follows:

In other words, in this way, you only need to write the test function first, and then define the data in the XML file, so as to control whether the function runs, the number of runs and the running data.

OK,Let's try.....

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>