IOS 9 Learning Series: Xcode Code Coverage

Source: Internet
Author: User


The Code coverage is a tool that calculates the coverage of your unit tests. A high level of coverage gives you confidence in your unit tests and also shows that your application has been thoroughly tested. You may have written thousands of unit tests, but if the coverage is not high, then the test you write may not be worth much.

There is no exact percentage that requires you to reach this coverage. This depends to a large extent on your project (in the specific case). For example, if you have many visual components in your project that cannot write unit tests, then coverage is much lower than simply working with the data frame.

Code Coverage in Xcode

In the past, if you wanted to make a test for the code coverage report, you would need to set a lot of [options]. Very complex, and many more need to be set manually. In IOS 9, Apple provides an intelligent code coverage tool that is integrated with LLVM and is called and evaluated each time the test runs.

Using the Code Coverage Tools

Now we use an example to show how to use the new Code coverage tool and how to improve the current test case. The finished code is placed on Github and you can follow it.

The first thing is to create a new project and confirm that you have selected the Unit tests option. This creates a default project as required, and now we need to test what the point is. This test may be any of your needs, here I add an empty swift file, which writes a global method. This method detects whether the two-letter string is a word that is composed of only the same letters in different order. Writing a global approach may not be a good design, but here's just a demonstration.

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


Check Word

Once you've written the algorithm, it's time to write a test. Open the default xctestcase that was created when the project was created, and add a simple test method below. That's what he looks like.



Tests

Before running the test, we must first confirm that the code coverage is open, and that when writing it, it is turned off by default. So you need to edit your test scheme and turn it on.


Turn on coverage

Make sure that "Gather coverage Data" is selected, then click the Close button to run the test target. We want the test cases that we just created to pass smoothly.

The Coverage Tab

Once this test is passed, you will know the Checkword method, at least one path is right. But what you don't know is how much more has not been tested. This is the benefit of the Code coverage tool. When you open the Code Coverage tab, you can clearly see the coverage of the test. They are automatically grouped by target, file, function.

Open the Report Navigator panel in the left-hand window of Xcode and select the test you just ran. Then select the Coverage in the tab.


Test coverage panel

This shows a list of your classes, methods, and each test coverage. If you hover your mouse over the Checkword method, you can see that the test coverage is 28%. Can't accept it! We need to find out that those code branches are able to be tested for execution and those that are not, and thus improve them. By double-clicking the name of the method, Xcode will open the code for the class, and see what happens to the code coverage.


Coverage result

The white area represents the code when the test is covered. Gray area when the test cannot be overwritten, we need to add more test cases to cover the gray part of the code. The numbers on the right hand side indicate the number of times these blocks of code were executed in this test.

Improving Coverage

Obviously, 28% coverage is not our goal. There's no UI, and it looks like a perfect candidate for writing test cases. So, we add a test case. Ideally, we want each of the branches to be tested. This will achieve a complete coverage. Add the following test cases to your test class.


Test Cases

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


The final coverage result

We succeeded, 100% coverage. You can see that the entire code becomes white, and the number on the right shows that each code snippet has been executed at least once.

Using the code coverage is a great way to help you build a truly valuable test mix. Far better than you write a lot of test cases, but not really tested to the code. Xcode 7 makes this way very simple, and I recommend that you open the Code Coverage in your project. Even if you have written the test, you can also help you know how the test is written.


Demo at GitHub address: https://github.com/fish-yan/XCode-Code-Coverage

IOS 9 Learning Series: 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.