Optimizeit Code Coverage Overview

Source: Internet
Author: User
Tags filter command line execution requires reset return window root directory

Optimizeit Code Coverage Overview



This article introduces some of the main features of Optimizeit Code coverage to give you a brief understanding of it. If you want to know more, check the Optimizeit Code coverage user's manual. You can also view all the usage documents by clicking Menu Info|help from Optimizeit Code coverage.

Please feel free to contact Borland Technical support for any problems in use.

Test a Java program



The Optimizeit Code coverage collects program coverage information from the virtual machine where the program is running.

To run a Java program first, you need to install a Java virtual machine. Optimizeit Code Coverage The default installation configuration JDK 1.4 or 1.4.1. If you want to use another virtual machine, you can see how the Optimizeit Code coverage user's manual adds a new virtual machine section.

Start the application



Optimizeit Profiler can be used to test any type of Java program, including standard applications, application applets, Servlets, JSP,EJB, and so on.

The demo in this article is a test object with a bank account program that is contained in the Quicktour directory of code coverage. Banking.java is the main class, which is used to create and process different types of transactions that are required by the user. There are generally four bank accounts per User:

§ Settlement accounts (C) (Checking account)



§ Savings Accounts (P) (primary savings account)



§ Student loan return account (S) (Student loan repayment savings accounts)



§ Auto Loan return account (L) (Car loan repayment accounts)



Bank accounts have the following common attributes:

§ Account number



§ Holder's name (owner ' s name)



§ balance (current balance)



Accounting accounts have no interest, no minimum balance, no interest. For a savings account, interest occurs when the money is deposited, but requires the balance of the account to be greater than 500 dollars. Savings accounts are also used to prevent overdrafts in settlement accounts. If the amount extracted from the accounting account is greater than the current balance, it is automatically transferred from the basic savings account to the accounting account. The basic savings account requires no less than 15 dollars per transaction.

Suppose we have used juint to test the Banking.java:

§BANKACCOUNTTEST1 provides a test to implement transactions through the banking class. Running this test through code coverage, you can see the coverage of Banking.java classes through BANKACCOUNTTEST1 tests. The goal of our testing is to cover the banking class 100%. The Code coverage provides the class name and method information for the Banking.java class, so you can find out how much of it is not being BankAccountTest1 executed from the source code.



§bankaccounttest2 is used to prove that 100% of the banking class can be overwritten by code coverage.



The following tests are run through JUnit testrunner. You can download the latest version of JUnit from www.junit.org. The entire testing process is done entirely through code coverage.


Download JUnit and extract to the local disk.

Open Optimizeit Code Coverage.

If this is the first time, Optimizeit will automatically open the Edit Settings window, if you have already opened the Optimizeit, you can select New from the menu file, and the window will also pop up.

In the Program Type box, select Application.

Enter Junit.swingui.TestRunner in the program main class or Jar file box.

Click the Browse button to select the working directory for your program, select the installation directory for JUnit.

The Code coverage automatically returns to the Edit Settings window. Click Change below the class path.

In class Path Chooser, open to the JUnit installation root directory and select the Junit.jar file. Click the Down button to add Junit.jar to the classpath.

Then open to the Optimizeit Code Coverage quicktour directory and select the \doc\code_coverage\quicktour directory. Click the Down button to add it to the classpath.

Click OK to return to the Edit Settings window.

Click the Change button under the Source Code path window.

In the Source Path Chooser window, open to the Optimizeit code Coverage quicktour directory and select the \doc\code_coverage\quicktour directory, and then click the Down button to add to the source code path.

Click OK to return to the Edit Settings window. This allows you to see the source code without having to do any other configuration during the test. The editing Settings window returned by Optimizeit is as follows:



You can select the Filter tab in the Filter window to select the class to analyze coverage. The default setting is to exclude all Java core code classes. and verify that all of the JUnit classes have been excluded. Junit.* should be included in the default exclusion list.

Click the Virtual Machines tab to select a different JDK.

Click the Start Now button.

The Edit Settings window closes automatically, and the Optimizeit Code coverage automatically loads the JUnit testrunner application.


View Class Code Coverage



This section describes the different ways to view code coverage for class tests.


In JUnit, reload Classes Every Run is unchecked.

Click the ellipsis button to the right of the test class name input box. In the test selector window that pops up, select BankAccountTest1, and then click OK.





Click the Run button to perform the BankAccountTest1. You will see the green scroll bar underneath the button to prove that all the tests have passed.

After the program runs, Optimizeit code coverage displays all the loaded classes in the virtual machine in real time and displays the coverage of their respective code.





The yellow bars represent the coverage of each class. There are two ways to view this information. For example, click the Detect button on the toolbar to open the detection window.

Select Show Line Coverage. This will add two columns of content to the table. As shown below:





The missed lines column represents the number of rows in the selected class that have not been executed and the total number of the classes. For example, the banking class in the figure has 217 lines of code, and 20 lines of code are not executed, and the code coverage is 90.78%.

Click the Detect button to select Show All Accessible Classes. This will display a class in the window that is not loaded into the virtual machine. You can see a new row in the window, classname for Italic BankAccountTest2, and the Code overlay column displays "CLASS not LOADED."

Click the tag button in the toolbar to reset the diff column. The Missed Lines diff column is also reset (the number of the missed Lines diff column is reset to the total number of lines in the code, which means that none of the rows are overwritten). Using tags, you can understand which class is used when you perform an operation in a program. You can click the tag button to mark it before you do something, and then proceed with it, which automatically generates a new code coverage record to compare to the marked record.


View coverage for a method



Our goal is also to reach the code that covers the banking class 100%. From the class coverage window we can see that even though JUnit's test results show 100% success but still only cover banking class 90.78% code. To make the analysis clearer, we use methods to overwrite the view to view the code coverage:


In the Class overlay table, select the banking class, and then click the Show Method Coverage button on the toolbar or double-click the row directly. The Optimizeit Code coverage opens to the Method coverage window:






The top of the window shows all the list of methods in the banking class and their execution coverage, the number of calls and the number of lines of code that were not executed, the total number of rows. The source code for the class is shown below the window.


Click the Parsearguments method row in the method list. The Source code window automatically scrolls to the line of code for the method, and you can see that the call column in some rows has a value of 0, indicating that the row has never been invoked. Rows with no numbers in the call column, such as comment lines, do not generate bytes in the class file.

Click the Detect button on the toolbar to open the detection window and select Show method signatures. This allows you to see the characteristics of all the classes in the banking class.

In the Test window, select Show difference. Optimizeit will add new columns:





The Invocation # diff column indicates the number of times the method has been executed since the last token.

More out of the missed Lines diff column.

The calls column in the source box also has a column (Diff) that indicates the number of times the line code was executed after it was marked.

Shows that the parsearguments coverage is 95.45%. If you can find a row with the calls value of 0 in the method, this corresponds to banking processing an invalid transaction.

Another one that did not reach 100% coverage was the transfer method, which reached only 95.45%. There is no code in the code list that appears with a yellow bottom as the code that is not executed. The corresponding business operation is to attempt to withdraw funds from the student loan that are larger than the current balance.

In the method overlay table, find the Printdescription method, which shows that the method is not actually invoked.

Click the Detect button and select the Highlight Since Mark option. This eliminates the yellow end of all the code that has been executed, since the code in the banking class has not been executed since the last time the tag was made.

Click the Detect button and select the Real time option. This will optimizeit even if the coverage information is displayed. Select the method Printdescription method in the method overlay table. The code window will automatically scroll to the method. All rows in this method are called 0 times, because the method has not been invoked so far.

Select BankAccountTest2 in JUnit and click Run.

In Optimizeit Code coverage you will see that the number of calls to the various methods is changing. For example, the number of calls to the Parsearguments method has become more frequent.

All lines of the Printdescription method in the code window are displayed in yellow. We can see from the table that the coverage of the Parsearguments method and the transfer method is up to 100%. Select the transfer method and uncheck the highlight Since Mark entry from the inspector. The code that does not have a yellow background in the previous test in the Source code table is now displayed with a yellow background:





Click the Class Overlay button on the toolbar or select file| from the menu Class to overwrite the view. The coverage of the banking class in the class overlay view has reached 100%.


View Test Results



Optimizeit Code coverage provides several different ways to view test results, and you can output data to an HTML file from the current class overlay view or method overlay. You can also output both types of information to an HTML file.



1. In the process of running the program, select the banking class and then click the Show Methods Coverage button to go to the method overlay window.


Click menu file| Export, open the Output window.

Select method and Source Code Coverage in the Export drop-down box.

Click the browse button in the FileName box to select the location where you want to store the report.

Enter the name of the report at the end of the FileName box.

Enter the comment information in the comments box.





Clicking Ok,optimizeit outputs the results to the defined HTML file and opens automatically:





If you want to generate a report of the program being tested, click on the Generate Reports button on the toolbar. The open window is as follows:





Select the directory where the results are stored and enter the filename. Select all of the options listed in options and click the Write-to button. Optimizeit automatically generates reports to the specified directory and automatically adds date and time information after the file name and automatically opens:





Click the Stop Java program to stop the test.

Click menu file| Save saves the current settings.


Save (and merge) test results



The Code coverage can not only view the coverage information in real time while in use, but also provides the ability to save and correlate test results. In this section, we create a snapshot of the Run-time information and then merge to get a complete test result of the program:


File| from Menu Open selects a previously saved scheme or file| from the menu In the Open recent, select the settings you just saved.

Click Start now to run JUnit.

Now run BankAccountTest1 in JUnit. Note that the Reload classes option in the JUnit window is not selected.

Click the Show class Coverage button to switch to the class overlay window.

Choose file| from the menu Generate Snapshot, open the Generate Snapshot window:





Select a directory and enter the name of the snapshot in the Name box, and then click the Write Snapshot button.

Click the Mark button on the toolbar to mark the current class.

Return to JUnit, select BankAccountTest2 from Test Selectotr, and then click Run.

Returning to the Optimizeit class coverage, you can see that the percentage coverage information for the banking class in the BankAccountTest2 window after the test execution becomes 100%, saving the information as a snapshot.

Open the DOS window and set the Optimizeit installation directory to the current directory, which has an executable program named Snapmerge.

On the DOS command line, enter: Snapmerge mergefile.snp snapshot1.snp SNAPSHOT2.SNP


Here mergefile.snp is the snapshot after merging, SNAPSHOTX.SNP is the name of the snapshot to be merged together. Note the file name in the command line should contain the. SNP suffix.


Return to the code coverage and click the Stop stop virtual machine. Code coverage opens a snapshot after the virtual machine stops.

Click menu file| Open snapshot, opens the merged snapshot.

The Code coverage appears as follows:






You can merge multiple snapshot files together. This is very useful. For example, if a different person is testing a different function of a program, everyone's test results merged into the same snapshot file will be combined into a complete coverage test result for that product.



This article ends here. For more information about code coverage operations and functionality, check the Optimizeit code coverage user manual.



Translation: Wyingquan@hotmail.com 2004-11-8

If you are interested in this article or white box test, please login to http://groups.yahoo.com/group/WhiteBoxTestCN/and join the member to download the complete contents of this article (Word document in the form of a stuck picture). Here are some other white box testing tools and related discussions.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.