IOS 9 Learning Series: Xcode Code Coverage tool Introduction

Source: Internet
Author: User

IOS 9 Learning Series: Xcode Code Coverage tool Introduction

Code coverage is a tool for calculating the coverage rate of your unit test. High coverage brings confidence to your unit test and shows that your application has been thoroughly tested. You may have written thousands of unit tests, but if the coverage rate is not high, the tests you have written may be of little value.

There is no exact percentage here, which requires you to reach this coverage rate. This depends largely on your project ). For example, if your project has many visual components that cannot write unit tests, the coverage rate will be much lower than the data processing framework.

Code Coverage in Xcode

In the past, if you wanted to prepare a test code coverage report, you would need to set many [Options]. It is very complicated, and there are a lot of manual settings. In iOS 9, Apple provides a smart code coverage tool that integrates LLVM and is called and computed every time a test is run.

Using the Code Coverage Tools

Now we use an example to demonstrate how to use the new code coverage tool and how to improve the current test cases. After the code is completed, put it on Github. You can follow it.

The first thing is to create a new project and confirm that you have selected the Unit tests option. This will create a default project as required. Now we need to test what to test. This test may be your arbitrary requirement. Here I add an empty swift file and write a global method. This method checks whether the two strings are words composed of different identical letters. Writing a global method may not be a good design, but here we will only demonstrate it.

This is a relatively simple method, so we may get a test coverage rate of 100% coverage without any problems.

Once you have written the algorithm, you should write a test. Open the default XCTestCase created when the project is created, and add the following simple test method. He looks like this.

 

Before running the test, we must first check whether the code coverage is enabled. When writing the code, it is disabled by default. So you need to edit your test scheme and open it.

Make sure that "Gather coverage data" is selected, and then click the close button to run the test target. We hope that the test case just created can pass smoothly.

The Coverage Tab

Once this test is passed, you will be able to know that the checkWord method has at least one path that is correct. But what you don't know is how many others are not tested. This is the benefit of code coverage. After you open the code coverage tab, you can clearly see the coverage of the test. They automatically grouped by target, file, and function.

Open the Report Navigator panel in the left-side window of Xcode and select the test you just run. Select Coverage in the tab.

 

This will display a list of your classes and methods and indicate each test coverage rate. If you hover your mouse over the checkWord method, you can see that the test coverage rate is 28%. Unacceptable! We need to find out the code branches that can be tested and executed, and those that cannot be used to improve them. Double-click the method name. Xcode will open the class code and view the code coverage.

 

The white area indicates that the code is overwritten during the test. If the gray area is not covered by the test, we need to add more test cases to overwrite the gray part of the code. The number on the right hand side indicates the number of times these code blocks were executed in this test.

Improving Coverage

Obviously, 28% of coverage is not our goal. There is no UI here, and it looks like a perfect candidate function for writing test cases. Therefore, we add a test case. Ideally, we want each branch to be tested. In this way, complete coverage can be achieved. Add the following test cases to your test class.

These test cases should completely overwrite our code. Run the unit and open the last test report.

We succeeded, with a coverage rate of 100%. You can see that the entire code is white, and the number on the right shows that each code segment is executed at least once.

Using code coverage is a great way to help you build a truly valuable test combination. Far better than writing many test cases, but not really testing the code. Xcode 7 makes this method very simple. I recommend that you enable Code Coverage in the project. Even if you have already completed the test, you can know how the test is written.

 

Demo At Github address: https://github.com/fish-yan/XCode-Code-Coverage

 

Related Article

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.