Reprinted from Http://www.infoq.com/cn/articles/ios-unit-test-1
Introduction : This article does not discuss what unit testing is, or its pros and cons of a project, and I think unit testing is an effective tool for developers to ensure the quality of their output code. This article from the user's point of view compared with the current more popular two unit testing framework, to give you some suggestions for selection. If you do not know much about the role of unit testing in engineering, or if you do not yet know the development model of TDD, refer to:test-driven Development and Unit testing.
This article compares the common Unit testing framework in two iOS development: Ocunit, which is officially integrated into the Xcode 4.x version, and Ghunit, the most recommended test framework with GUI interface. The first glimpse of the two test frameworks is very similar, and the difference is found in the use of the app. The difference in the details makes the two frames have their pros and cons at different angles.
Ocunit
Related Vendor Content
Docker-based devops-- Qcon Shanghai 2014 keynote pea pod architect Greatlypraised joins Archsummit Beijing 2014 Conference Organizing Committee AWS Technical presentation: How AWS Design Patterns are implemented in Autodesk How to build an engineer culture Team white paper Download: Pain points and best practices for release Management
Ocunit is an integrated unit testing framework for Xcode 4.x, and the tests in Ocunit are divided into two categories, one called Logic Tests and the other called Application Tests. Logic tests is more inclined to the so-called white-box test, which is used to test the more detailed logic of the project; application tests prefers black-box testing, or interface testing, to test interfaces that interact directly with the user.
• Add Unit Tests
Ocunit is integrated with Xcode, so its combination with engineering should be the best, and the cost to add to the project should be the lowest. There is a "Include Unit Tests" option in the process of creating a new project using Xcode (1), and the new project will automatically generate a logic Tests.
Adding Ocunit Logic tests to existing projects is not complicated, just add a target of type: "Cocoa Touch Unit Testing Bundle" (2).
Figure 2, adding a ocunit test to an existing project
When you add a test target to an existing project, Xcode automatically generates a scheme that runs the unit test case and the build original project needs to switch different scheme. If you think switching Scheme is a hassle, you can also cancel "autocreate schemes" (3) in the Manage Scheme menu before adding the target.
Figure 3, adding target does not create scheme
Application tests to make some changes based on logic tests. In general, a project requires both logic Tests and application Tests, so it is recommended to add a separate target as described above, and then do the following (4):
1. Search for "bundle loader" in build settings, set to: $ (built_products_dir)/app_name.app/app_name (App_name is the app name)
2. Then search for "test host", set to: $ (bundle_loader)
3. Add dependencies in Build phases-target dependencies, select the main program Target
Figure 4, add a application Tests
• Create test Cases
The most common methods of ocunit test cases are three
1.-(void) SetUp: Called before each test method executes
2.-(void) TearDown: Called after each test method executes
3.-(void) Testxxx: Test method named xxx
When adding target, Xcode has automatically created a test case class: Unittestdemotests, where Unittestdemo is the name of the project, which already contains Setup,teardown and testexample three methods.
With Command+n, select "Objective-c test Case Class" to create a new test cases Class (5). The test case class created through Xcode is an empty class that inherits from Sentestcase (Ocunit developed by Sen:te, so the base class is named Sentestcase) and needs to imitate unittestdemotests to write test methods.
Figure 5, creating a test case class
Developers can implement no return values themselves, and the naming convention is an instance method of Testxxx, and uses a large number of assertion methods provided by the framework.
The difference between logic tests and application tests is primarily in the Setup method, where logic tests simply initializes some test data in the Setup method, and application Tests needs to get the appdelegate of the main application in the Setup method for the test method call.
It is important to note that Ocunit's test bundle is intrusive to the main application, so be very careful in using the process, do not let the unit test resources to cover the main application resources, causing a strange bug.
• Run Tests
Because Ocunit is a framework integrated in Xcode, it is also easier to run in Xcode. The scheme for switching to unit tests (if the project is shared with scheme without switching), product->test (or directly using the shortcut key Command+u), the framework automatically finds subclasses of Sentestcase in all projects, Run the no return value method where all names resemble testxxx.
• Test Feedback
Ocunit's failure method will be two position feedback via console and Xcode issues, and with xcode issues you can navigate directly to the unit test code line where the error occurred. The issue message is the description defined in the unit test assertion method.
Ghunit
Ghunit is a OBJECTIVE-C test framework that supports OSX's engineering in addition to supporting iOS engineering, but OSX is not covered in this article. Unlike Ocunit, Ghunit provides a GUI interface to manipulate test cases, and does not differentiate between logic Tests and application Tests.
• Add Unit Tests
The process of adding ghunit is slightly more complex than the Ocunit integrated into Xcode. First on the Download Ghunit framework package, the current for iOS latest version is 0.5.6, after decompression is a ghunitios.framework folder.
Open the existing project, add a emptyapplication target, and add the newly downloaded ghunitios.framework (6, 7) to the new target.
Figure 6, adding ghunitios.framework to the new target
Adding an unofficial framework to build phases does not copy the framework file to the project directory, but rather a link, so it is recommended that you copy the framework to the project directory before adding it.
Figure 7, select Ghunitios.framework
Next, add the other libraries that the framework relies on in the same way: "Quartzcore.framework".
Search for "linker flags" in build settings, set up other linker Flags-debug-add a labeled "-objc-all_load" (8) that supports the full-schema and full-version SDK.
Figure 8, setting linker flags
Delete the appdelegate (. h and. m) in tests target. Modify the main function, support Ghunitios, import ghunitiosappdelegate instead of the original appdelegate, modify the Uiapplicationmain parameters (9).
Figure 9, modifying the main function
Now that you have finished adding the Ghunit, choose the scheme created by the new target and start a new app (10) in the device or simulator, which is the app that the unit tests.
Figure 10, Unit test app
• Create test Cases
Creating a Ghunit test case is similar to creating a ocunit test case.
Create a new Objective-c class file that inherits from Ghtestcase, does not import GHUnit.h files in the. h file generated by Xcode and requires the developer to import "#import <GHUnitIOS/GHUnit.h>" yourself.
The Ghunit framework provides an assertion method that is richer than ocunit, and the development use case can be more detailed and advantageous in finding/locating errors.
The naming convention for test methods, like Ocunit, is the no return value method that begins with test:-(void) testxxx. The usual method, in addition to the above mentioned setup and Teardown,ghunit, provides the SetupClass and Teardownclass two methods, which are called before and after the use case is run. Also, just mentioned that Ghunit does not differentiate between logic Tests and application Tests, so there is no distinction between setup and Teardown methods.
• Run Tests
Running Ghunit takes two steps, first compiling and installing the Unit test app into the device or simulator (11), creating two use cases, one in each case.
Figure 11, the Ghunit App for two use cases
In the app, you can run all of the use cases by clicking the Run button in the upper right corner, and the framework will look for all the no return value methods named Testxxx and execute. Or, click a cell in TableView to run a separate test method.
• Test Feedback
Methods that fail the assertion failure test are marked in red in the app, and the elapsed time of each method is given. Detailed error messages are printed in the console, including: Exception type, error file, location, and the reason for the error specified in the assertion method. More importantly, the program stack contents (12) When the error occurs.
Figure 12, the method of failure to pass the test, the contents of the console
Ghunit helps developers with content in the console to quickly pinpoint where the program is wrong, which is better than ocunit.
Summarize
Ghunit does seem a bit cumbersome to install, and cannot be compared to ocunit integrated in Xcode. But from the developer's point of view, I prefer the experience that Ghunit brings, the GUI interface can be run from the IDE alone, support running a single test method and running all the use cases, the print error stack can be faster to locate the problem.
This article briefly introduces the installation and introduction of the two frameworks to get an initial look at their features, and in the next article we'll show you in more detail how to use the framework for unit testing, as well as some of the advanced features in the framework. In addition, the follow-up will introduce additional unit testing frameworks that are more distinct from the two frameworks.
Unit tests in iOS (i)