You use logic unit tests to perform exhaustive,highly tailored testing of your code. With application unit tests you test your code within an app running in a simulator or on a device,with access to the resources available in the Cocoa framework in Mac apps,and the Cocoa touch framework in iOS apps. A unit-test target can perform either logic unit tests or application unit tests on your code or your app.
The most convenIEnt way of configuring unit tests in a project or target is at the time you create it. If you select the Include Unit Tests option when you create a project or a target,Xcode includes a unit-test target in the scheme that builds the product. For apps,the unit-test target implements application unit tests. For all other product types,the unit-test target implements logic unit tests.
A scheme’s Test action IDentifIEs the test suites Xcode performs when you execute the Test command on that scheme. To implement unit-testing in an existing project,you add one or more unit-test targets to the project.
iOS: Because Xcode cannot run logic unit tests on iOS-based devices,you should configure specialized schemes to perform them.
This section describes how to add unit-test targets to a project.
Adding a Unit-Test Target to Your ProjectUnit-test targets define one or more test suites.
Note: Regardless of the type of unit-testing you want the target to perform,these instructions generate a unit-test target that implements logic unit tests. See “Setting Up Application Unit Tests” for instructions on changing a logic unit-test target into an application unit-test target.
To add a unit-test target to a project:
Open the project to which you want to add a unit-test target.
Choose file > New > New Target.
Select the unit-testing bundle template for your platform:
iOS: In the iOS section,select Other,then select the Cocoa touch Unit Testing Bundle template.
Mac: In the OS X section,then select the Cocoa Unit Testing Bundle template.
Click Next.
Specify the following unit-test target options:
Product name: Enter a name for the target’s product that IDentifIEs the type of unit testing you want the target to perform and that includes the “Tests” suffix (for example,MyAppLogicTests
,MyAppApplicationTests
).
You can use any name for the target,but it’s helpful to include the type of unit tests you intend the target to run and the fact that the target runs unit tests.
Project: Choose the project to which you want to add the unit-test target.
Click Finish.
After adding the unit-test target the project navigator shows a new group—with the same name as the target—that contains the source files that implement a temporary test case. The new target is Listed in the project’s target List.
If Xcode autocreates schemes for you,the Scheme toolbar menu contains a new scheme—with the same name as the unit-test target. This is the scheme you use to run the test cases that are part of the unit-test target. The Test action of the new scheme IDentifIEs the unit-test target,and Lists the test suites and test cases the scheme runs. You add additional test suites to a unit-test target by adding an Objective-C test case class to the target.Note: A unit-test target can perform only one type of unit testing,logic or application,not both.
Setting Up Logic Unit TestsIf you added a unit-test target to your project as described in “Adding a Unit-Test Target to Your Project,” the added target is already configured to perform logic tests.
To set up a unit-test target to perform logic tests:
In the project editor,select the unit-test target you want to set up,and display the Build Settings pane.
In the Build Settings pane’s scope bar,click All.
In the search fIEld,enter bundle loader
,and press Return.
If the Bundle Loader build setting appears in bold,select it,and press Delete.
In the search fIEld,enter test host
,and press Return.
If the Test Host build setting appears in bold,and press Delete.
To confirm that the unit-test target is configured correctly to perform logic tests:
From the Scheme toolbar menu,choose the unit-tests scheme and a run destination:
iOS: For logic tests,the run destination can be only a simulator.
Choose Product > Test.
Choose VIEw > Navigators > Issue,to display the issue navigator.
The issue navigator Lists the Failed temporary test case,indicating that the unit-test target is working correctly.
Choose VIEw > Navigators > Show Log Navigator to display the log navigator.
In the action List in the log navigator (on the left sIDe of the window),select the test run named after the unit-test target.
The test run log appears in the editor area.
The test-run transcript should look similar to Listing 2-1. The phrase “All tests” does not appear in the heading of the transcript; this indicates that the unit tests performed are logic unit tests.Listing 2-1 Transcript of a logic unit-test run
Test Suite '/Users/ernest/library/Developer/Xcode/DerivedData/ |
MyApp-efqrlcykgdvbaocnrardtbvymzbp/Build/Products/DeBUG-iphonesimulator/ |
MyAppLogicTests.octest(Tests)' started at 2011-12-02 17:49:51 +0000 |
Test Suite 'MyAppLogicTests' started at 2011-12-02 17:49:51 +0000 |
Test Case '-[MyAppLogicTests testExample]' started. |
/Users/ernest/Desktop/MyApp/MyAppLogicTests/MyAppLogicTests.m:29: |
error: -[MyAppLogicTests testExample] : Unit tests are not implemented |
yet in MyAppLogicTests |
Test Case '-[MyAppLogicTests testExample]' Failed (0.000 seconds). |
Test Suite 'MyAppLogicTests' finished at 2011-12-02 17:49:51 +0000. |
Executed 1 test,with 1 failure (0 unexpected) in 0.000 (0.001) seconds |
Test Suite '/Users/ernest/library/Developer/Xcode/DerivedData/ |
MyApp-efqrlcykgdvbaocnrardtbvymzbp/Build/Products/DeBUG-iphonesimulator/ |
MyAppLogicTests.octest(Tests)' finished at 2011-12-02 17:49:51 +0000. |
Executed 1 test,with 1 failure (0 unexpected) in 0.000 (0.003) seconds |
At this point,you have a correctly configured unit-test target that runs logic unit tests. See “Writing Test Case Methods” to learn how to add test cases to it.
Setting Up Application Unit TestsIf you created a project including unit-testing,its main unit-test target is already configured to perform application unit tests.
To set up a unit-test target to perform application tests:
In the project editor,select the unit-test target,click All.
Set the value of the Bundle Loader build setting to:
iOS: $(BUILT_PRODUCTS_DIR)/<app_name>.app/<app_name>
Mac: $(BUILT_PRODUCTS_DIR)/<app_name>.app/Contents/MacOS/<app_name>
where <app_name>
is the name of your app (the value of the Product name build setting of the target that builds the app).
Xcode displays value of the build setting for the project’s build configurations,in this case DeBUG and Release.
Set the value of the Test Host build setting to:
$(BUNDLE_LOADER)
Make this target dependent on the target that builds your app.
Ensure that this target is in the Test action of the scheme that builds the app to which you want to add the application unit tests.
To confirm that the unit-test target is configured correctly to perform application tests:
From the Scheme toolbar menu,choose the scheme that builds the app to test and a run destination:
Choose Product > Test.
Choose VIEw > Navigators > Log to display the log navigator.
In the List in the log navigator (on the left sIDe of the window),select the test run named after the application unit-tests target.
The test run log appears in the editor area.
The test-run log should look similar to Listing 2-2. The phrase “All tests” appears in the heading of the transcript; this indicates that the unit tests performed are application unit tests.Listing 2-2 Transcript of an iOS Simulator-based application unit-test run
Test Suite 'All tests' started at 2011-12-02 19:21:52 +0000 |
Test Suite '/var/mobile/Applications/A973B25F-0B11-417D-AD59-2A499060F521/ |
MyAppApplicationTests.octest(Tests)' started at 2011-12-02 19:21:52 +0000 |
Test Suite 'MyAppApplicationTests' started at 2011-12-02 19:21:52 +0000 |
Test Case '-[MyAppApplicationTests testExample]' started. |
/Users/ernest/Desktop/MyApp/MyAppApplicationTests/MyAppApplicationTests.m:29: |
error: -[MyAppApplicationTests testExample] : Unit tests are not implemented |
yet in MyAppApplicationTests |
Test Case '-[MyAppApplicationTests testExample]' Failed (0.001 seconds). |
Test Suite 'MyAppApplicationTests' finished at 2011-12-02 19:21:52 +0000. |
Executed 1 test,with 1 failure (0 unexpected) in 0.001 (0.002) seconds |
Test Suite '/var/mobile/Applications/A973B25F-0B11-417D-AD59-2A499060F521/ |
MyAppApplicationTests.octest(Tests)' finished at 2011-12-02 19:21:52 +0000. |
Executed 1 test,with 1 failure (0 unexpected) in 0.001 (0.003) seconds |
Test Suite 'All tests' finished at 2011-12-02 19:21:52 +0000. |
Executed 1 test,with 1 failure (0 unexpected) in 0.001 (0.012) seconds |
以上是内存溢出为你收集整理的Unit-Testing in a Project --- 单元测试全部内容,希望文章能够帮你解决Unit-Testing in a Project --- 单元测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)