Code coverage is a tool that calculates the coverage of your unit tests. A high level of coverage gives confidence in your unit tests and shows that your application is thoroughly tested. You may have written thousands of unit tests, but if the coverage is not high, then the set of tests you wrote may not be of much value. There is no exact percentage that you must reach. It depends a lot on your project. For example, if your project has many visual components that cannot be unit tested, then the coverage will be much lower than a framework that only processes data. Code Coverage in Xcode In the past, if you wanted to generate a code coverage report for a test, you had to set a lot of [options]. It was very complicated, and there were many manual settings. In iOS 9, Apple provided an intelligent code coverage tool that is integrated with LLVM and is called and calculated every time a test is run. Using the Code Coverage Tools Now we will use an example to show how to use the new code coverage tool and how to improve the existing test cases. The completed code is on Github and you can follow along. The first thing to do is create a new project and make sure you check the Unit tests option. This will create a default project as required, now we need to test something. This test can be anything you want, here I added an empty Swift file with a global method written in it. This method checks if two strings of letters are words that only have the same letters that are different in order. Writing it as a global method may not be a good design, but here we just want to demonstrate it. This is a relatively simple approach, so we will probably get 100% test coverage without any issues. check word Once you have written the algorithm, it is time to write a test. Open the default XCTestCase that was created when the project was created and add the following simple test method. It looks like this. tests Before running the tests, we must first make sure that code coverage is turned on. When writing code, it is turned off by default. So you need to edit your test scheme and turn it on. turn on coverage Make sure "Gather coverage data" is selected, then click the Close button and run the test target. We hope that the test case we just created will pass successfully. The Coverage Tab Once this test passes, you know that at least one path of the checkWord method is correct. 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 test coverage. They are automatically grouped by target, file, and function. Open the Report Navigator panel on the left side of the Xcode window, select the test you just ran, and then select Coverage in the tab. test coverage panel This will show you a list of your classes and methods, and indicate the test coverage for each. If you hover over the checkWord method, you can see that the test coverage is 28%. Unacceptable! We need to find out which code branches can be exercised by the tests and which cannot, and then improve them. Double-click the method name, and Xcode will open the class code and show the code coverage. coverage result The white area indicates that these codes are covered by the test. The gray area is not covered by the test, and we need to add more test cases to cover the gray code. The numbers on the right side indicate the number of times these code blocks are executed in this test. Improving Coverage Obviously, 28% coverage is not our goal. There is no UI here, so it seems like a good candidate for writing a test case. So, let's add a test case. Ideally, we want every branch to be tested. This will achieve complete coverage. Add the following test case to your test class. test cases These tests should fully cover 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 is white, and the numbers on the right show that each code segment was executed at least once. Using code coverage is a great way to build a truly valuable test suite. It's much better than writing a lot of test cases but not really testing the code. Xcode 7 makes this very easy, and I recommend turning on Code Coverage in your project. Even if you have already written the tests, it can help you know how well the tests are written. Further reading For more information about the Code Coverage tool in Xcode 7, I recommend watching WWDC 2015 session 410 Continuous Integration and Code Coverage in Xcode.*** Don’t forget that if you want to try out our example above, you can find it on Github. |
<<: 6 Things I Wish I Knew When I Wrote My First Android App
>>: Setting up the environment for decompiling Android apps on Mac OS
A promotion method that many webmasters are famil...
Some time ago, a celebrity wore a mouth guard to ...
This article is based on answering similar questi...
Beijing Winter Paralympics is about to open Xue R...
Author: Song Run, Children's Hospital Affilia...
[[134121]] Dear iOS and OS X application develope...
When you are on an airplane, you may have had thi...
I used to say that the soul of an anchor does not...
The following is the latest traffic ranking of 56...
When it comes to customer acquisition, there are ...
Does water have light and heavy water? Of course,...
Data analysis makes your sales boom come true Cou...
The weather is getting colder There are more reas...
What is the core competitiveness of event plannin...