WO2023217118A1 - Code test method and apparatus, and test case generation method and apparatus - Google Patents

Code test method and apparatus, and test case generation method and apparatus Download PDF

Info

Publication number
WO2023217118A1
WO2023217118A1 PCT/CN2023/092939 CN2023092939W WO2023217118A1 WO 2023217118 A1 WO2023217118 A1 WO 2023217118A1 CN 2023092939 W CN2023092939 W CN 2023092939W WO 2023217118 A1 WO2023217118 A1 WO 2023217118A1
Authority
WO
WIPO (PCT)
Prior art keywords
code
tested
test
return value
test case
Prior art date
Application number
PCT/CN2023/092939
Other languages
French (fr)
Chinese (zh)
Inventor
张剑飞
周海莲
周智泉
赵红兵
Original Assignee
支付宝(杭州)信息技术有限公司
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by 支付宝(杭州)信息技术有限公司 filed Critical 支付宝(杭州)信息技术有限公司
Publication of WO2023217118A1 publication Critical patent/WO2023217118A1/en

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3692Test management for test results analysis

Definitions

  • This description relates to the field of computer technology, and in particular, to a method and device for code testing and test case generation.
  • This specification provides a method and device for code testing and test case generation to partially solve the problem in related technologies of low accuracy of test results caused by returning error type return values.
  • This manual adopts the following technical solution:
  • This manual provides a code testing method, including: obtaining the code to be tested; according to the type of parameters required for the method called by the code to be tested, and/or testing the code to be tested.
  • the analysis results obtained after analyzing the bytecode corresponding to the code determine the actual return value type corresponding to the method called when testing the code to be tested.
  • the method types of the methods called when testing the code to be tested include: At least one of a generic method and forced type conversion; according to the actual return value type, the test result for the code to be tested is obtained.
  • the code to be tested is method called when The actual return value type corresponding to the method specifically includes: obtaining the code of the method called by the code to be tested; and determining the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested.
  • the actual return value of the method called, and the association relationship between the parameter type required by the method called by the code to be tested; according to the association relationship, and the type of parameters required by the method called by the code under test Determine the actual return value type corresponding to the method called when testing the code to be tested.
  • the test result for the code to be tested which specifically includes: creating a method data structure MethodDescripter structure according to the association relationship, the MethodDescripter structure includes: the actual return of the calling method Value type, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, and the incoming parameters of the calling method; generate the code to be tested based on the MethodDescripter structure and the actual return value type
  • the mock method corresponding to the called method; through the mock method, the test results for the code to be tested are obtained.
  • the code to be tested is The actual return value type corresponding to the method called, specifically includes: by analyzing the bytecode corresponding to the code to be tested, determining that the method type from the method called by the code to be tested is forced type conversion The method is used as the target method; when testing the code to be tested, the actual return value type corresponding to the method called by the code to be tested is determined according to the target method.
  • the method further includes: converting the The corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type after forced type conversion of the return value of the target method is saved; when testing the code to be tested, According to the target method, determine the actual return value type corresponding to the method called by the code to be tested, specifically including: if during the testing process of the code to be tested, determine the method called by the code to be tested The code line number of the forced type conversion method is used as the target line number; according to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved correspondence relationship.
  • obtaining the test result for the code to be tested specifically includes: querying the actual return value corresponding to the method called by the code to be tested from the pre-saved correspondence relationship Type, generate a mock method corresponding to the method called by the code to be tested; use the mock method to obtain test results for the code to be tested.
  • This manual provides a method for generating test cases, including: obtaining each test case; for each test Use case, through the above code testing method, test the set code to obtain the test results corresponding to the test case; according to the test results corresponding to each test case, determine whether the preset test conditions are met; if it is determined that the preset test conditions are not met, test conditions, regenerate each test case, and test the set code according to each regenerated test case until the test condition is met, and obtain the test case generated when the test condition is met.
  • regenerating each test case specifically includes: selecting at least some test cases from each test case and combining them in pairs to obtain each test case combination; for each test case combination, combining the two test cases included in the test case combination. Parameters of the same type in each test case are weighted to obtain a regenerated test case.
  • regenerating each test case specifically includes: selecting at least some test cases from each test case; for each selected test case, numerically adjusting at least some parameters included in the test case, and/or Adjust the code to be tested contained in the test case to obtain a regenerated test case.
  • This specification provides a code testing device, including: an acquisition module, used to obtain the code to be tested; a determination module, used to determine the type of parameters required by the method called by the code to be tested, and/or to determine the The analysis results obtained after analyzing the bytecode corresponding to the code to be tested determine the actual return value type corresponding to the method called when the code to be tested is tested, and the method type of the method called when the code to be tested is tested. It includes: at least one of a generic method and forced type conversion; a test module, used to obtain test results for the code to be tested according to the actual return value type.
  • the determination module is specifically configured to obtain the code of the method called by the code to be tested; and determine the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested.
  • the relationship between the actual return value of the method called during testing and the type of parameters required by the method called by the code to be tested; according to the relationship, and the type of parameters required by the method called by the code under test determine the actual return value type corresponding to the method called when testing the code to be tested.
  • the test module is specifically configured to create a method data structure MethodDescripter structure according to the association relationship.
  • the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, and the method of calling the method. name, the class name of the calling method, and the incoming parameters of the calling method; according to the MethodDescripter structure and the actual return value type, a mock method corresponding to the method called by the code to be tested is generated; through the mock method , to obtain the test results for the code to be tested.
  • the determination module is specifically configured to determine, from the methods called by the code to be tested, that the method type is a forced type conversion method by analyzing the bytecode corresponding to the code to be tested, as target Method; when testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested according to the target method.
  • the device further includes: an analysis module, configured to compare the code line number corresponding to the target method in the code to be tested with the actual return value of the return value of the target method after forced type conversion.
  • the corresponding relationship of the types is saved; the determination module is specifically used to determine, during the testing process of the code to be tested, that the method called by the code to be tested is the code line number of the forced type conversion method, as the target Line number; according to the target line number, query the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence.
  • the test module is specifically configured to generate the actual return value type corresponding to the method called by the code to be tested based on the query from the pre-saved correspondence relationship, and generate the return value type corresponding to the method called by the code to be tested. Mock method; through the mock method, test results for the code to be tested are obtained.
  • This manual provides a device for generating test cases, including: a use case acquisition module, used to obtain each test case; a use case testing module, used to test the set code for each test case through the above code testing method , obtain the test results corresponding to the test case; the judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case; the generation module is used to re-run the test when it is determined that the test conditions are not met.
  • a use case acquisition module used to obtain each test case
  • a use case testing module used to test the set code for each test case through the above code testing method , obtain the test results corresponding to the test case
  • the judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case
  • the generation module is used to re-run the test when it is determined that the test conditions are not met.
  • Each test case is generated, and the set code is tested according to each regenerated test case until the test condition is met, and the test case
  • the generation module is specifically configured to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, combine the two test case combinations included in the test case combination. Parameters of the same type in the test case are weighted to obtain a regenerated test case.
  • the generation module is specifically configured to select at least some test cases from each test case; for each selected test case, numerically adjust at least some parameters contained in the test case, and/or The code to be tested contained in the test case is adjusted to obtain a regenerated test case.
  • the storage medium stores a computer program.
  • the computer program is executed by a processor, the above code testing and test case generation methods are implemented.
  • This specification provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor.
  • the processor executes the program, the above code testing and test case generation methods are implemented.
  • the first method of code testing is to obtain the code to be tested, and then determine the code to be tested based on the type of parameters required by the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested.
  • the method type of the method called when testing the code to be tested includes at least one of calling a generic method and forced type conversion, and based on the actual return value type, the specific return value type is obtained. Test results of the code under test.
  • the actual return value type of the generic method called during the running process of the code to be tested and the method where forced type conversion occurs can be accurately determined, so that the actual return value type can be determined based on The determined actual return value type is used to test the code to be tested, thereby improving the accuracy of the test results.
  • Figure 1 is a schematic flow chart of a code testing method provided in this manual
  • Figure 2 is a schematic diagram of the method for determining the actual return value type of the generic method provided in this specification
  • Figure 3 is a schematic flow chart of a test case generation method provided in this manual
  • Figure 4 is a schematic diagram of a code testing device provided in this specification.
  • Figure 5 is a schematic diagram of a device for generating test cases provided in this specification.
  • FIG. 6 is a schematic diagram of an electronic device corresponding to FIG. 1 provided in this specification.
  • Figure 1 is a schematic flow chart of a code testing method provided in this manual, including the following steps S101 Go to S103.
  • the tester can test the code of the software to be tested based on the testing requirements. Specifically, the tester can first determine a part of the code from the code of the software to be tested as the code to be tested, and then the code to be tested can be Conduct tests to obtain test results for the code to be tested.
  • the code to be tested may refer to a collection of codes that are part of the code of the software to be tested.
  • the code to be tested may be one or more classes to be tested (a class here refers to a A custom reference data type, the class encapsulates the code corresponding to several attributes and methods).
  • the code to be tested can also be one or more methods (i.e., a collection of code statements) contained in the code of the software to be tested. , these code statements together perform a function (the method is contained in the class or object of the class) corresponding code.
  • the execution subject of the method for implementing code testing may refer to a designated device such as a terminal device installed on the business platform, or may refer to a terminal device such as a desktop computer, laptop computer, etc.
  • a terminal device such as a desktop computer, laptop computer, etc.
  • the terminal device can first obtain the code to be tested, and then test the code to be tested to obtain the test results of the code to be tested.
  • S102 Based on the type of parameters required for the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, determine the parameters used when the code to be tested is tested.
  • the method type of the method called when testing the code to be tested includes at least one of a generic method and forced type conversion.
  • the terminal device can intercept the calling instructions of the method called by the code under test during running, and determine the method type of the method called by the code under test during running based on the intercepted calling instructions.
  • the method type of the method called by the code under test during the running process can be a generic method (that is, a method whose incoming parameters are of uncertain type) and a forced type conversion (that is, a method where a forced type conversion occurs during the calling process).
  • a generic method that is, a method whose incoming parameters are of uncertain type
  • a forced type conversion that is, a method where a forced type conversion occurs during the calling process.
  • interception in the above content is not interception in the literal sense, but monitoring the calling instructions of the method called by the code under test during the running process, through the method of dynamic proxy. A method similar to the interception effect.
  • this manual mainly focuses on determining the actual return value types of the above two method types, that is, the generic method and the forced type conversion. Therefore, the following will focus on how to determine the two method types.
  • the actual return value type of the method is explained in detail.
  • the terminal device can obtain the code of the generic method called in the code to be tested after intercepting the calling instruction of the generic method in the code to be tested.
  • the code of the generic method called determines the relationship between the actual return value type of the method called when the code to be tested is tested, and the required parameter type of the method called by the code to be tested, and then based on the determined relationship, Determine the actual return value type corresponding to the generic method called when testing the code to be tested, as shown in Figure 2.
  • Figure 2 is a schematic diagram of a method for determining the actual return value type of a generic method provided in this specification.
  • the terminal device can monitor the calling instructions of the method under test when the code under test is running, and then can obtain the calling instructions in the code under test before the called method is executed.
  • the code of the generic method called so as to conduct a generic analysis of the code of the generic method called by the code to be tested, and determine the various actual return value types of the generic method called when the code to be tested is tested,
  • the association between the various parameter types required by the generic method called by the code under test that is, the parameter types that need to be passed in when the code under test calls the method).
  • the terminal device can create a MethodDescripter structure for each method data structure according to the determined association relationship (for each incoming parameter type of the generic method, the incoming parameter type corresponds to the actual The combination of return value types corresponds to a MethodDescripter structure), and then based on the intercepted calling instructions, the actual parameters passed in when the code under test calls the generic method can be determined, and based on the parameters passed in, the query can be performed in each MethodDescripter structure. The MethodDescripter structure that matches the passed-in parameter can then determine the actual return value type of the generic method called when testing the code to be tested.
  • the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, the incoming parameters of the calling method, and the unique identification ID of the MethodDescripter structure.
  • the terminal device can generate a mock method corresponding to the method called by the code to be tested based on the MethodDescripter structure that matches the incoming parameter, and then can simulate and generate the return value of the method called by the code to be tested through the mock method. To get the test results for the code to be tested.
  • the terminal device can determine that the method type from the method called by the code to be tested is mandatory by analyzing the bytecode corresponding to the code to be tested in advance.
  • the method of type conversion is used as the target method, and the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type after forced type conversion of the return value of the target method is saved (taking into account that it is saved to The data in the hash table is easy to read, and the corresponding relationship here can be saved in the hash table).
  • the terminal device can determine the actual return value type based on the above correspondence during the code testing process.
  • the method information here includes: the code of the calling method, and the context information of the calling method, where,
  • the context information of the calling method includes: the variable object defined by the calling method, the scope chain of the calling method (i.e., the sequence linked list for finding variables in the calling method), the caller's calling code (i.e., the calling code of the generic method or The code of the method where the cast occurs), etc.
  • the terminal device can determine the code line number of the method called by the code to be tested where the forced type conversion occurs based on the obtained method information of the calling method, as the target line number, and then based on the target line number, from the pre-saved code where the forced type conversion occurs
  • the type conversion method can query the corresponding code line number in the code to be tested and the actual return value type after the forced type conversion of the return value of the method where the forced type conversion occurs.
  • the actual return value type corresponding to the type conversion method.
  • the terminal device can arbitrarily select a data type from the preset type pool as a candidate return value type, and then use the candidate return value type as the code to be tested when testing.
  • the actual return value type of the method where the forced type conversion occurs, and the test results based on the candidate return value type are obtained. Then, based on the test results, it can be judged whether the candidate return value type is the forced type called when testing the code to be tested.
  • the actual return value type of the converted method if not, select a return value type from the type pool again as a candidate return value type until it is determined that the candidate return value type is the one called when testing the code to be tested. to the actual return value type of the method.
  • the data types in the type pool in the above content may be predetermined by the terminal device before the code to be tested is run. Specifically, the terminal device can analyze the bytecode corresponding to the code to be tested and determine all the data types contained in the code to be tested. After that, the determined data types contained in the code to be tested can be saved to a predetermined data type. Set the type in the pool.
  • the terminal device can accurately determine the actual return value type of the generic method called during the running of the code under test and the method where forced type conversion occurs, so that it can Based on the determined actual return value type, the code to be tested is tested, thereby improving the accuracy of the test results.
  • the above code testing method can not only be used to test the code to be tested, but can also be used to generate test cases, that is, through the above code testing method, to build a test case that can achieve a variety of test goals (the goals here can be Refers to some preferred test cases (when the coverage of various types of code to be tested reaches a preset threshold).
  • the method of generating test cases is described in detail below, as shown in Figure 3.
  • FIG. 3 is a schematic flowchart of a test case generation method provided in this specification, including the following steps S301 to S304.
  • the terminal device can obtain the code association information of the code to be tested before testing the code to be tested, and then generate each test case based on the code to be tested and the association information of the code to be tested, where the code association information It can be, for example, document information of the code to be tested, context information of the code to be tested, etc.
  • the terminal device can run the test case through the above code testing method to test the setting code contained in the test case and obtain the test results corresponding to the test case, where the setting code It can refer to the code to be tested, or it can refer to any piece of code (for example, the code that has been tested, or it can be used as the setting code, and then by using the method of testing the setting code, some optimal test cases are finally obtained.
  • the test results here refer to the testing effect of the code to be tested, such as: line coverage of the code to be tested, method coverage of the code to be tested, exception coverage of the code to be tested, etc.
  • the initial test cases generated by the terminal device usually cannot achieve the multiple preset test goals well. Therefore, iteration based on these initial test cases is also needed to optimize each initial test case and finally achieve the target to be tested.
  • the test goals of the code among which, the specific process of iterating on each test case is as follows: steps to achieve.
  • S303 Determine whether the preset test conditions are met based on the test results corresponding to each test case.
  • test conditions If it is determined that the test conditions are not met, regenerate each test case, and test the code to be tested based on each regenerated test case until the test conditions are met. When the test conditions are met, the test conditions are met. Generated test cases.
  • the terminal device determines whether the preset test conditions are met based on the test results of each test case. If it is determined that the test conditions are not met, each test case is regenerated, and the code to be tested is tested based on the regenerated test cases until the test conditions are met. Until the test conditions are met, the test cases generated when the test conditions are met are obtained.
  • the preset test conditions can be formulated according to the test requirements, for example: the line coverage of the code to be tested reaches the preset threshold, and another example: the number of iterations reaches the preset threshold, etc.
  • the method of regenerating each test case can be to sort the test cases according to the test results of each test case (for example, sort according to the line coverage of the code to be tested), and according to the sorted test cases , filter out each basic test case.
  • the terminal device can select at least some basic test cases from each basic test case and combine them in pairs to obtain each test case combination, and for each test case combination, according to the test case combination, among the two basic test cases
  • the included parameters of the same type are weighted to obtain regenerated test cases.
  • the terminal device can also select at least some basic test cases from each basic test case, and for each selected basic test case, numerically adjust the parameters included in the basic test case, and/or change the The code under test contained in the basic test case is adjusted to regenerate each test case.
  • the MethodDescripter structure contains: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, the type of parameters passed in by the calling method, and regenerates each test case to treat Test each incoming parameter type of the generic method called by the test code, as well as the actual return value type.
  • the terminal device can generate initial test cases for the set code and pass them through In the process of testing the set code through each initial test case, each initial test case is optimized through iteration based on the test results of each initial test case, so that the final generated test case can test the set code. Code, conduct comprehensive testing, thereby improving testing results.
  • Figure 4 is a schematic diagram of a code testing device provided in this specification, including: an acquisition module 401, used to obtain the code to be tested; a determination module 402, used to determine the type of parameters required by the method called by the code to be tested. , and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, determine the actual return value type corresponding to the method called when the code to be tested is tested, when the code to be tested is tested.
  • the method type of the called method includes: at least one of a generic method and forced type conversion; the testing module 403 is used to obtain test results for the code to be tested according to the actual return value type.
  • the determination module 402 is specifically configured to obtain the code of the method called by the code to be tested; and determine the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested.
  • the actual return value of the method called during code testing is related to the parameter type required by the method called by the code to be tested; according to the related relationship, and the parameters required by the method called by the code to be tested.
  • Type determine the actual return value type corresponding to the method called when testing the code to be tested.
  • the test module 403 is specifically configured to create a method data structure MethodDescripter structure according to the association relationship.
  • the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, and the method of calling the method. name, the class name of the calling method, and the incoming parameters of the calling method; according to the MethodDescripter structure and the actual return value type, a mock method corresponding to the method called by the code to be tested is generated; through the mock method , to obtain the test results for the code to be tested.
  • the determination module 402 is specifically configured to determine, from the methods called by the code to be tested, that the method type is a forced type conversion method by analyzing the bytecode corresponding to the code to be tested. , as the target method; when testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested according to the target method.
  • the device further includes: an analysis module 404, configured to compare the code line number corresponding to the target method in the code to be tested with the actual return value of the target method after forced type conversion.
  • the corresponding relationship between the value types is saved; the determination module 402 is specifically used to determine if during the testing process of the code to be tested, The method called by the code to be tested is determined to be the code line number of the forced type conversion method as the target line number; according to the target line number, the code called by the code to be tested is queried from the pre-saved correspondence.
  • the actual return value type corresponding to the method is performed by the code line number of the forced type conversion method.
  • the testing module 403 is specifically configured to generate a response value corresponding to the method called by the code to be tested based on querying the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence relationship.
  • the mock method through the mock method, the test results for the code to be tested are obtained.
  • Figure 5 is a schematic diagram of a test case generation device provided in this specification, including: a use case acquisition module 501, used to obtain each test case; a use case testing module 502, used for each test case, through the above code testing method , test the set code to obtain the test results corresponding to the test case; the judgment module 503 is used to determine whether the preset test conditions are met based on the test results corresponding to each test case; the generation module 504 is used to determine whether When the test conditions are not met, each test case is regenerated, and the set code is tested according to each regenerated test case until the test condition is met, and the test generated when the test condition is met is obtained.
  • a use case acquisition module 501 used to obtain each test case
  • a use case testing module 502 used for each test case, through the above code testing method , test the set code to obtain the test results corresponding to the test case
  • the judgment module 503 is used to determine whether the preset test conditions are met based on the test results corresponding to each test case
  • the generation module 504 is specifically configured to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, combine the two test case combinations included in the test case combination. Parameters of the same type in each test case are weighted to obtain a regenerated test case.
  • the generation module 504 is specifically configured to select at least some test cases from each test case; for each selected test case, numerically adjust at least some parameters included in the test case, and/or Adjust the code to be tested contained in the test case to obtain a regenerated test case.
  • This specification also provides a computer-readable storage medium that stores a computer program.
  • the computer program can be used to execute the method of code testing and test case generation provided in Figure 1 above.
  • FIG. 6 This specification also provides a schematic structural diagram of the electronic device shown in FIG. 6 corresponding to FIG. 1 .
  • the electronic device includes a processor, internal bus, network interface, memory and non-volatile memory, and of course may also include other hardware required for business.
  • the processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to implement the method of code testing and test case generation described in Figure 1 above.
  • this specification does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logical unit, and may also be hardware or logic device.
  • PLD Programmable Logic Device
  • FPGA Field Programmable Gate Array
  • HDL Hardware Description Language
  • HDL High-Speed Integrated Circuit Hardware Description Language
  • ABEL Advanced Boolean Expression Language
  • AHDL Advanced Boolean Expression Language
  • Confluence CUPL
  • HDCal Component Description Language
  • JHDL Java Hardware Description Language
  • Lava Lava
  • Lola MyHDL
  • PALASM RHDL
  • VHDL Very-High-Speed Integrated Circuit Hardware Description Language
  • Verilog Verilog
  • the controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, a microprocessor or processor and a computer readable medium storing computer readable program code (eg, software or firmware) executable by the (micro)processor. , logic gates, switches, Application Specific Integrated Circuit (ASIC), programmable logic controllers and embedded microcontrollers.
  • controllers include but are not limited to the following microcontrollers: ARC 625D, Atmel AT91SAM, For Microchip PIC18F26K20 and Silicone Labs C8051F320, the memory controller can also be implemented as part of the memory's control logic.
  • the controller in addition to implementing the controller in the form of pure computer-readable program code, the controller can be completely programmed with logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded logic by logically programming the method steps. Microcontroller, etc. to achieve the same function. Therefore, this controller can be considered as a hardware component, and the devices included therein for implementing various functions can also be considered as structures within the hardware component. Or even, the means for implementing various functions can be considered as structures within hardware components as well as software modules implementing the methods.
  • a typical implementation device is a computer.
  • computer examples For example, it may be a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any of these devices. Any combination of equipment.
  • embodiments of the present specification may be provided as methods, systems, or computer program products.
  • the present description may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment that combines software and hardware aspects.
  • the present description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk memory, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
  • These computer program instructions may also be stored in a computer-readable memory that causes a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including the instruction means, the instructions
  • the device implements the functions specified in a process or processes of the flowchart and/or a block or blocks of the block diagram.
  • These computer program instructions may also be loaded onto a computer or other programmable data processing device, causing a series of operating steps to be performed on the computer or other programmable device to produce computer-implemented processing, thereby executing on the computer or other programmable device.
  • Instructions provide steps for implementing the functions specified in a process or processes of a flowchart diagram and/or a block or blocks of a block diagram.
  • a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
  • processors CPUs
  • input/output interfaces network interfaces
  • memory volatile and non-volatile memory
  • Memory may include non-permanent storage in computer-readable media, random access memory (RAM), and/or non-volatile memory in the form of read-only memory (ROM) or flash memory (flash RAM).
  • RAM random access memory
  • ROM read-only memory
  • flash RAM flash memory
  • Computer-readable media includes both persistent and non-volatile, removable and non-removable media that can be implemented by any method or technology for storage of information.
  • Information may be computer-readable instructions, data structures, modules of programs, or other data.
  • Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), and read-only memory.
  • PRAM phase change memory
  • SRAM static random access memory
  • DRAM dynamic random access memory
  • RAM random access memory
  • read-only memory read-only memory
  • ROM read-only memory
  • EEPROM electrically erasable programmable read-only memory
  • flash memory or other memory technology
  • compact disc read-only memory CD-ROM
  • DVD digital versatile disc
  • Magnetic tape cassettes tape magnetic disk storage or other magnetic storage devices or any other non-transmission medium can be used to store information that can be accessed by a computing device.
  • computer-readable media does not include transitory media, such as modulated data signals and carrier waves.
  • embodiments of the present specification may be provided as methods, systems, or computer program products.
  • the present description may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment that combines software and hardware aspects.
  • the present description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk memory, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
  • program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types.
  • the present description may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through communications networks.
  • program modules may be located in both local and remote computer storage media including storage devices.

Abstract

Disclosed in the present description are a code test method and apparatus, and a test case generation method and apparatus. When a code to be tested is under test, it is possible to accurately determine actual return value types of a generic method which is called during a running process of said code, and a method in which forced type casting occurs, such that said code can be tested on the basis of the determined actual return value types, and the accuracy of a test result is thus improved.

Description

一种代码测试、测试用例生成的方法及装置A method and device for code testing and test case generation 技术领域Technical field
本说明书涉及计算机技术领域,尤其涉及一种代码测试、测试用例生成的方法及装置。This description relates to the field of computer technology, and in particular, to a method and device for code testing and test case generation.
背景技术Background technique
在软件测试的过程中,由于待测试方法可能会与其他方法之间存在较高的依赖,从而会导致测试人员不能单独针对待测试方法进行测试,进而影响了测试工作的进行,因此,为了解决这个问题,软件的测试人员通常会通过Mock服务(即,依赖服务替身),用来代替与待测试方法相关联的其他方法,来进行待测试方法的测试工作。In the process of software testing, since the method to be tested may have a high dependence on other methods, the tester will not be able to test the method to be tested alone, which will affect the progress of the testing work. Therefore, in order to solve the problem For this problem, software testers usually use Mock services (that is, dependent service substitutes) to replace other methods associated with the method to be tested to test the method to be tested.
但是,由于在使用Mock服务时,需要确定待测试方法所调用的方法的返回值类型,对于普通的方法来说,可以直接确定出方法的返回值类型,而对于一些返回值需要在根据实际运行时,才能确定出具体类型的方法而言,往往会随机确定一个类型作为该方法的返回值类型,这样就降低了生成的测试用例的准确性,影响了测试用例的覆盖率。However, when using the Mock service, you need to determine the return value type of the method called by the method to be tested. For ordinary methods, you can directly determine the return value type of the method, but for some return values, you need to determine the return value type according to the actual operation. For methods that can only determine the specific type at the time, a type is often randomly determined as the return value type of the method, which reduces the accuracy of the generated test cases and affects the coverage of the test cases.
因此,如何能够在进行软件的测试工作时,有效的确定出待测试方法的返回值类型,进而提升待测试方法的测试结果的准确性,则是一个亟待解决的问题。Therefore, how to effectively determine the return value type of the method to be tested during software testing, and thereby improve the accuracy of the test results of the method to be tested, is an urgent problem that needs to be solved.
发明内容Contents of the invention
本说明书提供一种代码测试、测试用例生成的方法及装置,以部分的解决相关技术存在的返回错误类型的返回值而导致的测试结果准确性较低的问题。This specification provides a method and device for code testing and test case generation to partially solve the problem in related technologies of low accuracy of test results caused by returning error type return values.
本说明书采用下述技术方案:本说明书提供了一种代码测试的方法,包括:获取待测试代码;根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种;根据所述实际返回值类型,得到针对所述待测试代码的测试结果。This manual adopts the following technical solution: This manual provides a code testing method, including: obtaining the code to be tested; according to the type of parameters required for the method called by the code to be tested, and/or testing the code to be tested. The analysis results obtained after analyzing the bytecode corresponding to the code determine the actual return value type corresponding to the method called when testing the code to be tested. The method types of the methods called when testing the code to be tested include: At least one of a generic method and forced type conversion; according to the actual return value type, the test result for the code to be tested is obtained.
可选地,根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方 法对应的实际返回值类型,具体包括:获取所述待测试代码所调用的方法的代码;根据获取到的所述待测试代码所调用的方法的代码,确定所述在所述待测试代码测试时所调用的方法的实际返回值,与所述待测试代码所调用的方法所需参数类型的关联关系;根据所述关联关系,以及所述待测试代码所调用的方法所需参数的类型,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型。Optionally, according to the type of parameters required by the method called by the code to be tested, and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, it is determined that the code to be tested is method called when The actual return value type corresponding to the method specifically includes: obtaining the code of the method called by the code to be tested; and determining the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested. The actual return value of the method called, and the association relationship between the parameter type required by the method called by the code to be tested; according to the association relationship, and the type of parameters required by the method called by the code under test, Determine the actual return value type corresponding to the method called when testing the code to be tested.
可选地,根据所述实际返回值类型,得到针对所述待测试代码的测试结果,具体包括:根据所述关联关系,创建方法数据结构MethodDescripter结构,所述MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数;根据所述MethodDescripter结构,以及所述实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, according to the actual return value type, obtain the test result for the code to be tested, which specifically includes: creating a method data structure MethodDescripter structure according to the association relationship, the MethodDescripter structure includes: the actual return of the calling method Value type, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, and the incoming parameters of the calling method; generate the code to be tested based on the MethodDescripter structure and the actual return value type The mock method corresponding to the called method; through the mock method, the test results for the code to be tested are obtained.
可选地,根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,具体包括:通过对所述待测试代码对应的字节码的分析,确定从所述待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标方法;在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型。Optionally, according to the type of parameters required by the method called by the code to be tested, and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, it is determined that the code to be tested is The actual return value type corresponding to the method called, specifically includes: by analyzing the bytecode corresponding to the code to be tested, determining that the method type from the method called by the code to be tested is forced type conversion The method is used as the target method; when testing the code to be tested, the actual return value type corresponding to the method called by the code to be tested is determined according to the target method.
可选地,在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型之前,所述方法还包括:将所述目标方法在所述待测试代码中对应的代码行号,与所述目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存;在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型,具体包括:若在所述待测试代码的测试过程中,确定所述待测试代码所调用的方法为强制类型转换的方法的代码行号,作为目标行号;根据所述目标行号,从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型。Optionally, when testing the code to be tested, before determining the actual return value type corresponding to the method called by the code to be tested according to the target method, the method further includes: converting the The corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type after forced type conversion of the return value of the target method is saved; when testing the code to be tested, According to the target method, determine the actual return value type corresponding to the method called by the code to be tested, specifically including: if during the testing process of the code to be tested, determine the method called by the code to be tested The code line number of the forced type conversion method is used as the target line number; according to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved correspondence relationship.
可选地,根据所述实际返回值类型,得到针对所述待测试代码的测试结果,具体包括:根据从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, according to the actual return value type, obtaining the test result for the code to be tested specifically includes: querying the actual return value corresponding to the method called by the code to be tested from the pre-saved correspondence relationship Type, generate a mock method corresponding to the method called by the code to be tested; use the mock method to obtain test results for the code to be tested.
本说明书提供了一种测试用例生成的方法,包括:获取各测试用例;针对每个测试 用例,通过上述代码测试的方法,对设定代码进行测试,得到该测试用例对应的测试结果;根据每个测试用例对应的测试结果,确定是否满足预设的测试条件;若确定不满足所述测试条件,重新生成各测试用例,并根据重新生成的各测试用例,对所述设定代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。This manual provides a method for generating test cases, including: obtaining each test case; for each test Use case, through the above code testing method, test the set code to obtain the test results corresponding to the test case; according to the test results corresponding to each test case, determine whether the preset test conditions are met; if it is determined that the preset test conditions are not met, test conditions, regenerate each test case, and test the set code according to each regenerated test case until the test condition is met, and obtain the test case generated when the test condition is met.
可选地,重新生成各测试用例,具体包括:从各测试用例中选取至少部分测试用例进行两两组合,得到各测试用例组合;针对每个测试用例组合,将该测试用例组合中包含的两个测试用例内的同类型参数进行加权,得到重新生成的测试用例。Optionally, regenerating each test case specifically includes: selecting at least some test cases from each test case and combining them in pairs to obtain each test case combination; for each test case combination, combining the two test cases included in the test case combination. Parameters of the same type in each test case are weighted to obtain a regenerated test case.
可选地,重新生成各测试用例,具体包括:从各测试用例中选取至少部分测试用例;针对选取出的每个测试用例,将该测试用例中包含的至少部分参数进行数值调整,和/或将该测试用例中包含的待测试代码进行调整,得到重新生成的测试用例。Optionally, regenerating each test case specifically includes: selecting at least some test cases from each test case; for each selected test case, numerically adjusting at least some parameters included in the test case, and/or Adjust the code to be tested contained in the test case to obtain a regenerated test case.
本说明书提供了一种代码测试的装置,包括:获取模块,用于获取待测试代码;确定模块,用于根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种;测试模块,用于根据所述实际返回值类型,得到针对所述待测试代码的测试结果。This specification provides a code testing device, including: an acquisition module, used to obtain the code to be tested; a determination module, used to determine the type of parameters required by the method called by the code to be tested, and/or to determine the The analysis results obtained after analyzing the bytecode corresponding to the code to be tested determine the actual return value type corresponding to the method called when the code to be tested is tested, and the method type of the method called when the code to be tested is tested. It includes: at least one of a generic method and forced type conversion; a test module, used to obtain test results for the code to be tested according to the actual return value type.
可选地,所述确定模块具体用于,获取所述待测试代码所调用的方法的代码;根据获取到的所述待测试代码所调用的方法的代码,确定所述在所述待测试代码测试时所调用的方法的实际返回值,与所述待测试代码所调用的方法所需参数类型的关联关系;根据所述关联关系,以及所述待测试代码所调用的方法所需参数的类型,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型。Optionally, the determination module is specifically configured to obtain the code of the method called by the code to be tested; and determine the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested. The relationship between the actual return value of the method called during testing and the type of parameters required by the method called by the code to be tested; according to the relationship, and the type of parameters required by the method called by the code under test , determine the actual return value type corresponding to the method called when testing the code to be tested.
可选地,所述测试模块具体用于,根据所述关联关系,创建方法数据结构MethodDescripter结构,所述MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数;根据所述MethodDescripter结构,以及所述实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, the test module is specifically configured to create a method data structure MethodDescripter structure according to the association relationship. The MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, and the method of calling the method. name, the class name of the calling method, and the incoming parameters of the calling method; according to the MethodDescripter structure and the actual return value type, a mock method corresponding to the method called by the code to be tested is generated; through the mock method , to obtain the test results for the code to be tested.
可选地,所述确定模块具体用于,通过对所述待测试代码对应的字节码的分析,确定从所述待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标 方法;在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型。Optionally, the determination module is specifically configured to determine, from the methods called by the code to be tested, that the method type is a forced type conversion method by analyzing the bytecode corresponding to the code to be tested, as target Method; when testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested according to the target method.
可选地,所述装置还包括:分析模块,用于将所述目标方法在所述待测试代码中对应的代码行号,与所述目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存;所述确定模块具体用于,若在所述待测试代码的测试过程中,确定所述待测试代码所调用的方法为强制类型转换的方法的代码行号,作为目标行号;根据所述目标行号,从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型。Optionally, the device further includes: an analysis module, configured to compare the code line number corresponding to the target method in the code to be tested with the actual return value of the return value of the target method after forced type conversion. The corresponding relationship of the types is saved; the determination module is specifically used to determine, during the testing process of the code to be tested, that the method called by the code to be tested is the code line number of the forced type conversion method, as the target Line number; according to the target line number, query the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence.
可选地,所述测试模块具体用于,根据从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, the test module is specifically configured to generate the actual return value type corresponding to the method called by the code to be tested based on the query from the pre-saved correspondence relationship, and generate the return value type corresponding to the method called by the code to be tested. Mock method; through the mock method, test results for the code to be tested are obtained.
本说明书提供了一种测试用例生成的装置,包括:用例获取模块,用于获取各测试用例;用例测试模块,用于针对每个测试用例,通过上述代码测试的方法,对设定代码进行测试,得到该测试用例对应的测试结果;判断模块,用于根据每个测试用例对应的测试结果,确定是否满足预设的测试条件;生成模块,用于在确定不满足所述测试条件时,重新生成各测试用例,并根据重新生成的各测试用例,对所述设定代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。This manual provides a device for generating test cases, including: a use case acquisition module, used to obtain each test case; a use case testing module, used to test the set code for each test case through the above code testing method , obtain the test results corresponding to the test case; the judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case; the generation module is used to re-run the test when it is determined that the test conditions are not met. Each test case is generated, and the set code is tested according to each regenerated test case until the test condition is met, and the test case generated when the test condition is met is obtained.
可选地,所述生成模块具体用于,从各测试用例中选取至少部分测试用例进行两两组合,得到各测试用例组合;针对每个测试用例组合,将该测试用例组合中包含的两个测试用例内的同类型参数进行加权,得到重新生成的测试用例。Optionally, the generation module is specifically configured to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, combine the two test case combinations included in the test case combination. Parameters of the same type in the test case are weighted to obtain a regenerated test case.
可选地,所述生成模块具体用于,从各测试用例中选取至少部分测试用例;针对选取出的每个测试用例,将该测试用例中包含的至少部分参数进行数值调整,和/或将该测试用例中包含的待测试代码进行调整,得到重新生成的测试用例。Optionally, the generation module is specifically configured to select at least some test cases from each test case; for each selected test case, numerically adjust at least some parameters contained in the test case, and/or The code to be tested contained in the test case is adjusted to obtain a regenerated test case.
本说明书提供了一种计算机可读存储介质,所述存储介质存储有计算机程序,所述计算机程序被处理器执行时实现上述代码测试、测试用例生成的方法。This specification provides a computer-readable storage medium. The storage medium stores a computer program. When the computer program is executed by a processor, the above code testing and test case generation methods are implemented.
本说明书提供了一种电子设备,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现上述代码测试、测试用例生成的方法。This specification provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the above code testing and test case generation methods are implemented.
本说明书采用的上述至少一个技术方案能够达到以下有益效果:在本说明书提供的 代码测试的方法,首选获取待测试代码,进而根据待测试代码所调用的方法所需参数的类型,和/或对待测试代码对应的字节码进行分析后得到的分析结果,确定在待测试代码测试时所调用的方法对应的实际返回值类型,待测试代码测试时所调用的方法的方法类型包括:调用泛型方法以及强制类型转换中的至少一种,并根据实际返回值类型,得到针对待测试代码的测试结果。At least one of the above technical solutions adopted in this manual can achieve the following beneficial effects: The first method of code testing is to obtain the code to be tested, and then determine the code to be tested based on the type of parameters required by the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested. The actual return value type corresponding to the method called during testing. The method type of the method called when testing the code to be tested includes at least one of calling a generic method and forced type conversion, and based on the actual return value type, the specific return value type is obtained. Test results of the code under test.
从上述方法中可以看出,可以在待测试代码测试时,准确的确定出在待测试代码在运行过程中所调用的泛型方法和发生强制类型转换的方法的实际返回值类型,从而可以基于确定出的实际返回值类型,对待测试代码进行测试,进而提升了测试结果的准确性。As can be seen from the above method, when testing the code to be tested, the actual return value type of the generic method called during the running process of the code to be tested and the method where forced type conversion occurs can be accurately determined, so that the actual return value type can be determined based on The determined actual return value type is used to test the code to be tested, thereby improving the accuracy of the test results.
附图说明Description of the drawings
此处所说明的附图用来提供对本说明书的进一步理解,构成本说明书的一部分,本说明书的示意性实施例及其说明用于解释本说明书,并不构成对本说明书的不当限定。在附图中:The drawings described here are used to provide a further understanding of this specification and constitute a part of this specification. The illustrative embodiments and descriptions of this specification are used to explain this specification and do not constitute an improper limitation of this specification. In the attached picture:
图1为本说明书中提供的一种代码测试的方法的流程示意图;Figure 1 is a schematic flow chart of a code testing method provided in this manual;
图2为本说明书中提供的泛型方法的实际返回值类型的确定方法的示意图;Figure 2 is a schematic diagram of the method for determining the actual return value type of the generic method provided in this specification;
图3为本说明书提供的一种测试用例生成的方法的流程示意图;Figure 3 is a schematic flow chart of a test case generation method provided in this manual;
图4为本说明书提供的一种代码测试的装置的示意图;Figure 4 is a schematic diagram of a code testing device provided in this specification;
图5为本说明书提供的一种测试用例生成的装置的示意图;Figure 5 is a schematic diagram of a device for generating test cases provided in this specification;
图6为本说明书提供的一种对应于图1的电子设备的示意图。FIG. 6 is a schematic diagram of an electronic device corresponding to FIG. 1 provided in this specification.
具体实施方式Detailed ways
为使本说明书的目的、技术方案和优点更加清楚,下面将结合本说明书具体实施例及相应的附图对本说明书技术方案进行清楚、完整地描述。显然,所描述的实施例仅是本说明书一部分实施例,而不是全部的实施例。基于本说明书中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本说明书保护的范围。In order to make the purpose, technical solutions and advantages of this specification more clear, the technical solutions of this specification will be clearly and completely described below in conjunction with specific embodiments of this specification and the corresponding drawings. Obviously, the described embodiments are only some of the embodiments of this specification, but not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the scope of protection of this specification.
以下结合附图,详细说明本说明书各实施例提供的技术方案。The technical solutions provided by each embodiment of this specification will be described in detail below with reference to the accompanying drawings.
图1为本说明书中提供的一种代码测试的方法的流程示意图,包括以下步骤S101 至S103。Figure 1 is a schematic flow chart of a code testing method provided in this manual, including the following steps S101 Go to S103.
S101:获取待测试代码。S101: Obtain the code to be tested.
在本说明书中,测试人员可以基于测试需求,对待测试软件的代码进行测试,具体地,测试人员可以先从待测试软件的代码中,确定出一部分代码,作为待测试代码,进而可以对待测试代码进行测试,以得到针对待测试代码的进行测试的测试结果。In this manual, the tester can test the code of the software to be tested based on the testing requirements. Specifically, the tester can first determine a part of the code from the code of the software to be tested as the code to be tested, and then the code to be tested can be Conduct tests to obtain test results for the code to be tested.
其中,待测试代码可以是指作为待测试软件的代码中一部分代码的集合,例如:待测试代码可以是待测试软件的代码中包含的一个或多个待测试类(这里的类是指一种自定义的引用数据类型,类中封装了若干个属性和方法)对应的代码,再例如:待测试代码还可以是待测试软件的代码中包含的一个或多个方法(即,代码语句的集合,这些代码语句一起执行了一个功能,方法包含于类或类的对象中)对应的代码。The code to be tested may refer to a collection of codes that are part of the code of the software to be tested. For example, the code to be tested may be one or more classes to be tested (a class here refers to a A custom reference data type, the class encapsulates the code corresponding to several attributes and methods). Another example: the code to be tested can also be one or more methods (i.e., a collection of code statements) contained in the code of the software to be tested. , these code statements together perform a function (the method is contained in the class or object of the class) corresponding code.
在本说明书中,用于实现代码测试的方法的执行主体,可以是指终端设备等设置于业务平台的指定设备,也可以是指诸如台式电脑、笔记本电脑等终端设备,为了便于描述,下面仅以终端设备是执行主体为例,对本说明书提供的代码测试的方法进行说明。In this specification, the execution subject of the method for implementing code testing may refer to a designated device such as a terminal device installed on the business platform, or may refer to a terminal device such as a desktop computer, laptop computer, etc. For the convenience of description, only Taking the terminal device as the execution subject as an example, the code testing method provided in this manual will be explained.
所以,终端设备在接收到测试人员发送的测试指令后,可以先获取待测试代码,进而针对待测试代码进行测试,以得到待测试代码的测试结果。Therefore, after receiving the test instruction sent by the tester, the terminal device can first obtain the code to be tested, and then test the code to be tested to obtain the test results of the code to be tested.
S102:根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种。S102: Based on the type of parameters required for the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, determine the parameters used when the code to be tested is tested. The actual return value type corresponding to the called method. The method type of the method called when testing the code to be tested includes at least one of a generic method and forced type conversion.
在待测试代码运行时,终端设备可以拦截待测试代码在运行过程中调用方法的调用指令,根据拦截的调用指令,确定待测试代码在运行过程中调用方法的方法类型。When the code under test is running, the terminal device can intercept the calling instructions of the method called by the code under test during running, and determine the method type of the method called by the code under test during running based on the intercepted calling instructions.
其中,待测试代码在运行过程中调用方法的方法类型可以是泛型方法(即,方法的传入参数的类型不确定的方法)以及强制类型转换(即,在调用过程中发生强制类型转换的方法,其中,发生强制类型转换是将方法原本的返回值的类型,转换成了另一种数据类型)中的至少一种。Among them, the method type of the method called by the code under test during the running process can be a generic method (that is, a method whose incoming parameters are of uncertain type) and a forced type conversion (that is, a method where a forced type conversion occurs during the calling process). method, in which the forced type conversion occurs when the original return value type of the method is converted into at least one of another data type).
需要说明的是,上述内容中的拦截并不是字面意义上的拦截,而是在待测试代码的运行过程中,监听待测试代码在运行过程中调用方法的调用指令,通过动态代理的方式,实现的一种类似于拦截效果的方法。 It should be noted that the interception in the above content is not interception in the literal sense, but monitoring the calling instructions of the method called by the code under test during the running process, through the method of dynamic proxy. A method similar to the interception effect.
基于对上述方法类型的介绍,本说明书中主要是对上述两种方法类型的方法,即对泛型方法和强制类型转换的实际返回值类型进行确定,所以,下面将针对如何确定两种方法类型的方法的实际返回值类型,进行详细说明。Based on the introduction of the above method types, this manual mainly focuses on determining the actual return value types of the above two method types, that is, the generic method and the forced type conversion. Therefore, the following will focus on how to determine the two method types. The actual return value type of the method is explained in detail.
对于泛型方法来说,终端设备可以在拦截到的待测试代码中对泛型方法的调用指令后,可以获取待测试代码中调用的泛型方法的代码,从而根据获取到的待测试代码所调用的泛型方法的代码,确定在待测试代码测试时所调用的方法的实际返回值类型,与待测试代码所调用的方法所需参数类型的关联关系,进而可以基于确定出的关联关系,确定在待测试代码测试时所调用的泛型方法对应的实际返回值类型,如图2所示。For generic methods, the terminal device can obtain the code of the generic method called in the code to be tested after intercepting the calling instruction of the generic method in the code to be tested. The code of the generic method called determines the relationship between the actual return value type of the method called when the code to be tested is tested, and the required parameter type of the method called by the code to be tested, and then based on the determined relationship, Determine the actual return value type corresponding to the generic method called when testing the code to be tested, as shown in Figure 2.
图2为本说明书中提供的泛型方法的实际返回值类型的确定方法的示意图。Figure 2 is a schematic diagram of a method for determining the actual return value type of a generic method provided in this specification.
结合图2可以看出,终端设备可以在待测试代码运行的过程中,监听到待测试代码在运行过程中调用方法的调用指令,进而可以在被调用的方法执行之前,可以获取待测试代码中调用的泛型方法的代码,从而对获取到的待测试代码所调用的泛型方法的代码进行泛型分析,确定在待测试代码测试时所调用的泛型方法的各种实际返回值类型,与待测试代码所调用的泛型方法所需的各种参数类型(即,待测试代码调用该方法时所需要传入的参数类型)之间的关联关系。As can be seen from Figure 2, the terminal device can monitor the calling instructions of the method under test when the code under test is running, and then can obtain the calling instructions in the code under test before the called method is executed. The code of the generic method called, so as to conduct a generic analysis of the code of the generic method called by the code to be tested, and determine the various actual return value types of the generic method called when the code to be tested is tested, The association between the various parameter types required by the generic method called by the code under test (that is, the parameter types that need to be passed in when the code under test calls the method).
进一步地,终端设备可以根据确定出的关联关系,创建各方法数据结构MethodDescripter结构(针对泛型方法的每种传入的参数类型,该传入的参数类型与该传入的参数类型对应的实际返回值类型的组合,对应一个MethodDescripter结构),进而可以根据拦截的调用指令,确定待测试代码调用泛型方法时的实际传入的参数,并根据传入的参数,在各MethodDescripter结构中,查询与该传入的参数相匹配的MethodDescripter结构,进而可以确定出待测试代码测试时所调用的泛型方法的实际返回值类型。Further, the terminal device can create a MethodDescripter structure for each method data structure according to the determined association relationship (for each incoming parameter type of the generic method, the incoming parameter type corresponds to the actual The combination of return value types corresponds to a MethodDescripter structure), and then based on the intercepted calling instructions, the actual parameters passed in when the code under test calls the generic method can be determined, and based on the parameters passed in, the query can be performed in each MethodDescripter structure. The MethodDescripter structure that matches the passed-in parameter can then determine the actual return value type of the generic method called when testing the code to be tested.
其中,MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数、MethodDescripter结构的唯一标识ID。Among them, the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, the incoming parameters of the calling method, and the unique identification ID of the MethodDescripter structure.
进一步地,终端设备可以基于与该传入的参数相匹配的MethodDescripter结构,生成待测试代码所调用的方法对应的mock方法,进而可以通过mock方法,模拟生成待测试代码调用的方法的返回值,以得到针对待测试代码的测试结果。Further, the terminal device can generate a mock method corresponding to the method called by the code to be tested based on the MethodDescripter structure that matches the incoming parameter, and then can simulate and generate the return value of the method called by the code to be tested through the mock method. To get the test results for the code to be tested.
需要说明的是,由于在同一个泛型方法在待测试代码中可能被多次调用,因此,在 生成该泛型方法对应的mock方法时,需要根据MethodDescripter结构的被调用次数,按照调用MethodDescripter结构的顺序组装mock语句,生成mock方法。It should be noted that since the same generic method may be called multiple times in the code to be tested, When generating the mock method corresponding to the generic method, you need to assemble the mock statements in the order in which the MethodDescripter structure is called according to the number of times the MethodDescripter structure is called, and generate the mock method.
对于发生强制类型转换的方法来说,终端设备可以在对待测试代码进行测试之前,预先通过对待测试代码对应的字节码的分析,确定从待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标方法,并将目标方法在待测试代码中对应的代码行号,与目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存(考虑到保存到哈希表的数据易读取的特点,可以将这里的对应关系保存到哈希表中)。For methods where forced type conversion occurs, the terminal device can determine that the method type from the method called by the code to be tested is mandatory by analyzing the bytecode corresponding to the code to be tested in advance. The method of type conversion is used as the target method, and the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type after forced type conversion of the return value of the target method is saved (taking into account that it is saved to The data in the hash table is easy to read, and the corresponding relationship here can be saved in the hash table).
完成上述对应关系的存储后,终端设备即可在代码测试过程中,基于上述对应关系,确定出实际返回值类型。具体的,可以在拦截到待测试代码中调用的强制类型转换的方法的调用指令后,获取调用方法的方法信息,这里的方法信息包括:调用方法的代码,以及调用方法的上下文信息,其中,调用方法的上下文信息包括:调用方法定义的变量对象、调用方法的作用域链(即,调用方法中查找变量的顺序链表)、调用者的调用代码(即,待测试代码中调用泛型方法或发生强制转换的方法的代码)等。After completing the storage of the above correspondence, the terminal device can determine the actual return value type based on the above correspondence during the code testing process. Specifically, after intercepting the calling instruction of the forced type conversion method called in the code to be tested, the method information of the calling method can be obtained. The method information here includes: the code of the calling method, and the context information of the calling method, where, The context information of the calling method includes: the variable object defined by the calling method, the scope chain of the calling method (i.e., the sequence linked list for finding variables in the calling method), the caller's calling code (i.e., the calling code of the generic method or The code of the method where the cast occurs), etc.
进一步地,终端设备可以根据获取的调用方法的方法信息,确定待测试代码所调用的发生强制类型转换的方法的代码行号,作为目标行号,进而根据目标行号,从预先保存的发生强制类型转换的方法在待测试代码中对应的代码行号,与发生强制类型转换的方法的返回值经强制类型转换后的实际返回值类型的对应关系中,查询出待测试代码所调用的发生强制类型转换的方法对应的实际返回值类型。Further, the terminal device can determine the code line number of the method called by the code to be tested where the forced type conversion occurs based on the obtained method information of the calling method, as the target line number, and then based on the target line number, from the pre-saved code where the forced type conversion occurs The type conversion method can query the corresponding code line number in the code to be tested and the actual return value type after the forced type conversion of the return value of the method where the forced type conversion occurs. The actual return value type corresponding to the type conversion method.
由于在实际应用中,可能会在对待测试代码进行字节码分析时,没有将待测试代码中包含的全部发生强制转换的方法识别出来,这样将导致在实际的代码测试过程中,可能无法根据待测试代码中的发生强制转换的方法的代码行号,从预先保存的对应关系中,查询出待测试代码所调用的发生强制类型转换的方法对应的实际返回值类型。Since in actual applications, when performing bytecode analysis on the code to be tested, all the forced conversion methods contained in the code to be tested may not be identified. This will lead to the possibility that during the actual code testing process, it may not be possible to use The code line number of the method in the code to be tested where forced conversion occurs, and the actual return value type corresponding to the method called by the code under test that requires forced type conversion is queried from the pre-saved correspondence.
对于这种情况,在本说明书中,终端设备可以从预设的类型池中,任意选取一个数据类型,作为候选返回值类型,进而可以将该候选返回值类型,作为待测试代码测试时所调用的发生强制类型转换的方法的实际返回值类型,并得到基于候选返回值类型的测试结果,而后,可以根据测试结果,判断该候选返回值类型是否为待测试代码测试时所调用的发生强制类型转换的方法的实际返回值类型;若否,则再次从类型池中选取一个返回值类型,作为候选返回值类型,直到确定该候选返回值类型为待测试代码测试时所调用的发生强制类型转换的方法的实际返回值类型为止。 For this situation, in this manual, the terminal device can arbitrarily select a data type from the preset type pool as a candidate return value type, and then use the candidate return value type as the code to be tested when testing. The actual return value type of the method where the forced type conversion occurs, and the test results based on the candidate return value type are obtained. Then, based on the test results, it can be judged whether the candidate return value type is the forced type called when testing the code to be tested. The actual return value type of the converted method; if not, select a return value type from the type pool again as a candidate return value type until it is determined that the candidate return value type is the one called when testing the code to be tested. to the actual return value type of the method.
其中,上述内容中的类型池中的数据类型,可以是终端设备在待测试代码运行前预先确定出的。具体的,终端设备可以通过对待测试代码对应的字节码进行分析,确定的待测试代码中包含的所有数据类型,之后,可以将确定出的在待测试代码中包含的数据类型,保存到预设的类型池中的。The data types in the type pool in the above content may be predetermined by the terminal device before the code to be tested is run. Specifically, the terminal device can analyze the bytecode corresponding to the code to be tested and determine all the data types contained in the code to be tested. After that, the determined data types contained in the code to be tested can be saved to a predetermined data type. Set the type in the pool.
从上述内容可以看出,终端设备可以在待测试代码测试时,准确的确定出在待测试代码在运行过程中所调用的泛型方法和发生强制类型转换的方法的实际返回值类型,从而可以基于确定出的实际返回值类型,对待测试代码进行测试,进而提升了测试结果的准确性。As can be seen from the above content, the terminal device can accurately determine the actual return value type of the generic method called during the running of the code under test and the method where forced type conversion occurs, so that it can Based on the determined actual return value type, the code to be tested is tested, thereby improving the accuracy of the test results.
在实际应用中,上述的代码测试方法不仅可以用于对待测试代码进行测试,也可以用于生成测试用例,即,通过上述代码测试方法,来构建出能够实现多种测试目标(这里的目标可以是指,对待测试代码的各类覆盖率达到预设的阈值)的一些优选测试用例,下面详细描述测试用例生成的方法,如图3所示。In practical applications, the above code testing method can not only be used to test the code to be tested, but can also be used to generate test cases, that is, through the above code testing method, to build a test case that can achieve a variety of test goals (the goals here can be Refers to some preferred test cases (when the coverage of various types of code to be tested reaches a preset threshold). The method of generating test cases is described in detail below, as shown in Figure 3.
图3为本说明书中提供的一种测试用例生成的方法的流程示意图,包括以下步骤S301至S304。Figure 3 is a schematic flowchart of a test case generation method provided in this specification, including the following steps S301 to S304.
S301:获取各测试用例。S301: Obtain each test case.
在本说明书中,终端设备可以在针对待测试代码进行测试前,获取待测试代码的代码关联信息,进而根据待测试代码,以及待测试代码的关联信息,生成各测试用例,其中,代码关联信息可以是诸如:待测试代码的文档信息、待测试代码的上下文信息等。In this specification, the terminal device can obtain the code association information of the code to be tested before testing the code to be tested, and then generate each test case based on the code to be tested and the association information of the code to be tested, where the code association information It can be, for example, document information of the code to be tested, context information of the code to be tested, etc.
S302:针对每个测试用例,通过上述代码测试的方法,对待测试代码进行测试,得到该测试用例对应的测试结果。S302: For each test case, use the above code testing method to test the code to be tested and obtain the test results corresponding to the test case.
针对每个测试用例,终端设备可以通过上述的代码测试的方法,运行该测试用例,以对该测试用例中包含的设定代码进行测试,得到该测试用例对应的测试结果,其中,设定代码可以是指待测试代码,也可以是指任意的一段代码(例如:已经测试过的代码,也可以作为设定代码,进而通过使用对设定代码进行测试的手段,最终得到一些优选测试用例。)这里的测试结果是指待测试代码的测试效果,诸如:待测试代码的行覆盖率、待测试代码的方法覆盖率、待测试代码的异常覆盖率等。For each test case, the terminal device can run the test case through the above code testing method to test the setting code contained in the test case and obtain the test results corresponding to the test case, where the setting code It can refer to the code to be tested, or it can refer to any piece of code (for example, the code that has been tested, or it can be used as the setting code, and then by using the method of testing the setting code, some optimal test cases are finally obtained. ) The test results here refer to the testing effect of the code to be tested, such as: line coverage of the code to be tested, method coverage of the code to be tested, exception coverage of the code to be tested, etc.
终端设备生成的初始的测试用例,通常不能很好的实现预先设置的多个测试目标,因此,还需要基于这些初始的测试用例进行迭代,以对各初始的测试用例进行优化,最终达成对待测试代码的测试目标,其中,针对各测试用例进行迭代的具体过程,由以下 步骤实现。The initial test cases generated by the terminal device usually cannot achieve the multiple preset test goals well. Therefore, iteration based on these initial test cases is also needed to optimize each initial test case and finally achieve the target to be tested. The test goals of the code, among which, the specific process of iterating on each test case is as follows: steps to achieve.
S303:根据每个测试用例对应的测试结果,确定是否满足预设的测试条件。S303: Determine whether the preset test conditions are met based on the test results corresponding to each test case.
S304:若确定不满足所述测试条件,重新生成各测试用例,并根据重新生成的各测试用例,对所述待测试代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。S304: If it is determined that the test conditions are not met, regenerate each test case, and test the code to be tested based on each regenerated test case until the test conditions are met. When the test conditions are met, the test conditions are met. Generated test cases.
终端设备根据每个测试用例的测试结果,确定是否满足预设的测试条件,若确定不满足测试条件,重新生成各测试用例,并根据重新生成的各测试用例,对待测试代码进行测试,直到满足测试条件为止,得到满足测试条件时生成的测试用例。The terminal device determines whether the preset test conditions are met based on the test results of each test case. If it is determined that the test conditions are not met, each test case is regenerated, and the code to be tested is tested based on the regenerated test cases until the test conditions are met. Until the test conditions are met, the test cases generated when the test conditions are met are obtained.
其中,预设的测试条件可以根据测试需求制定,例如:待测试代码的行覆盖率达到预设阈值,再例如:迭代的次数达到预设阈值等。Among them, the preset test conditions can be formulated according to the test requirements, for example: the line coverage of the code to be tested reaches the preset threshold, and another example: the number of iterations reaches the preset threshold, etc.
上述内容中,重新生成各测试用例的方法可以是根据各测试用例的测试结果,对各测试用例进行排序(例如:按照待测试代码的行覆盖率大小进行排序),根据排序后的各测试用例,筛选得到各基础测试用例。In the above content, the method of regenerating each test case can be to sort the test cases according to the test results of each test case (for example, sort according to the line coverage of the code to be tested), and according to the sorted test cases , filter out each basic test case.
进一步地,终端设备可以从各基础测试用例中选取至少部分基础测试用例进行两两组合,得到各测试用例组合,并针对每个测试用例组合,根据该测试用例组合中,两个基础测试用例中包含的同类型参数进行加权,得到重新生成的测试用例。Further, the terminal device can select at least some basic test cases from each basic test case and combine them in pairs to obtain each test case combination, and for each test case combination, according to the test case combination, among the two basic test cases The included parameters of the same type are weighted to obtain regenerated test cases.
除此之外,终端设备还可以从各基础测试用例中选取至少部分基础测试用例,针对选取出的每个基础测试用例,将该基础测试用例中包含的参数进行数值调整,和/或将该基础测试用例中包含的待测试代码进行调整,以重新生成各测试用例。In addition, the terminal device can also select at least some basic test cases from each basic test case, and for each selected basic test case, numerically adjust the parameters included in the basic test case, and/or change the The code under test contained in the basic test case is adjusted to regenerate each test case.
另外,由于在测试用例运行的过程中,针对待测试代码所调用的泛型方法通常会有多个可以传入的参数的类型,因此,终端设备还可以根据该测试用例在运行过程中生成的MethodDescripter结构中包含的:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法传入参数的类型,重新生成各测试用例,以对待测试代码所调用的泛型方法的各个传入参数类型,以及实际返回值类型进行测试。In addition, since during the running of the test case, the generic method called for the code under test usually has multiple types of parameters that can be passed in, therefore, the terminal device can also generate parameters based on the test case during running. The MethodDescripter structure contains: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, the type of parameters passed in by the calling method, and regenerates each test case to treat Test each incoming parameter type of the generic method called by the test code, as well as the actual return value type.
需要说明的是,上述的三种方法可以单独使用,以重新生成各测试用例,也可以一起使用,以重新生成测试用例。It should be noted that the above three methods can be used individually to regenerate each test case, or can be used together to regenerate test cases.
通过上述内容可以看出,终端设备可以针对设定代码生成初始的测试用例,并在通 过各初始的测试用例对设定代码进行测试的过程中,根据各初始的测试用例的测试结果,通过迭代的方式对各初始的测试用例进行优化,以使最终生成的测试用例能够对设定代码,进行全面的测试,从而提升了测试效果。As can be seen from the above content, the terminal device can generate initial test cases for the set code and pass them through In the process of testing the set code through each initial test case, each initial test case is optimized through iteration based on the test results of each initial test case, so that the final generated test case can test the set code. Code, conduct comprehensive testing, thereby improving testing results.
以上为本说明书的一个或多个实施例提供的代码测试的方法,基于同样的思路,本说明书还提供了相应的代码测试的装置以及测试用例生成的装置,如图4、图5所示。The above is a code testing method provided by one or more embodiments of this specification. Based on the same idea, this specification also provides a corresponding code testing device and a test case generation device, as shown in Figures 4 and 5.
图4为本说明书提供的一种代码测试的装置的示意图,包括:获取模块401,用于获取待测试代码;确定模块402,用于根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种;测试模块403,用于根据所述实际返回值类型,得到针对所述待测试代码的测试结果。Figure 4 is a schematic diagram of a code testing device provided in this specification, including: an acquisition module 401, used to obtain the code to be tested; a determination module 402, used to determine the type of parameters required by the method called by the code to be tested. , and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, determine the actual return value type corresponding to the method called when the code to be tested is tested, when the code to be tested is tested The method type of the called method includes: at least one of a generic method and forced type conversion; the testing module 403 is used to obtain test results for the code to be tested according to the actual return value type.
可选地,所述确定模块402具体用于,获取所述待测试代码所调用的方法的代码;根据获取到的所述待测试代码所调用的方法的代码,确定所述在所述待测试代码测试时所调用的方法的实际返回值,与所述待测试代码所调用的方法所需参数类型的关联关系;根据所述关联关系,以及所述待测试代码所调用的方法所需参数的类型,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型。Optionally, the determination module 402 is specifically configured to obtain the code of the method called by the code to be tested; and determine the code of the method called by the code to be tested according to the obtained code of the method called by the code to be tested. The actual return value of the method called during code testing is related to the parameter type required by the method called by the code to be tested; according to the related relationship, and the parameters required by the method called by the code to be tested. Type, determine the actual return value type corresponding to the method called when testing the code to be tested.
可选地,所述测试模块403具体用于根据所述关联关系,创建方法数据结构MethodDescripter结构,所述MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数;根据所述MethodDescripter结构,以及所述实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, the test module 403 is specifically configured to create a method data structure MethodDescripter structure according to the association relationship. The MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, and the method of calling the method. name, the class name of the calling method, and the incoming parameters of the calling method; according to the MethodDescripter structure and the actual return value type, a mock method corresponding to the method called by the code to be tested is generated; through the mock method , to obtain the test results for the code to be tested.
可选地,所述确定模块402具体用于,通过对所述待测试代码对应的字节码的分析,确定从所述待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标方法;在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型。Optionally, the determination module 402 is specifically configured to determine, from the methods called by the code to be tested, that the method type is a forced type conversion method by analyzing the bytecode corresponding to the code to be tested. , as the target method; when testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested according to the target method.
可选地,所述装置还包括:分析模块404,用于将所述目标方法在所述待测试代码中对应的代码行号,与所述目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存;所述确定模块402具体用于,若在所述待测试代码的测试过程中,确 定所述待测试代码所调用的方法为强制类型转换的方法的代码行号,作为目标行号;根据所述目标行号,从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型。Optionally, the device further includes: an analysis module 404, configured to compare the code line number corresponding to the target method in the code to be tested with the actual return value of the target method after forced type conversion. The corresponding relationship between the value types is saved; the determination module 402 is specifically used to determine if during the testing process of the code to be tested, The method called by the code to be tested is determined to be the code line number of the forced type conversion method as the target line number; according to the target line number, the code called by the code to be tested is queried from the pre-saved correspondence. The actual return value type corresponding to the method.
可选地,所述测试模块403具体用于,根据从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。Optionally, the testing module 403 is specifically configured to generate a response value corresponding to the method called by the code to be tested based on querying the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence relationship. The mock method; through the mock method, the test results for the code to be tested are obtained.
图5为本说明书提供的一种测试用例生成的装置的示意图,包括:用例获取模块501,用于获取各测试用例;用例测试模块502,用于针对每个测试用例,通过上述代码测试的方法,对设定代码进行测试,得到该测试用例对应的测试结果;判断模块503,用于根据每个测试用例对应的测试结果,确定是否满足预设的测试条件;生成模块504,用于在确定不满足所述测试条件时,重新生成各测试用例,并根据重新生成的各测试用例,对所述设定代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。Figure 5 is a schematic diagram of a test case generation device provided in this specification, including: a use case acquisition module 501, used to obtain each test case; a use case testing module 502, used for each test case, through the above code testing method , test the set code to obtain the test results corresponding to the test case; the judgment module 503 is used to determine whether the preset test conditions are met based on the test results corresponding to each test case; the generation module 504 is used to determine whether When the test conditions are not met, each test case is regenerated, and the set code is tested according to each regenerated test case until the test condition is met, and the test generated when the test condition is met is obtained. Example.
可选地,所述生成模块504具体用于,从各测试用例中选取至少部分测试用例进行两两组合,得到各测试用例组合;针对每个测试用例组合,将该测试用例组合中包含的两个测试用例内的同类型参数进行加权,得到重新生成的测试用例。Optionally, the generation module 504 is specifically configured to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, combine the two test case combinations included in the test case combination. Parameters of the same type in each test case are weighted to obtain a regenerated test case.
可选地,所述生成模块504具体用于,从各测试用例中选取至少部分测试用例;针对选取出的每个测试用例,将该测试用例中包含的至少部分参数进行数值调整,和/或将该测试用例中包含的待测试代码进行调整,得到重新生成的测试用例。Optionally, the generation module 504 is specifically configured to select at least some test cases from each test case; for each selected test case, numerically adjust at least some parameters included in the test case, and/or Adjust the code to be tested contained in the test case to obtain a regenerated test case.
本说明书还提供了一种计算机可读存储介质,该存储介质存储有计算机程序,计算机程序可用于执行上述图1提供的一种代码测试、测试用例生成的方法。This specification also provides a computer-readable storage medium that stores a computer program. The computer program can be used to execute the method of code testing and test case generation provided in Figure 1 above.
本说明书还提供了图6所示的一种对应于图1的电子设备的示意结构图。如图6所述,在硬件层面,该电子设备包括处理器、内部总线、网络接口、内存以及非易失性存储器,当然还可能包括其他业务所需要的硬件。处理器从非易失性存储器中读取对应的计算机程序到内存中然后运行,以实现上述图1所述的代码测试、测试用例生成的方法。当然,除了软件实现方式之外,本说明书并不排除其他实现方式,比如逻辑器件抑或软硬件结合的方式等等,也就是说以下处理流程的执行主体并不限定于各个逻辑单元,也可以是硬件或逻辑器件。This specification also provides a schematic structural diagram of the electronic device shown in FIG. 6 corresponding to FIG. 1 . As shown in Figure 6, at the hardware level, the electronic device includes a processor, internal bus, network interface, memory and non-volatile memory, and of course may also include other hardware required for business. The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to implement the method of code testing and test case generation described in Figure 1 above. Of course, in addition to software implementation, this specification does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logical unit, and may also be hardware or logic device.
在20世纪90年代,对于一个技术的改进可以很明显地区分是硬件上的改进(例如, 对二极管、晶体管、开关等电路结构的改进)还是软件上的改进(对于方法流程的改进)。然而,随着技术的发展,当今的很多方法流程的改进已经可以视为硬件电路结构的直接改进。设计人员几乎都通过将改进的方法流程编程到硬件电路中来得到相应的硬件电路结构。因此,不能说一个方法流程的改进就不能用硬件实体模块来实现。例如,可编程逻辑器件(Programmable Logic Device,PLD)(例如现场可编程门阵列(Field Programmable Gate Array,FPGA))就是这样一种集成电路,其逻辑功能由用户对器件编程来确定。由设计人员自行编程来把一个数字系统“集成”在一片PLD上,而不需要请芯片制造厂商来设计和制作专用的集成电路芯片。而且,如今,取代手工地制作集成电路芯片,这种编程也多半改用“逻辑编译器(logic compiler)”软件来实现,它与程序开发撰写时所用的软件编译器相类似,而要编译之前的原始代码也得用特定的编程语言来撰写,此称之为硬件描述语言(Hardware Description Language,HDL),而HDL也并非仅有一种,而是有许多种,如ABEL(Advanced Boolean Expression Language)、AHDL(Altera Hardware Description Language)、Confluence、CUPL(Cornell University Programming Language)、HDCal、JHDL(Java Hardware Description Language)、Lava、Lola、MyHDL、PALASM、RHDL(Ruby Hardware Description Language)等,目前最普遍使用的是VHDL(Very-High-Speed Integrated Circuit Hardware Description Language)与Verilog。本领域技术人员也应该清楚,只需要将方法流程用上述几种硬件描述语言稍作逻辑编程并编程到集成电路中,就可以很容易得到实现该逻辑方法流程的硬件电路。In the 1990s, improvements in a technology could clearly be distinguished from improvements in hardware (e.g., Improvements in circuit structures such as diodes, transistors, switches, etc.) or software improvements (improvements in method processes). However, with the development of technology, many improvements in today's method processes can be regarded as direct improvements in hardware circuit structures. Designers almost always obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement of a method flow cannot be implemented using hardware entity modules. For example, a Programmable Logic Device (PLD) (such as a Field Programmable Gate Array (FPGA)) is such an integrated circuit whose logic functions are determined by the user programming the device. Designers can program themselves to "integrate" a digital system on a PLD, instead of asking chip manufacturers to design and produce dedicated integrated circuit chips. Moreover, nowadays, instead of manually making integrated circuit chips, this kind of programming is mostly implemented using "logic compiler" software, which is similar to the software compiler used in program development and writing. Before compiling, The original code must also be written in a specific programming language, which is called Hardware Description Language (HDL). There is not only one type of HDL, but many types, such as ABEL (Advanced Boolean Expression Language) , AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc., are currently the most commonly used The most popular ones are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also know that by simply logically programming the method flow using the above-mentioned hardware description languages and programming it into the integrated circuit, the hardware circuit that implements the logical method flow can be easily obtained.
控制器可以按任何适当的方式实现,例如,控制器可以采取例如微处理器或处理器以及存储可由该(微)处理器执行的计算机可读程序代码(例如软件或固件)的计算机可读介质、逻辑门、开关、专用集成电路(Application Specific Integrated Circuit,ASIC)、可编程逻辑控制器和嵌入微控制器的形式,控制器的例子包括但不限于以下微控制器:ARC 625D、Atmel AT91SAM、Microchip PIC18F26K20以及Silicone Labs C8051F320,存储器控制器还可以被实现为存储器的控制逻辑的一部分。本领域技术人员也知道,除了以纯计算机可读程序代码方式实现控制器以外,完全可以通过将方法步骤进行逻辑编程来使得控制器以逻辑门、开关、专用集成电路、可编程逻辑控制器和嵌入微控制器等的形式来实现相同功能。因此这种控制器可以被认为是一种硬件部件,而对其内包括的用于实现各种功能的装置也可以视为硬件部件内的结构。或者甚至,可以将用于实现各种功能的装置视为既可以是实现方法的软件模块又可以是硬件部件内的结构。The controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, a microprocessor or processor and a computer readable medium storing computer readable program code (eg, software or firmware) executable by the (micro)processor. , logic gates, switches, Application Specific Integrated Circuit (ASIC), programmable logic controllers and embedded microcontrollers. Examples of controllers include but are not limited to the following microcontrollers: ARC 625D, Atmel AT91SAM, For Microchip PIC18F26K20 and Silicone Labs C8051F320, the memory controller can also be implemented as part of the memory's control logic. Those skilled in the art also know that in addition to implementing the controller in the form of pure computer-readable program code, the controller can be completely programmed with logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded logic by logically programming the method steps. Microcontroller, etc. to achieve the same function. Therefore, this controller can be considered as a hardware component, and the devices included therein for implementing various functions can also be considered as structures within the hardware component. Or even, the means for implementing various functions can be considered as structures within hardware components as well as software modules implementing the methods.
上述实施例阐明的系统、装置、模块或单元,具体可以由计算机芯片或实体实现,或者由具有某种功能的产品来实现。一种典型的实现设备为计算机。具体的,计算机例 如可以为个人计算机、膝上型计算机、蜂窝电话、相机电话、智能电话、个人数字助理、媒体播放器、导航设备、电子邮件设备、游戏控制台、平板计算机、可穿戴设备或者这些设备中的任何设备的组合。The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, computer examples For example, it may be a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any of these devices. Any combination of equipment.
为了描述的方便,描述以上装置时以功能分为各种单元分别描述。当然,在实施本说明书时可以把各单元的功能在同一个或多个软件和/或硬件中实现。For the convenience of description, when describing the above device, the functions are divided into various units and described separately. Of course, when implementing this specification, the functions of each unit can be implemented in the same or multiple software and/or hardware.
本领域内的技术人员应明白,本说明书的实施例可提供为方法、系统、或计算机程序产品。因此,本说明书可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本说明书可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art will understand that embodiments of the present specification may be provided as methods, systems, or computer program products. Thus, the present description may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment that combines software and hardware aspects. Furthermore, the present description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk memory, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
本说明书是参照根据本说明书实施例的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The specification is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the specification. It will be understood that each process and/or block in the flowchart illustrations and/or block diagrams, and combinations of processes and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing device to produce a machine, such that the instructions executed by the processor of the computer or other programmable data processing device produce a use A device for realizing the functions specified in one process or multiple processes of the flowchart and/or one block or multiple blocks of the block diagram.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory that causes a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including the instruction means, the instructions The device implements the functions specified in a process or processes of the flowchart and/or a block or blocks of the block diagram.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions may also be loaded onto a computer or other programmable data processing device, causing a series of operating steps to be performed on the computer or other programmable device to produce computer-implemented processing, thereby executing on the computer or other programmable device. Instructions provide steps for implementing the functions specified in a process or processes of a flowchart diagram and/or a block or blocks of a block diagram.
在一个典型的配置中,计算设备包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。内存是计算机可读介 质的示例。Memory may include non-permanent storage in computer-readable media, random access memory (RAM), and/or non-volatile memory in the form of read-only memory (ROM) or flash memory (flash RAM). Memory is a computer-readable medium Qualitative examples.
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带,磁带磁磁盘存储或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括暂存电脑可读媒体(transitory media),如调制的数据信号和载波。Computer-readable media includes both persistent and non-volatile, removable and non-removable media that can be implemented by any method or technology for storage of information. Information may be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), and read-only memory. (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, Magnetic tape cassettes, tape magnetic disk storage or other magnetic storage devices or any other non-transmission medium can be used to store information that can be accessed by a computing device. As defined in this article, computer-readable media does not include transitory media, such as modulated data signals and carrier waves.
还需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、商品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、商品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、商品或者设备中还存在另外的相同要素。It should also be noted that the terms "comprises," "comprises," or any other variation thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements not only includes those elements, but also includes Other elements are not expressly listed or are inherent to the process, method, article or equipment. Without further limitation, an element defined by the statement "comprises a..." does not exclude the presence of additional identical elements in a process, method, article, or device that includes the stated element.
本领域技术人员应明白,本说明书的实施例可提供为方法、系统或计算机程序产品。因此,本说明书可采用完全硬件实施例、完全软件实施例或结合软件和硬件方面的实施例的形式。而且,本说明书可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art will appreciate that embodiments of the present specification may be provided as methods, systems, or computer program products. Thus, the present description may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment that combines software and hardware aspects. Furthermore, the present description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk memory, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
本说明书可以在由计算机执行的计算机可执行指令的一般上下文中描述,例如程序模块。一般地,程序模块包括执行特定任务或实现特定抽象数据类型的例程、程序、对象、组件、数据结构等等。也可以在分布式计算环境中实践本说明书,在这些分布式计算环境中,由通过通信网络而被连接的远程处理设备来执行任务。在分布式计算环境中,程序模块可以位于包括存储设备在内的本地和远程计算机存储介质中。This specification may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present description may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through communications networks. In a distributed computing environment, program modules may be located in both local and remote computer storage media including storage devices.
本说明书中的各个实施例均采用递进的方式描述,各个实施例之间相同相似的部分互相参见即可,每个实施例重点说明的都是与其他实施例的不同之处。尤其,对于系统实施例而言,由于其基本相似于方法实施例,所以描述的比较简单,相关之处参见方法实施例的部分说明即可。 Each embodiment in this specification is described in a progressive manner. The same and similar parts between the various embodiments can be referred to each other. Each embodiment focuses on its differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple. For relevant details, please refer to the partial description of the method embodiment.
以上所述仅为本说明书的实施例而已,并不用于限制本说明书。对于本领域技术人员来说,本说明书可以有各种更改和变化。凡在本说明书的精神和原理之内所作的任何修改、等同替换、改进等,均应包含在本说明书的权利要求范围之内。 The above descriptions are only examples of this specification and are not intended to limit this specification. Various modifications and variations may occur to those skilled in the art. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of this specification shall be included in the scope of the claims of this specification.

Claims (20)

  1. 一种代码测试的方法,包括:A method of code testing, including:
    获取待测试代码;Get the code to be tested;
    根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种;According to the type of parameters required for the method called by the code to be tested, and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, determine the method called when the code to be tested is tested. The actual return value type corresponding to the method. The method type of the method called when testing the code to be tested includes: at least one of a generic method and forced type conversion;
    根据所述实际返回值类型,得到针对所述待测试代码的测试结果。According to the actual return value type, the test result for the code to be tested is obtained.
  2. 如权利要求1所述的方法,根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,具体包括:The method of claim 1, determining the location of the method according to the type of parameters required by the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested. Describe the actual return value type corresponding to the method called when testing the code to be tested, including:
    获取所述待测试代码所调用的方法的代码;Obtain the code of the method called by the code to be tested;
    根据获取到的所述待测试代码所调用的方法的代码,确定在所述待测试代码测试时所调用的方法的实际返回值类型,与所述待测试代码所调用的方法所需参数类型的关联关系;According to the obtained code of the method called by the code under test, determine the actual return value type of the method called when the code under test is tested, and the type of parameters required by the method called by the code under test. connection relation;
    根据所述关联关系,以及所述待测试代码所调用的方法所需参数的类型,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型。According to the association relationship and the types of parameters required by the method called by the code to be tested, the actual return value type corresponding to the method called when the code to be tested is tested is determined.
  3. 如权利要求2所述的方法,根据所述实际返回值类型,得到针对所述待测试代码的测试结果,具体包括:The method of claim 2, obtaining test results for the code to be tested according to the actual return value type, specifically including:
    根据所述关联关系,创建方法数据结构MethodDescripter结构,所述MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数;According to the association relationship, a method data structure MethodDescripter structure is created. The MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, and the name of the calling method. Pass in parameters;
    根据所述MethodDescripter结构,以及所述实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;According to the MethodDescripter structure and the actual return value type, generate a mock method corresponding to the method called by the code to be tested;
    通过所述mock方法,得到针对所述待测试代码的测试结果。Through the mock method, test results for the code to be tested are obtained.
  4. 如权利要求1所述的方法,根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,具体包括:The method of claim 1, determining the location of the method according to the type of parameters required by the method called by the code to be tested and/or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested. Describe the actual return value type corresponding to the method called when testing the code to be tested, including:
    通过对所述待测试代码对应的字节码的分析,确定从所述待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标方法;By analyzing the bytecode corresponding to the code to be tested, it is determined that the method type is a forced type conversion method from the methods called by the code to be tested, and is used as the target method;
    在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所 调用的方法对应的实际返回值类型。When testing the code to be tested, determine where the code to be tested is based on the target method. The actual return value type corresponding to the called method.
  5. 如权利要求4所述的方法,在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型之前,所述方法还包括:The method of claim 4, when testing the code to be tested, determining based on the target method that before the actual return value type corresponding to the method called by the code to be tested, the method also include:
    将所述目标方法在所述待测试代码中对应的代码行号,与所述目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存;Save the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion;
    在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型,具体包括:When testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested based on the target method, specifically including:
    在所述待测试代码的测试过程中,确定所述待测试代码所调用的方法为强制类型转换的方法的代码行号,作为目标行号;During the testing process of the code to be tested, the code line number of the method called by the code to be tested is determined to be a method of forced type conversion, and is used as the target line number;
    根据所述目标行号,从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型。According to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved correspondence relationship.
  6. 如权利要求5所述的方法,根据所述实际返回值类型,得到针对所述待测试代码的测试结果,具体包括:The method of claim 5, obtaining test results for the code to be tested according to the actual return value type, specifically including:
    根据从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;Generate a mock method corresponding to the method called by the code to be tested based on querying the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence;
    通过所述mock方法,得到针对所述待测试代码的测试结果。Through the mock method, test results for the code to be tested are obtained.
  7. 一种测试用例生成的方法,包括:A method of test case generation, including:
    获取各测试用例;Get each test case;
    针对每个测试用例,通过上述权利要求1~6任一项所述的方法,对设定代码进行测试,得到该测试用例对应的测试结果;For each test case, test the setting code through the method described in any one of the above claims 1 to 6, and obtain the test results corresponding to the test case;
    根据每个测试用例对应的测试结果,确定是否满足预设的测试条件;Based on the test results corresponding to each test case, determine whether the preset test conditions are met;
    若确定不满足所述测试条件,重新生成各测试用例,并根据重新生成的各测试用例,对所述设定代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。If it is determined that the test conditions are not met, each test case is regenerated, and the set code is tested according to each regenerated test case until the test conditions are met, and the generated code generated when the test conditions are met is obtained. Test cases.
  8. 如权利要求7所述的方法,重新生成各测试用例,具体包括:According to the method of claim 7, regenerating each test case specifically includes:
    从各测试用例中选取至少部分测试用例进行两两组合,得到各测试用例组合;Select at least some test cases from each test case and combine them in pairs to obtain each test case combination;
    针对每个测试用例组合,将该测试用例组合中包含的两个测试用例内的同类型参数进行加权,得到重新生成的测试用例。For each test case combination, the parameters of the same type in the two test cases included in the test case combination are weighted to obtain a regenerated test case.
  9. 如权利要求7所述的方法,重新生成各测试用例,具体包括:According to the method of claim 7, regenerating each test case specifically includes:
    从各测试用例中选取至少部分测试用例; Select at least some test cases from each test case;
    针对选取出的每个测试用例,将该测试用例中包含的至少部分参数进行数值调整,和/或将该测试用例中包含的待测试代码进行调整,得到重新生成的测试用例。For each selected test case, numerically adjust at least some of the parameters included in the test case, and/or adjust the code to be tested included in the test case to obtain a regenerated test case.
  10. 一种代码测试的装置,包括:A device for code testing, including:
    获取模块,用于获取待测试代码;Obtain module, used to obtain the code to be tested;
    确定模块,用于根据所述待测试代码所调用的方法所需参数的类型,和/或对所述待测试代码对应的字节码进行分析后得到的分析结果,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型,所述待测试代码测试时所调用的方法的方法类型包括:泛型方法以及强制类型转换中的至少一种;Determining module, configured to determine the location of the code under test based on the type of parameters required by the method called by the code under test and/or the analysis results obtained after analyzing the bytecode corresponding to the code under test. The actual return value type corresponding to the method called during testing. The method type of the method called when testing the code to be tested includes: at least one of a generic method and forced type conversion;
    测试模块,用于根据所述实际返回值类型,得到针对所述待测试代码的测试结果。A testing module, configured to obtain test results for the code to be tested based on the actual return value type.
  11. 如权利要求10所述的装置,所述确定模块具体用于,获取所述待测试代码所调用的方法的代码;根据获取到的所述待测试代码所调用的方法的代码,确定所述在所述待测试代码测试时所调用的方法的实际返回值,与所述待测试代码所调用的方法所需参数类型的关联关系;根据所述关联关系,以及所述待测试代码所调用的方法所需参数的类型,确定在所述待测试代码测试时所调用的方法对应的实际返回值类型。The device according to claim 10, the determining module is specifically configured to obtain the code of the method called by the code to be tested; and determine the code of the method called by the code to be tested based on the obtained code. The actual return value of the method called when the code to be tested is tested, and the relationship between the parameter types required by the method called by the code to be tested; according to the relationship, and the method called by the code to be tested The type of required parameters determines the actual return value type corresponding to the method called when testing the code to be tested.
  12. 如权利要求11所述的装置,所述测试模块具体用于,根据所述关联关系,创建方法数据结构MethodDescripter结构,所述MethodDescripter结构包括:调用方法的实际返回值类型、MethodDescripter结构被调用的次数、调用方法的方法名、调用方法的所属类名、调用方法的传入参数;根据所述MethodDescripter结构,以及所述实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。The device according to claim 11, the test module is specifically configured to create a method data structure MethodDescripter structure according to the association relationship, the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called , the method name of the calling method, the class name of the calling method, and the incoming parameters of the calling method; according to the MethodDescripter structure and the actual return value type, generate a mock method corresponding to the method called by the code to be tested; Through the mock method, test results for the code to be tested are obtained.
  13. 如权利要求10所述的装置,所述确定模块具体用于,通过对所述待测试代码对应的字节码的分析,确定从所述待测试代码所调用的方法中确定出方法类型为强制类型转换的方法,作为目标方法;在对所述待测试代码进行测试时,根据所述目标方法,确定出在所述待测试代码所调用的方法对应的实际返回值类型。The device according to claim 10, the determination module is specifically configured to determine that the method type from the method called by the code to be tested is mandatory by analyzing the bytecode corresponding to the code to be tested. The method of type conversion serves as the target method; when testing the code to be tested, the actual return value type corresponding to the method called by the code to be tested is determined according to the target method.
  14. 如权利要求13所述的装置,所述装置还包括:The device of claim 13, further comprising:
    分析模块,用于将所述目标方法在所述待测试代码中对应的代码行号,与所述目标方法的返回值经强制类型转换后的实际返回值类型的对应关系进行保存;An analysis module, configured to save the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion;
    所述确定模块具体用于,在所述待测试代码的测试过程中,确定所述待测试代码所调用的方法为强制类型转换的方法的代码行号,作为目标行号;根据所述目标行号,从预先保存的对应关系中查询出所述待测试代码所调用的方法对应的实际返回值类型。The determination module is specifically configured to determine, during the testing process of the code to be tested, the code line number in which the method called by the code to be tested is a forced type conversion method, as the target line number; according to the target line number number, and query the actual return value type corresponding to the method called by the code to be tested from the pre-saved correspondence relationship.
  15. 如权利要求14所述的装置,所述测试模块具体用于,根据从预先保存的对应 关系中查询出所述待测试代码所调用的方法对应的实际返回值类型,生成所述待测试代码所调用的方法对应的mock方法;通过所述mock方法,得到针对所述待测试代码的测试结果。The device according to claim 14, the test module is specifically configured to: according to the pre-saved corresponding The actual return value type corresponding to the method called by the code to be tested is queried in the relationship, and a mock method corresponding to the method called by the code to be tested is generated; through the mock method, a test for the code to be tested is obtained result.
  16. 一种测试用例生成的装置,包括:A device for generating test cases, including:
    用例获取模块,用于获取各测试用例;The use case acquisition module is used to obtain each test case;
    用例测试模块,用于针对每个测试用例,通过上述权利要求1~6任一项所述的方法,对设定代码进行测试,得到该测试用例对应的测试结果;The use case testing module is used to test the setting code for each test case through the method described in any one of the above claims 1 to 6, and obtain the test results corresponding to the test case;
    判断模块,用于根据每个测试用例对应的测试结果,确定是否满足预设的测试条件;The judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case;
    生成模块,用于在确定不满足所述测试条件时,重新生成各测试用例,并根据重新生成的各测试用例,对所述设定代码进行测试,直到满足所述测试条件为止,得到满足所述测试条件时生成的测试用例。A generation module, configured to regenerate each test case when it is determined that the test conditions are not met, and test the set code based on the regenerated test cases until the test conditions are met, and all test cases are obtained. Test cases generated when describing test conditions.
  17. 如权利要求16所述的装置,所述生成模块具体用于,从各测试用例中选取至少部分测试用例进行两两组合,得到各测试用例组合;针对每个测试用例组合,将该测试用例组合中包含的两个测试用例内的同类型参数进行加权,得到重新生成的测试用例。The device according to claim 16, the generating module is specifically configured to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, combine the test case combinations The parameters of the same type in the two test cases contained in are weighted to obtain a regenerated test case.
  18. 如权利要求16所述的装置,所述生成模块具体用于,从各测试用例中选取至少部分测试用例;针对选取出的每个测试用例,将该测试用例中包含的至少部分参数进行数值调整,和/或将该测试用例中包含的待测试代码进行调整,得到重新生成的测试用例。The device according to claim 16, the generating module is specifically configured to select at least some test cases from each test case; for each selected test case, perform numerical adjustment on at least some parameters contained in the test case. , and/or adjust the code to be tested contained in the test case to obtain a regenerated test case.
  19. 一种计算机可读存储介质,所述存储介质存储有计算机程序,所述计算机程序被处理器执行时实现上述权利要求1~9任一项所述的方法。A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the method described in any one of claims 1 to 9 is implemented.
  20. 一种电子设备,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现上述权利要求1~9任一项所述的方法。 An electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the method of any one of claims 1 to 9 is implemented.
PCT/CN2023/092939 2022-05-10 2023-05-09 Code test method and apparatus, and test case generation method and apparatus WO2023217118A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202210508851.1 2022-05-10
CN202210508851.1A CN114840427A (en) 2022-05-10 2022-05-10 Code testing and test case generating method and device

Publications (1)

Publication Number Publication Date
WO2023217118A1 true WO2023217118A1 (en) 2023-11-16

Family

ID=82569467

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2023/092939 WO2023217118A1 (en) 2022-05-10 2023-05-09 Code test method and apparatus, and test case generation method and apparatus

Country Status (2)

Country Link
CN (1) CN114840427A (en)
WO (1) WO2023217118A1 (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114840427A (en) * 2022-05-10 2022-08-02 支付宝(杭州)信息技术有限公司 Code testing and test case generating method and device

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107133174A (en) * 2017-05-04 2017-09-05 浙江路港互通信息技术有限公司 Test case code automatically generating device and method
CN113778849A (en) * 2021-03-15 2021-12-10 北京沃东天骏信息技术有限公司 Method, apparatus, device and storage medium for testing code
US20210390038A1 (en) * 2020-06-10 2021-12-16 Sap Se Automatic evaluation of test code quality
CN114840427A (en) * 2022-05-10 2022-08-02 支付宝(杭州)信息技术有限公司 Code testing and test case generating method and device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107133174A (en) * 2017-05-04 2017-09-05 浙江路港互通信息技术有限公司 Test case code automatically generating device and method
US20210390038A1 (en) * 2020-06-10 2021-12-16 Sap Se Automatic evaluation of test code quality
CN113778849A (en) * 2021-03-15 2021-12-10 北京沃东天骏信息技术有限公司 Method, apparatus, device and storage medium for testing code
CN114840427A (en) * 2022-05-10 2022-08-02 支付宝(杭州)信息技术有限公司 Code testing and test case generating method and device

Also Published As

Publication number Publication date
CN114840427A (en) 2022-08-02

Similar Documents

Publication Publication Date Title
US20130111267A1 (en) Optimizing regression testing based on code coverage analysis
CN107229559B (en) Detection method and device for testing integrity of service system
CN110389842B (en) Dynamic resource allocation method, device, storage medium and equipment
WO2023217118A1 (en) Code test method and apparatus, and test case generation method and apparatus
US11436188B2 (en) Resource optimization and update method, server, and device
TWI684916B (en) Function selection method and server
WO2020237508A1 (en) Assertion validation code binding method and apparatus
CN113535721A (en) Data writing method and device
CN110635962B (en) Abnormity analysis method and device for distributed system
CN110879781A (en) Program debugging method and device, electronic equipment and computer readable storage medium
CN108304313B (en) Device, client and method for data testing
CN114328250A (en) Automatic self-checking method, medium and device for software system
CN116402165B (en) Operator detection method and device, storage medium and electronic equipment
US20230367936A1 (en) Verification method, electronic device and storage medium
US20230367701A1 (en) Code testing method and apparatus
CN115656788B (en) Chip testing system, method, equipment and storage medium
CN110865931B (en) Simulation method, simulation device, electronic equipment and storage medium
CN111078435A (en) Service processing method and device and electronic equipment
CN112241362A (en) Test method, test device, server and storage medium
US9489284B2 (en) Debugging method and computer program product
CN115033434A (en) Kernel performance theoretical value calculation method and device and storage medium
US10761973B2 (en) Code coverage thresholds for code segments based on usage frequency and change frequency
CN108733564B (en) Browser performance testing method, device and equipment
CN110032624B (en) Sample screening method and device
CN110008112B (en) Model training method and device, service testing method and device

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 23802888

Country of ref document: EP

Kind code of ref document: A1