CN113138922A - Method for parametrically reading Excel test data based on TestNG - Google Patents
Method for parametrically reading Excel test data based on TestNG Download PDFInfo
- Publication number
- CN113138922A CN113138922A CN202110439364.XA CN202110439364A CN113138922A CN 113138922 A CN113138922 A CN 113138922A CN 202110439364 A CN202110439364 A CN 202110439364A CN 113138922 A CN113138922 A CN 113138922A
- Authority
- CN
- China
- Prior art keywords
- excel
- test data
- dataprovider
- reading
- testng
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000012360 testing method Methods 0.000 title claims abstract description 74
- 238000000034 method Methods 0.000 title claims abstract description 35
- 238000012545 processing Methods 0.000 claims abstract description 12
- 238000012544 monitoring process Methods 0.000 claims abstract description 5
- 230000009286 beneficial effect Effects 0.000 abstract description 3
- 230000006870 function Effects 0.000 description 5
- 230000008569 process Effects 0.000 description 4
- 238000010586 diagram Methods 0.000 description 2
- 230000004075 alteration Effects 0.000 description 1
- 230000008676 import Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/3668—Software testing
- G06F11/3672—Test management
- G06F11/3684—Test management for test design, e.g. generating new test cases
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/3003—Monitoring arrangements specially adapted to the computing system or computing system component being monitored
- G06F11/302—Monitoring arrangements specially adapted to the computing system or computing system component being monitored where the computing system component is a software system
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- Computing Systems (AREA)
- Quality & Reliability (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Mathematical Physics (AREA)
- Computer Hardware Design (AREA)
- Debugging And Monitoring (AREA)
Abstract
The invention relates to a method for parametrically reading Excel test data based on TestNG, which comprises the following steps: setting a monitor in the configuration file, and starting monitoring on the target annotation; when a target annotation is monitored, calling an annotation implementation class excelDataTransformer to acquire file information submitted by the annotation; and calling an implementation class IExcell DataProvider with a uniform Excel reading function, wherein the implementation class IExcell DataProvider is used for reading test data from an Excel file according to file information, processing the test data into a standard DataProvider data format and returning the standard DataProvider data format to the TestNG test entrance. The method solves the problem that the efficiency of reading Excel data by the existing TestNG framework is low, improves the compiling efficiency of the automatic test script, and is beneficial to realizing automatic test.
Description
Technical Field
The invention relates to the technical field of automatic testing, in particular to a method for parametrically reading Excel test data based on TestNG.
Background
TestNG is a test framework widely used in automated testing, and a commonly used parameterization mode is to read an Excel data file, but TestNG ng primary annotation @ DataProvider does not deal with the parameterization mode of Excel, and writers of dataproviders need to read and write test data of Excel by themselves, so that more repeated work can be performed in the parameterized test framework, the read and write implementation process of Excel test data by each writer can be different from person to person, the uniform specification cannot be achieved, and the script writing efficiency is not high. Therefore, how to quickly read the test data in the Excel file and improve the efficiency of compiling the automatic test script becomes a key factor for realizing the automatic test.
Disclosure of Invention
In view of the above, the present invention provides a method for reading Excel test data based on TestNG parameterization, which overcomes the shortcomings of the prior art.
In order to achieve the purpose, the invention adopts the following technical scheme: a method for parametrically reading Excel test data based on TestNG comprises the following steps:
setting a monitor in the configuration file, and starting monitoring the annotation @ excelDataProvider;
when the comment @ excelDataProvider is monitored, calling an annotation implementation class excelDataTransformer to acquire file information submitted by the annotation;
and calling an implementation-like IExcell DataProvider with a uniform Excel reading function, wherein the implementation-like IExcell DataProvider is used for reading test data from an Excel file according to the file information, processing the test data into a standard DataProvider data format and returning the standard DataProvider data format to the TestNG test entrance.
Optionally, the file information includes:
file name and worksheet name.
Optionally, the annotation @ ExcelDataProvider is written based on a TestNG open source framework; the comment @ ExcelDataProvider is a comment name for reading Excel test data.
Optionally, the method further includes:
and reading the test data from the Excel file, processing the Excel test data into a format customized by a user and returning.
Optionally, the method further includes:
and reading the test data from the file of the specified type, processing the test data into a standard DataProvider data format, and returning the standard DataProvider data format to the TestNG test inlet.
By adopting the technical scheme, the method for reading Excel test data based on testNG parameterization comprises the following steps: setting a monitor in the configuration file, and starting monitoring the annotation @ excelDataProvider; when the comment @ excelDataProvider is monitored, calling an annotation implementation class excelDataTransformer to acquire file information submitted by the annotation; and calling an implementation-like IExcell DataProvider with a uniform Excel reading function, wherein the implementation-like IExcell DataProvider is used for reading test data from an Excel file according to the file information, processing the test data into a standard DataProvider data format and returning the standard DataProvider data format to the TestNG test entrance. The method can realize the rapid reading of the test data in the Excel file based on the testNG open-source framework, solves the problem of low efficiency of the prior testNG parameterization reading of the Excel data, improves the efficiency of compiling the automatic test script, and is beneficial to realizing the automatic test.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a schematic flow chart diagram provided by an embodiment of a method for parameterized reading of Excel test data based on TestNG according to the present invention;
FIG. 2 is a schematic of the invention with the addition of the annotation @ excelDataProvider;
fig. 3 is a schematic diagram of the execution result obtained by the method of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the technical solutions of the present invention will be described in detail below. It is to be understood that the described embodiments are merely exemplary of the invention, and not restrictive of the full scope of the invention. All other embodiments, which can be derived by a person skilled in the art from the examples given herein without any inventive step, are within the scope of the present invention.
FIG. 1 is a schematic flow chart of a method for parameterized reading of Excel test data based on TestNG according to an embodiment of the present invention.
As shown in fig. 1, the method for reading Excel test data based on TestNG parameterization in this embodiment includes:
s11: a monitor is arranged in a configuration file, and monitoring of the annotation @ ExcelDataProvider (namely, a target annotation) is started;
further, the configuration file is org.testng.itestnglisterner configuration file.
S12: when the comment @ excelDataProvider is monitored, calling an annotation implementation class excelDataTransformer to acquire file information submitted by the annotation;
s13: and calling an implementation-like IExcell DataProvider with a uniform Excel reading function, wherein the implementation-like IExcell DataProvider is used for reading test data from an Excel file according to the file information, processing the test data into a standard DataProvider data format and returning the standard DataProvider data format to the TestNG test entrance.
Further, the file information includes:
file name and worksheet name.
Further, the comment @ ExcelDataProvider is written based on a TestNG open source framework; the comment @ ExcelDataProvider is a comment name for reading Excel test data.
When the method is developed under a TestNG open source framework, the method comprises 5 java files and 1 configuration file, and specifically comprises the following steps:
java file 1: java, which defines an annotation ExcelDataProvider, which generally does not need to be modified unless there is a special requirement;
java file 2: java, exceldatafransformer
The file ensures that after the @ Excel DataProvider annotation is used, a code for reading the Excel file can be called, and test data in the Excel file is processed into a standard DataProvider data format and output;
java file 3: iexcel dataprovider. java
The built-in DataProvider outputs the result in an Iterator < Object [ ] > mode by default;
java file 4: java, which is an interface and can read test data from various types of files by a custom implementation class;
java file 5: java, excel file data reading implementation class, and the default of output result is Iterator < Object >;
1 configuration file: the configuration file of org.testng.itestnglistener defines a monitor for starting ExcelDataTransformer by default, and when @ ExcelDataProvider is monitored, file information submitted by annotation is acquired, so that the iexcell DataProvider-like is enabled to read test data from an Excel file according to the file information, and the test data is processed into a standard DataProvider data format and returned to a TestNG test entrance.
Specifically, the specific implementation process of the TestNG using the custom annotation @ ExcelDataProvider includes:
one, self-built spring boot (maven) engineering
Second, engineering import jar package
Dependence on pom
Downloading service.
Giving a test data file name, and storing the test data file in a/src/test/resources/TestCases directory;
writing a Test class, and if a dataprovider is required to be used, adding a note @ ExcelDataProvider only without specifying in an @ Test note, as shown in FIG. 2;
the test data in the Excel file are shown in table 1 below,
example numbering | Description of use cases | Input device | Expected value |
TestCase1 | Normal case | TestCase1 | TestCase1 |
TestCase2 | Exception case | TestCase2 | TestCase2 |
TABLE 1
The resulting execution results are shown in fig. 3.
It should be noted that, because the jar package of testng is relied on, dependence needs to be added to the pom file; since apache poi ooxml is relied upon, the dependence needs to be added in the pom file.
Further, the test data structure output by the ExcelDataProvider may be:
object [0] → Map < "use case declaration", Map < String, String > parameters >
object [1] → Map < "use case declaration", Map < String, String > parameters >
For the sake of universality, only String type parameters are provided, and a user can customize input models of various interfaces, directly convert String into corresponding input model objects, and conveniently set \ read data.
Correspondingly, the method further comprises the following steps:
and reading the test data from the Excel file, processing the Excel test data into a format customized by a user and returning.
Further, the method may further include:
and reading the test data from the file of the specified type, processing the test data into a standard DataProvider data format, and returning the standard DataProvider data format to the TestNG test inlet.
According to the method, the test data in the Excel file can be rapidly read by simply configuring the testNG open-source framework, and the problem that the efficiency of reading Excel data by testNG parameterization is low in the prior art is solved; in addition, an interface is additionally provided for the method, the expansion of the Excel test data return format is reserved, and the customized return format can be realized according to the needs of users; the method can quickly read the test data in the Excel file, improve the efficiency of compiling the automatic test script and be beneficial to realizing the automatic test.
It is understood that the same or similar parts in the above embodiments may be mutually referred to, and the same or similar parts in other embodiments may be referred to for the content which is not described in detail in some embodiments.
It should be noted that in the description of the present invention, any process or method descriptions in flow charts or otherwise described herein may be understood as representing modules, segments, or portions of code which include one or more executable instructions for implementing specific logical functions or steps of the process, and that the scope of the preferred embodiments of the present invention includes additional implementations in which functions may be executed out of order from that shown or discussed, including substantially concurrently or in reverse order, depending on the functionality involved, as would be understood by those reasonably skilled in the art of the present invention.
It should be understood that portions of the present invention may be implemented in software, firmware, or a combination thereof. In the above embodiments, the various steps or methods may be implemented in software or firmware stored in memory and executed by a suitable instruction execution system.
It will be understood by those skilled in the art that all or part of the steps carried by the method for implementing the above embodiments may be implemented by hardware related to instructions of a program, which may be stored in a computer readable storage medium, and when the program is executed, the program includes one or a combination of the steps of the method embodiments.
In addition, functional units in the embodiments of the present invention may be integrated into one processing module, or each unit may exist alone physically, or two or more units are integrated into one module. The integrated module can be realized in a hardware mode, and can also be realized in a software functional module mode. The integrated module, if implemented in the form of a software functional module and sold or used as a stand-alone product, may also be stored in a computer readable storage medium.
The storage medium mentioned above may be a read-only memory, a magnetic or optical disk, etc.
In the description herein, references to the description of the term "one embodiment," "some embodiments," "an example," "a specific example," or "some examples," etc., mean that a particular feature, structure, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the terms used above do not necessarily refer to the same embodiment or example. Furthermore, the particular features, structures, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples.
Although embodiments of the present invention have been shown and described above, it is understood that the above embodiments are exemplary and should not be construed as limiting the present invention, and that variations, modifications, substitutions and alterations can be made to the above embodiments by those of ordinary skill in the art within the scope of the present invention.
Claims (5)
1. A method for parametrically reading Excel test data based on TestNG is characterized by comprising the following steps:
setting a monitor in the configuration file, and starting monitoring the annotation @ excelDataProvider;
when the comment @ excelDataProvider is monitored, calling an annotation implementation class excelDataTransformer to acquire file information submitted by the annotation;
and calling an implementation-like IExcell DataProvider with a uniform Excel reading function, wherein the implementation-like IExcell DataProvider is used for reading test data from an Excel file according to the file information, processing the test data into a standard DataProvider data format and returning the standard DataProvider data format to the TestNG test entrance.
2. The method of claim 1, wherein the file information comprises:
file name and worksheet name.
3. The method of claim 1,
the annotation @ ExcelDataProvider is written based on a TestNG open source framework; the comment @ ExcelDataProvider is a comment name for reading Excel test data.
4. The method of any of claims 1 to 3, further comprising:
and reading the test data from the Excel file, processing the Excel test data into a format customized by a user and returning.
5. The method of any of claims 1 to 3, further comprising:
and reading the test data from the file of the specified type, processing the test data into a standard DataProvider data format, and returning the standard DataProvider data format to the TestNG test inlet.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110439364.XA CN113138922A (en) | 2021-04-23 | 2021-04-23 | Method for parametrically reading Excel test data based on TestNG |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110439364.XA CN113138922A (en) | 2021-04-23 | 2021-04-23 | Method for parametrically reading Excel test data based on TestNG |
Publications (1)
Publication Number | Publication Date |
---|---|
CN113138922A true CN113138922A (en) | 2021-07-20 |
Family
ID=76811801
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202110439364.XA Pending CN113138922A (en) | 2021-04-23 | 2021-04-23 | Method for parametrically reading Excel test data based on TestNG |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN113138922A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115982055A (en) * | 2023-02-27 | 2023-04-18 | 云筑信息科技(成都)有限公司 | Test method special for java language development program |
CN117312146A (en) * | 2023-09-26 | 2023-12-29 | 北京五一嘉峪科技有限公司 | Automatic testing method, system, computing equipment and medium for competition platform |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101882107A (en) * | 2010-06-28 | 2010-11-10 | 山东中创软件商用中间件股份有限公司 | Method and device for automatically testing WEB (World Wide Web) application |
US20120017143A1 (en) * | 2010-07-15 | 2012-01-19 | Hon Hai Precision Industry Co., Ltd. | Electronic device and method for data management |
CN111459821A (en) * | 2020-04-01 | 2020-07-28 | 汇通达网络股份有限公司 | Software automation unit testing method based on TestNG |
-
2021
- 2021-04-23 CN CN202110439364.XA patent/CN113138922A/en active Pending
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101882107A (en) * | 2010-06-28 | 2010-11-10 | 山东中创软件商用中间件股份有限公司 | Method and device for automatically testing WEB (World Wide Web) application |
US20120017143A1 (en) * | 2010-07-15 | 2012-01-19 | Hon Hai Precision Industry Co., Ltd. | Electronic device and method for data management |
CN111459821A (en) * | 2020-04-01 | 2020-07-28 | 汇通达网络股份有限公司 | Software automation unit testing method based on TestNG |
Non-Patent Citations (1)
Title |
---|
JESSYSONG: "testNG参数化之excel读取数据", pages 1 - 6, Retrieved from the Internet <URL:CSDN博客,https://blog.csdn.net/jessysong/article/details/53141314> * |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115982055A (en) * | 2023-02-27 | 2023-04-18 | 云筑信息科技(成都)有限公司 | Test method special for java language development program |
CN115982055B (en) * | 2023-02-27 | 2023-06-23 | 云筑信息科技(成都)有限公司 | Test method special for java language development program |
CN117312146A (en) * | 2023-09-26 | 2023-12-29 | 北京五一嘉峪科技有限公司 | Automatic testing method, system, computing equipment and medium for competition platform |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN107368593B (en) | Data import method and device and server | |
US9098500B1 (en) | Revision history storage and maintenance | |
CN108121542B (en) | MyBatis-based SQL statement configuration method and system | |
CN101996131A (en) | Automatic test method and automatic test platform for graphic user interface (GUI) based on x extensive makeup language (XML) packaging key word | |
CN113138922A (en) | Method for parametrically reading Excel test data based on TestNG | |
CN111176979B (en) | Test case generation method and device of graph database | |
CN108241720B (en) | Data processing method, device and computer readable storage medium | |
Ciroth et al. | openLCA 1.4 overview and first steps | |
WO2020259516A1 (en) | Unit testing system and unit testing method | |
CN114138748A (en) | Database mapping file generation method, device, equipment and storage medium | |
CN105867886B (en) | Method and device for writing table | |
CN114048415A (en) | Form generation method and device, electronic equipment and computer readable storage medium | |
CN114116691A (en) | Data import checking method, system, device and computer readable medium | |
CN111158665B (en) | Code generation method and device, electronic equipment and storage medium | |
CN111475150A (en) | Cross-language binding method, device, equipment and storage medium | |
JP5377522B2 (en) | Efficiently correlate nominally incompatible types | |
CN115686589A (en) | Entity class file updating system and method, corresponding computer equipment and storage medium | |
US7831633B1 (en) | Methods and apparatus for implementing a custom driver for accessing a data source | |
CN113961238A (en) | Object conversion method and device, electronic equipment and storage medium | |
CN113901025A (en) | Database management method, device, equipment and storage medium | |
CN109840213B (en) | Test data creating method, device, terminal and storage medium for GUI test | |
CN112380142A (en) | Interface document management method and device and test equipment | |
CN112464636A (en) | Constraint file comparison method and device, electronic equipment and storage medium | |
CN109117422B (en) | Global unique number rapid generation method and system in large-scale high-concurrency system | |
CN116661758B (en) | Method, device, electronic equipment and medium for optimizing log framework configuration |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |