Method and device for retrieving test case based on code coverage
Embodiments of the present invention relate to a method and a device for obtaining test cases based on a code coverage. The method comprises creating a mapping between source code and test cases based on a code coverage; and obtaining, in response to a change in the source code, the test cases for the change based on the mapping. The device comprises: a creating module configured to create a mapping between source code and test cases based on a code coverage; and an obtaining module configured to, in response to a change in the source code, obtain a test case for the change based on the mapping. Through such method and device, the embodiments of the present invention can obtain test cases as few as possible to cover the changed source code.
Latest EMC IP Holding Company LLC Patents:
- Data masking in a microservice architecture
- Systems and methods for device thermal management using dynamic ranking of device cooling needs
- Multiple data labels within a backup system
- Optimizing file system defrag for deduplicated block storage
- Method, electronic device, and computer program product for managing storage device
This application claim priority from Chinese Patent Application Number CN201510622723.X, filed on Sep. 25, 2015 at the State Intellectual Property Office, China, titled “METHOD AND DEVICE FOR RETRIEVING TEST CASE BASED ON CODE COVERAGE,” the contents of which is herein incorporated by reference in its entirety.
FIELDEmbodiments of the present invention relates to a software testing field, and more specifically relates to a method for obtaining a test case based on a code coverage.
BACKGROUNDSoftware testing refers to a process in which a program is operated in a specified condition to detect program errors, to measure software quality, and to evaluate whether it satisfies design requirements. Test cases are a group of test inputs, executing conditions and expected results complied for a certain particular purpose to test a certain program path or check whether a certain particular demand is satisfied.
In the software testing field, test cases are usually used to test source code. When a developer changes the source code (for example, adds, modifies or removes line in a function, or adds or removes a function in a class), a tester often has to run all test cases to cover the changed source code. However, if the source code is not changed that much, it is unnecessary to run all the test cases. If the source code is relatively complicated and/or there is relatively a large amount of test cases, it will be time-consuming and have a low efficiency due to necessity of running all test cases.
SUMMARYA method, system, device, and computer program product for obtaining test cases based on a code coverage, the method, system, device and computer program product including creating a mapping between source code and test cases based on a code coverage and in response to a change in the source code, obtaining a test case for the change based on the mapping.
With reference to the figures, embodiments of the present invention are illustrated exemplarily, wherein:
Hereinafter, embodiments of the present invention will be described with reference to the drawings. It should be noted that similar components or functional components may be represented by the same number. The appended drawings only intend to illustrate embodiments of the present invention. Those skilled in the art may obtain alternative embodiments from the following description without departing from the spirit and the protection scope of the present invention.
Embodiments of the present invention are new technical solutions proposed to solve the above mentioned technical problem, and the embodiments of the present invention may obtain test cases as few as possible to cover the changed source code.
In one aspect of the present invention, a method for obtaining test cases based on a code coverage is provided, comprising: creating a mapping between source code and test cases based on a code coverage; and in response to a change in the source code, obtaining a test case for the change based on the mapping.
According to one embodiment of the present invention, creating a mapping between source code and test cases based on a code coverage comprises: generating a run report after running the test cases on the source code using a code coverage tool; and obtaining a mapping between the source code and the test cases based on the run report.
According to one embodiment of the present invention, generating a run report after running the test cases on the source code using a code coverage tool comprises: after running each of the test cases on the source code, generating the run report for each of the test cases using the code coverage tool.
According to one embodiment of the present invention, obtaining a mapping between the source code and the test cases based on the run report comprises: creating, based on the run report, mapping tables between the test cases and line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, creating the mapping tables comprises: performing information extraction on the run report generated after running each of the test cases to obtain a mapping table between each of the tests case and corresponding line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, obtaining a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code comprises: reading the run report to obtain the line numbers, function names, class names and file names of the source code; and storing each of the test cases and the obtained line numbers, function names, class names and file names into a mapping table of a database.
According to one embodiment of the present invention, creating mapping between source code and the test cases based on a code coverage comprises: performing test cases in a test case repository for the source code to obtain the code coverage.
According to one embodiment of the present invention, performing test cases in a test case repository for the source code to obtain the code coverage comprises: if the test cases in the test case repository fail to cover the source code, adding a new test case into the test case repository for an uncovered part of the source code.
According to one embodiment of the present invention, creating mapping between source code and the test cases based on a code coverage comprises: storing the mapping in a database modeled using a unitary modeling language (UML).
According to one embodiment of the present invention, obtaining test cases for the change based on the mapping comprises: obtaining a change set in response to a change in the source code.
According to one embodiment of the present invention, obtaining a change set in response to a change in the source code comprises: obtaining, using a comparing tool, the change set as compared to previously stored source code in response to the change in the source code.
According to one embodiment of the present invention, obtaining test cases for the change based on the mapping comprises: analyzing the change set to obtain test cases for the change.
According to one embodiment of the present invention, analyzing the change set to obtain test cases for the change comprises: obtaining a first test case mapped into a function based on the mapping in response to the change occurring in the function.
According to one embodiment of the present invention, obtaining a first test case mapped to a function based on the mapping in response to the change occurring in the function comprises: obtaining file names and line numbers based on the change set in response to the change in the source code; searching for a mapping table created between the source code and test cases using the file names and the line numbers to obtain the changed function names and class names; and searching for test case names simultaneously corresponding to the file names, the function names and the class names in the mapping table to obtain the first test case.
According to one embodiment of the present invention, analyzing the change set to obtain test cases for the change comprises: obtaining a second test case mapped into a class based on the mapping in response to the change occurring in the class.
According to one embodiment of the present invention, obtaining a second test case mapped into a class based on the mapping in response to the change occurring in the class comprises: obtaining file names and line numbers based on the change set in response to the change in the source code; searching for a mapping table created between the source code and test cases using the file names and the line numbers to obtain the changed class names; and searching for test case names simultaneously corresponding to the file names and the class names in the mapping table to obtain the second test case.
According to one embodiment of the present invention, it further comprises: running a union set between the first test case and the second test case.
In one aspect of the present invention, a device for obtaining test cases based on a code coverage is provided, comprising: a creating module configured to create a mapping between source code and test cases based on a code coverage; and an obtaining module configured to, in response to a change in the source code, obtain a test case for the change based on the mapping.
According to one embodiment of the present invention, the creating module comprises: a generating module configured to generate a run report after running the test cases on the source code using a code coverage tool; and an obtaining module configured to obtain mapping between the source code and the test cases based on the run report.
According to one embodiment of the present invention, the generating module configured to generate a run report after running the test cases on the source code using a code coverage tool comprises: a generating module configured to generate a run report for each of the test cases using the code coverage tool after running each of the test cases on the source code.
According to one embodiment of the present invention, the obtaining module configured to obtain mapping between the source code and the test cases based on the run report comprises: a creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, the creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code comprises: a performing module configured to perform information extraction on a run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, the performing module configured to perform information extraction on a run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code comprises: a reading module configured to read the run report to obtain the line numbers, function names, class names and file names of the source code; and a storing module configured to store each of the test cases and the obtained line numbers, function names, class names and file names into a mapping table of a database.
According to one embodiment of the present invention, the creating module comprises: an executing module configured to execute all test cases in a test case repository for the source code to obtain the code coverage.
According to one embodiment of the present invention, the executing module configured to execute all test cases in a test case repository for the source code to obtain the code coverage comprises: an adding module configured to, if the test cases in the test case repository fail to cover the source code, add a new test case into the test case repository for an uncovered part of the source code.
According to one embodiment of the present invention, the creating module comprises: a storing module configured to store the mapping in a database modeled using a unitary modeling language.
According to one embodiment of the present invention, the obtaining module comprises: an obtaining module configured to obtain a change set in response to a change in the source code.
According to one embodiment of the present invention, the obtaining module configured to obtain a change set in response to a change in the source code comprises: an obtaining module configured to obtain, using a comparing tool, the change set as compared to previously stored source code in response to the change in the source code.
According to one embodiment of the present invention, the obtaining module comprises: an analyzing module configured to analyze the change set to obtain test cases for the change.
According to one embodiment of the present invention, wherein the analyzing module configured to analyze the change set to obtain test cases for the change comprises: an obtaining module configured to obtain a first test case mapped into a function in response to the change occurring in the function.
According to one embodiment of the present invention, the obtaining module configured to obtain a first test case mapped into a function in response to the change occurring in the function comprises: an obtaining module configured to obtain file numbers and line numbers based on the change set in response to a change in the source code; a searching module configured to search for a mapping table created between the source code and test cases using the file numbers and the line numbers to obtain the changed function names and class names; and a searching module configured to search for test case names simultaneously corresponding to the file names, the function names and the class names in the mapping table to obtain the first test case.
According to one embodiment of the present invention, the analyzing module configured to analyze the change set to obtain test cases for the change comprises: an obtaining module configured to obtain a second test case mapped into a class based on the mapping in response to the change occurring in the class.
According to one embodiment of the present invention, the obtaining module configured to obtain a second test case mapped into a class based on the mapping in response to the change occurring in the class comprises: an obtaining module configured to obtain file numbers and line numbers based on the change set in response to a change in the source code; a searching module configured to search for a mapping table created between the source code and test cases using the file numbers and the line numbers to obtain the changed class names; and a searching module configured to search for test case names simultaneously corresponding to the file names and the class names in the mapping table to obtain the second test case.
According to one embodiment of the present invention, it further comprises: a running module configured to run a union set between the first test case and the second test case.
In one aspect of the present invention, a computer program product is provided, comprising a computer readable program instruction embodied therein, which causes a processor to execute the above mentioned method when the computer readable program instruction is being executed by the processor.
In the embodiments of the present invention, as mapping between source code and test cases may be created based on the code coverage, test cases for the change may be obtained based on the mapping in response to a change in the source code, without the need of running all test cases for entire source code. By only obtaining corresponding test cases for the change of the source code, the test cases can be obtained as few as possible to cover the changed source code.
For example, the mapping between the source code and the test cases may be created based on the code coverage, and therefore, in response to a change in the source code, test cases for the change can be obtained based on the mapping, without a need of running all the test cases for entire source code. Accordingly, by only obtaining corresponding test cases for the change of the source code, the test cases can be obtained as few as possible to cover the changed source code.
For instance, the code coverage tool may be an opencover tool or a simplecov tool, etc. For example, the run report may be a run report in an XML (an extensible markup language) format generated by using the opencover tool, or may be a run report in an HTML (a hypertext markup language) generated by using the simplegov tool.
For instance,
For instance,
According to one embodiment of the present invention, the step of generating the run report after running test cases on source code using a code coverage tool comprises: generating a run report for each of the test cases using the code coverage tool after running each of the test cases on the source code.
For example, regarding each of the test cases, each of the test cases is run on the source code, and then the run report of each of the test cases is generated using a code coverage tool to facilitate the subsequent creation of the mapping between the source code and the test case based on the run report. Therefore, after running all the test cases, mapping between all the test cases and the source code may be created.
According to one embodiment of the present invention, the step of obtaining the mapping between the source code and the test cases based on the run report comprises: creating a mapping table between the test cases and line numbers, function names, class names and file names in the source code based on the run report.
For example, suppose that
For instance, suppose that
According to one embodiment of the present invention, the step of creating, based on the run report, the mapping between the test cases and line numbers, function names, class names and file names in the source code comprises: performing information extraction on the run report generated after running each of the test cases to obtain a mapping table between each of the test cases and the corresponding line numbers, function names, class names and file names in the source code.
For instance, as shown in
According to one embodiment of the present invention, the step of performing information extraction on the run report generated after running each of the test cases, to obtain the mapping table between each of the test cases and the corresponding line numbers, function names, class names and file names in the source code comprises: reading the run report to obtain line numbers, function names, class names and file names in the source code; and storing the obtained line numbers, function names, class names and file names into the mapping table of the database.
For instance, the step of obtaining the mapping table between the Testcase1 and the source code based on the report in the XML format in
1. reading report.xml to obtain a root node
XElement root=XElement.Parse (“report.xml”);
2. obtaining corresponding relation between the file name and uid using the root node, storing it in an object htFileObj (to be used later)
for example, regarding the following report portion in
the corresponding relation between the file name and uid may be obtained using the following pseudocode:
3. obtaining a name of the class (i.e., class name) using the root node, which will be used in the later process:
for example, regarding the following report portion in
<Class>
<FullName>Class1</FullName>
the following pseudocode may be used to obtain the class name (e.g., Class1):
className=root.Element(“Class”).Element(“FullName”).value
4. traversing the function in the class to obtain the function name, the line number, fileid, etc., and storing the test case name (e.g., testcase 1), the line number, the function name, the class name and the file name into the database
for instance, regarding the function (method) in
the mapping (e.g., a mapping table) between the test case 1 and the source code created through the above process, for example, may be presented in a form of Table 1:
In Table 1, Testcase1 is vacant due to not covering line numbers 32, 33, 34. As seen from the report in the foregoing XML format, code with line number 26-28 is covered and code with number lines 32-34 is not covered by Testcase1 when running the Testcase1, wherein the code with line numbers 26-28 corresponding to the function name Function1, the class name Class1, and the file name File2. Furthermore, though Testcase1 does not cover line numbers 32, 33, 34, it can be seen from the report in the above mentioned XML format that the source code with line numbers 32-34 corresponds to the function name Function2, the class name Class1 and the file number File2, and at this time, such corresponding relation may be stored in the mapping table. When each of the test cases is run for the source code (for instance, M test cases are run, and the case names are Testcase1, Testcase2 . . . TestcaseM), the mapping (for instance, a mapping table corresponding to the line number s1, the function name FunctionP, the class name ClassN and the file number FileQ) can be created in each of the test cases, and at this time when running all the test cases is completed, corresponding mapping (e.g., a mapping table) can be created between all test cases and the source code.
According to one embodiment of the present invention, creating mapping between the source code and the test cases based on the code coverage comprises: for the source code, executing all test cases in the test case repository to obtain the code coverage.
For instance, at step 703 in
For instance, step 702 may be designed prior to step 703 in
For instance, step 707 may be designed prior to step 702, wherein a scheduler is initialized at step 701, so as to facilitate scheduling, e.g., thread execution of the running process of the test cases.
According to one embodiment of the present invention, the step of executing, for the source code, all test cases in a test case repository to obtain the code coverage comprises: adding new test cases for uncovered source code into the test case repository when all test cases do not cover entire source code.
For instance, at step 704 of
According to one embodiment of the present invention, the step of creating mapping between the source code and the test cases based on the code coverage comprises: storing the mapping into a database modeled using a unitary modeling language. The database is only used exemplarily but not restrictively.
For instance,
For instance, the step of storing the mapping in the database modeled using a unitary modeling language comprises: based on a code coverage result, updating the mapping between the test cases and the source code into the database storing the mapping. For example, at step 706 of
Continuing to refer to
According to one embodiment of the present invention, the step of obtaining the test cases for the change based on the mapping comprises: obtaining a change set in response to a change in the source code (step 901 shown in
For instance, at step 1003 of
For instance, step 1002 is designed prior to step 1003 of
For instance, step 1001 is designed prior to step 1002 of
According to one embodiment of the present invention, the step of obtaining a change set in response to a change in the source code comprises: obtaining, using a comparing tool, a change set as compared to the previously stored source code in response to a change in the source code.
For instance, a comparing tool (such as a DiffMerge tool or a Beyond Compare tool) is used for comparison to obtain a change set between the current version of the source code and the previous version of the source code stored in the database, and such change set is exported to, for example, a text file or an XML file, and then the text file or the XML file is analyzed to facilitate the subsequent obtaining of the corresponding test case to be run.
According to one embodiment of the present invention, the step of obtaining test cases for the change based on the mapping comprises: analyzing the change set to obtain test cases for the change (step 902 as shown in
For example, at step 1004 of
According to one embodiment of the present invention, the step of analyzing the change set to obtain test cases for the change comprises: obtaining a first test case mapped to a function based on the mapping, in response to the change occurring in the function.
For instance, a change in the function comprises at least one of the following in the function: adding lines, modifying lines and deleting lines. For example, at step 1005 of
According to one embodiment of the present invention, the step of obtaining a first test case mapped to a function based on the mapping in response to the change occurring in the function comprises: obtaining changed file names and line numbers of the source code based on the change set; searching for a mapping table created between the source code and test cases using the file names and the line numbers to obtain changed function names and class names; searching for case names simultaneously corresponding to the file names, the function names and the class names in the mapping table to obtain the first test case.
First Example of Obtaining the First Test CaseAssuming that the previous source code (for example, stored in File1) is in a form of
If source code of class1 in
1. obtaining a change set using a comparing tool (e.g., a DiffMerge tool) (as shown in
2. by analysis of the text file, obtaining that a change occurs in latter code of line 5 (i.e., LineNumber=5) of File1 (i.e., FileName=File1), i.e., a line of code i++ is added;
3. searching for a corresponding record (which can be obtained in this example) in the mapping presented in Table 2 using LineNumber=5, FileName=File1, thereby obtaining that the changed code has the function name Function1 and the class name class1; and
4. obtaining, in Table 2, test case names simultaneously corresponding to the file name File1, the function name Function1 and the class name class1, wherein it is obtained in the example through Table 2 that test cases corresponding to the code change are Testcase1 and Testcase2 (i.e., the first test case).
Testcase1 and Testcase2 herein are the first test case, and the corresponding test for the change of the source code (in the example, the function is changed) is performed by running the two test cases, instead of running all the test cases (e.g., Testcase1, Testcase2, Testcase3 and Testcase4 in Table 2), thereby improving the test efficiency, i.e., test cases may be obtained as few as possible to cover the changed source code.
Second Example of Obtaining the First Test CaseLike in the first example, assume that the previous source code (for example, stored in File1) is in a form of
1. obtaining a change set using a comparing tool (e.g., a Beyond Compare tool), and exporting the change set to, for example, the XML file, as shown in
2. by analysis of the document file, obtaining that: “puts abc” is added in line 4 of File1 (i.e., FileName=File1) in the current source code (as shown in the block of
3. searching for a corresponding record in a created mapping table using LineNumber=4, FileName=File1, thereby obtaining a function name (Hello World) and a class name (class1) of the changed code; and
4. obtaining test cases simultaneously corresponding to the file name File1, the function name Hello World and the class name class1 in the created mapping table, i.e., the first test case.
According to one embodiment of the present invention, the step of analyzing the change set to obtain test cases for the change comprises: obtaining a second test case mapped into a class based on the mapping in response to the change occurring in the class.
For instance, the change in the class comprises adding and/or removing the function in the class. For example, judging whether the change of the source code occurs in a class may be performed at step 1007 of
According to one embodiment of the present disclosure, the step of obtaining a second test case mapped to a class based on the mapping in response to the change occurring in the class comprises: obtaining changed file names and line numbers of the source code based on the change set; searching for a mapping table created between the source code and test cases using the file names and the line numbers to obtain changed class names; and searching for case names simultaneously corresponding to the file names and the class names in the mapping table to obtain the second test case.
First Example of Obtaining the Second Test CaseContinuing the above mentioned example of the source code of
1. obtaining a change set (as shown in
2. by analysis of the text file, obtaining that: in the current source code, a function with a function name Function3 is added to the latter part in line 8 (i.e., LineNumber=8) of File1 (i.e., FileName=File1) (but the functions Function1 and Function2 are not changed as a matter of fact);
3. searching for a corresponding record in the mapping in Table 2 as shown above using LineNumber=8, FileName=File1 to obtain the changed class name of the code (not found in the example); as it is not found in the example, obtaining the class name, for example, through the following manner that the line 8 of File1 (FileName=File1) starts from line 4 and ends at line 13 in class1 of File1 (i.e., as can be obtained through searching in Table 2), and obtaining that the changed class name of the code is class1; and
4. obtaining, in Table 2, case names simultaneously corresponding to the file name File1 and the class name class1, wherein the test cases corresponding to the code change are Testcase1, Testcase2 and Testcase3 (i.e., the second test case), which can be obtained through Table 2 in the example.
If the obtained second test case is run herein, the corresponding test may be performed for the change of the source code (in the example, the class is changed), without the need of running all the test cases (e.g., Testcase1, Testcase2, Testcase3 and Testcase4 in Table 2), thereby improving the test efficiency, i.e., the test cases may be obtained as few as possible to cover the changed source code.
Second Example of Obtaining the Second Test CaseLike in the first example, assume that the previous source code (for example, stored in File1) is in a form of
1. obtaining a change set using a comparing tool (e.g. a Beyond Compare tool) and exporting the change set to, for example, the XML file, as shown in
2. by analysis of the XML file, obtaining that: in the current source code, a function “Hello World2” is added to lines 6-9 of File1 (i.e., FileName=File1) (as shown in a block of
3. searching for a corresponding record in the created mapping table using LineNumber=6, FileName=File1, thereby obtaining a changed class name (class1) of the code; and
4. obtaining case names simultaneously corresponding to the file name File1 and the class name class1 in the created mapping table, i.e., a second test case.
According to one embodiment of the present invention, it further comprises: running a union set between the first test case and the second test case.
For instance, at step 1009 of
As shown in
According to one embodiment of the present invention, creating module 2301 comprises: a generating module configured to generate a run report generated after running the test cases on the source code using a code coverage tool; and an obtaining module configured to obtain mapping between the source code and the test cases based on the run report.
According to one embodiment of the present invention, the generating module configured to generate a run report after running the test cases on the source code using a code coverage tool comprises: a generating module configured to generate a run report for each of the test cases using the code coverage tool after running each of the test cases on the source code.
According to one embodiment, the obtaining module configured to obtain mapping between the source code and the test cases based on the run report comprises: a creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, the creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code comprises: a performing module configured to perform information extraction on a run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code.
According to one embodiment of the present invention, the performing module configured to perform information extraction on the run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code comprises: a reading module configured to read the run report to obtain the line numbers, function names, class names and file names of the source code; and a storing configured to store each of the test cases and the obtained line numbers, function names, class names and file names into a mapping table of a database.
According to one embodiment of the present invention, the creating module 2301 comprises: an executing module configured to execute for the source code all test cases in a test case repository to obtain the code coverage.
According to one embodiment of the present invention, the executing module configured to execute for the source code all test cases in a test case repository to obtain the code coverage comprises: an adding module configured to, if the test cases in the test case repository fail to cover the source code, add a new test case into the test case repository for an uncovered part of the source code.
According to one embodiment of the present invention, the creating module 2301 comprises: a storing module configured to store the mapping in a database modeled using a unitary modeling language (UML).
According to one embodiment of the present invention, the obtaining module 2302 comprises: an obtaining module configured to obtain a change set in response to a change in the source code.
According to one embodiment of the present invention, the obtaining module configured to obtain a change set in response to a change in the source code comprises: an obtaining module configured to obtain, using a comparing tool, the change set as compared to previously stored source code in response to the change in the source code.
According to one embodiment of the present invention, the obtaining module 2302 comprises: an analyzing module configured to analyze a change set to obtain test cases for the change.
According to one embodiment of the present invention, the analyzing module configured to analyze a change set to obtain test cases for the change comprises: an obtaining module configured to obtain based on the mapping a first test case mapped to a function in response to the change occurring in the function.
According to one embodiment of the present invention, the obtaining module configured to obtain based on the mapping a first test case mapped into a function in response to the change occurring in the function comprises: an obtaining module configured to obtain changed filed names and line number of the source code based on the change set; a searching module configured to search for a mapping table created between the source code and test cases using the file names and line numbers to obtain the changed function names and class names; and a searching module configured to search for test case names simultaneously corresponding to the file names, the function names and the class names in the mapping table to obtain the first test case.
According to one embodiment of the present invention, the analyzing module configured to analyze the change set to obtain test cases for the change comprises: an obtaining module configured to obtain a second test case mapped into a class based on the mapping in response to the change occurring in the class.
According to one embodiment of the present invention, the obtaining module configured to obtain based on the mapping a second test case mapped into a class in response to the change occurring in a class comprises: an obtaining module configured to obtain file names and line numbers of the source code based on the change set in response to a change in the source code; a searching module configured to search for a mapping table created between the source code and test cases using the file numbers and line numbers to obtain the changed class names; and a searching module configured to search for test case names simultaneously corresponding to the file names and the class names in the mapping table to obtain the second test case.
According to one embodiment of the present invention, it further comprises: a running module configured to run a union set between the first test case and the second test case.
It should be appreciated that the above mentioned device and means of the present invention may be realized through hardware, software or combination of software and hardware. Wherein, a hardware portion may be realized using a dedicated logic; a software portion may be stored in a memory, and a system is executed through proper instructions, for example, executed by a microprocessor or dedicated hardware. Those skilled in the art may understand that the foregoing method and system may be realized using a computer executable instruction and/or being included in processor control code. For example, such code is provided on a carrier medium like a disk, CD or DVD-ROM, a programmable memory like a read-only memory (firmware) or a data carrier like an optical or electrical signal carrier. The device and means of the embodiments of the present invention may be realized by VLSI or a gate array, a semiconductor like a logic chip, a transistor, etc., or a hardware circuit of a programmable hardware device like field programmable gate array, a programmable logic device, etc., or by for example software executed by processors of various types, or by combination of the hardware circuit and software (e.g., firmware).
It should be noted that, though the preceding detailed description includes several means or sub-means of the device, the division is only used exemplarily but not restrictively. As a matter of fact, according to embodiments of the present disclosure, features and functions of two or more means as described above may be embodied in one means. On the contrary, features and function of one means as described above may be further embodied by a plurality of means.
The present invention further provides a computer program product, comprising a computer readable program instruction embodied therein, which causes a processor to perform the above method when the computer readable program instruction is being executed by the processor.
Under the teaching given in the description and relevant figures, modifications and other embodiments of the present invention will be appreciated by those skilled in the relevant field. Hence, it should be understood that the embodiments of the present invention are not limited to the disclosed specific embodiments, and modifications and other embodiments intend to be covered in the range of the present invention. Moreover, though the above description and relevant figures describe exemplary embodiments in a background of certain exemplary combinations of components and/or functions, it should be realized that alternative embodiments can provide different combinations of components and/or functions without departing from the scope of the present disclosure. Regarding this, for example, other combination of components and/or functions being somewhat different from the above explicit description also falls into the scope of the present invention. Although particular terms are used herein, they are used only in their general and descriptive senses, without a purpose of limiting.
Claims
1. A method for testing a software program, comprising:
- creating a mapping table expressing a mapping between source code of the software program and test cases based on code coverage, wherein the source code is stored within a source code repository on a data storage system and the source code includes classes and functions, each class including one or more of the functions, the mapping table including a plurality of mapping entries each associating a respective test with a corresponding function and a corresponding class that includes the function;
- in response to a change in the source code, obtaining a test case for the change based on the mapping in the mapping table, wherein the test case is selected from a plurality of test cases available to test the source code by (1) in a first case in which a function has changed, obtaining a first test case based on a first association of the first test case with the function in the mapping table, and (2) in a second case in which a class including the function has changed, obtaining a second test case based on a second association of the second test case with the class in the mapping table; and
- executing the software program according to the obtained test case to test the change in the source code,
- wherein the function is a first function and the first association associates the first test case with both the first function and the class, and wherein the mapping table includes one or more additional entries making a second association between the second test case, the class, and a second function of the class, such that selection of the second test case includes coverage of the class but not of the first function of the class.
2. The method according to claim 1, wherein the creating a mapping between source code and test cases based on a code coverage comprises:
- generating a run report after running the test cases on the source code using a code coverage tool; and
- obtaining a mapping between the source code and the test cases based on the run report.
3. The method according to claim 2, wherein the generating a run report after running the test cases on the source code using a code coverage tool comprises:
- after running each of the test cases on the source code, generating the run report for each of the test cases using the code coverage tool.
4. The method according to claim 2, wherein the obtaining a mapping between the source code and the test cases based on the run report comprises:
- creating, based on the run report, mapping tables between the test cases and line numbers, function names, class names and file names in the source code.
5. The method according to claim 4, wherein the creating the mapping tables comprises:
- performing information extraction on the run report generated after running each of the test cases to obtain a mapping table between each of the tests case and corresponding line numbers, function names, class names and file names in the source code.
6. The method according to claim 5, wherein the obtaining a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code comprises:
- reading the run report to obtain the line numbers, function names, class names and file names of the source code; and
- storing each of the test cases and the obtained line numbers, function names, class names and file names into a mapping table of a database.
7. The method according to claim 1, wherein the creating mapping between source code and the test cases based on a code coverage comprises:
- performing test cases in a test case repository for the source code to obtain the code coverage.
8. A computer-implemented device, comprising:
- memory storing instructions; and
- a processor coupled to the memory and operative to execute the instructions to form functional modules collectively operative to test a software program, the modules including:
- a creating module configured to create a mapping table expressing a mapping between source code of the software program and test cases based on code coverage, wherein the source code is stored within a source code repository on a data storage system and the source code includes classes and functions, each class including one or more of the functions, the mapping table including a plurality of mapping entries each associating a respective test with a corresponding function and a corresponding class that includes the function;
- an obtaining module configured to, in response to a change in the source code, obtain a test case for the change based on the mapping in the mapping table, wherein the test case is selected from a plurality of test cases available to test the source code, by (1) in a first case in which a function has changed, obtaining a first test case based on a first association of the first test case with the function in the mapping table, and (2) in a second case in which a class including the function has changed, obtaining a second test case based on a second association of the second test case with the class in the mapping table;
- wherein the device is enabled to execute the software program according to the obtained test case to test the change in the source code,
- and wherein the function is a first function and the first association associates the first test case with both the first function and the class, and wherein the mapping table includes one or more additional entries making a second association between the second test case, the class, and a second function of the class, such that selection of the second test case includes coverage of the class but not of the first function of the class.
9. The device according to claim 8, wherein the creating module comprises:
- a generating module configured to generate a run report after running the test cases on the source code using a code coverage tool; and
- an obtaining module configured to obtain a mapping between the source code and the test cases based on the run report.
10. The device according to claim 9, wherein the generating module configured to generate a run report after running the test cases on the source code using a code coverage tool comprises:
- a generating module configured to generate a run report for each of the test cases using the code coverage tool after running each of the test cases on the source code.
11. The device according to claim 9, wherein the obtaining module configured to obtain mapping between the source code and the test cases based on the run report comprises:
- a creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code.
12. The device according to claim 11, wherein the creating module configured to create based on the run report a mapping table between the test cases and line numbers, function names, class names and file names in the source code comprises:
- a performing module configured to perform information extraction on a run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code.
13. The device according to claim 12, wherein the performing module configured to perform information extraction on a run report generated after running each of the test cases to obtain a mapping table between each of the test cases and corresponding line numbers, function names, class names and file names in the source code comprises:
- a reading module configured to read the run report to obtain the line numbers, function names, class names and file names of the source code; and
- a storing module configured to store each of the test cases and the obtained line numbers, function names, class names and file names into a mapping table of a database.
14. The device according to claim 8, wherein the creating module comprises:
- an executing module configured to execute all test cases in a test case repository for the source code to obtain the code coverage.
15. The device according to claim 14, wherein the executing module configured to execute all test cases in a test case repository for the source code to obtain the code coverage comprises:
- an adding module configured to add a new test case into the test case repository for an uncovered part of the source code, if the test cases in the test case repository fail to cover the source code.
16. The device according to claim 8, wherein the creating module comprises:
- a storing module configured to store the mapping in a database modeled using a unitary modeling language.
17. A computer program product, comprising:
- a non-transitory computer readable medium encoded with computer-executable code for testing a software program, wherein the code is configured to enable the execution of:
- creating a mapping table expressing a mapping between source code of the software program and test cases based on code coverage, wherein the source code is stored within a source code repository on a data storage system and the source code includes classes and functions, each class including one or more of the functions, the mapping table including a plurality of mapping entries each associating a respective test with a corresponding function and a corresponding class that includes the function;
- in response to a change in the source code, obtaining a test case for the change based on the mapping in the mapping table, wherein the test case is selected from a plurality of test cases available to test the source code, by (1) in a first case in which a function has changed, obtaining a first test case based on a first association of the first test case with the function in the mapping table, and (2) in a second case in which a class including the function has changed, obtaining a second test case based on a second association of the second test case with the class in the mapping table; and
- executing the software program according to the obtained test case to test the change in the source code,
- wherein the function is a first function and the first association associates the first test case with both the first function and the class, and wherein the mapping table includes one or more additional entries making a second association between the second test case, the class, and a second function of the class, such that selection of the second test case includes coverage of the class but not of the first function of the class.
18. The method according to claim 17, wherein the creating a mapping between source code and test cases based on a code coverage comprises:
- generating a run report after running the test cases on the source code using a code coverage tool; and
- obtaining a mapping between the source code and the test cases based on the run report.
19. The method according to claim 18, wherein the generating a run report after running the test cases on the source code using a code coverage tool comprises:
- after running each of the test cases on the source code, generating the run report for each of the test cases using the code coverage tool.
20. The method according to claim 18, wherein the obtaining a mapping between the source code and the test cases based on the run report comprises:
- creating, based on the run report, mapping tables between the test cases and line numbers, function names, class names and file names in the source code.
9037595 | May 19, 2015 | Dangeville |
20130111267 | May 2, 2013 | Beryoza |
20160085663 | March 24, 2016 | Best |
20160217061 | July 28, 2016 | Fulton |
20170046245 | February 16, 2017 | Liu |
103034582 | April 2013 | CN |
103176895 | June 2013 | CN |
104657256 | May 2015 | CN |
Type: Grant
Filed: Sep 21, 2016
Date of Patent: Aug 6, 2019
Patent Publication Number: 20170132119
Assignee: EMC IP Holding Company LLC (Hopkinton, MA)
Inventors: Bin Xu (Shanghai), Yue Li (Shanghai), Neil Yongjing Wang (Shanghai), Sam Lei Sun (Shanghai), Edward Zhibin Xu (Shanghai)
Primary Examiner: Wei Y Zhen
Assistant Examiner: Amir Soltanzadeh
Application Number: 15/271,347
International Classification: G06F 11/36 (20060101);