Testing with Xcode

At a Glance

The XCTest framework is automatically linked by all new test targets.(XCTest框架由所有新的测试目标自动连接)

  • Quick Start. Beginning with Xcode 5 and the introduction of the XCTest framework, the process of configuring projects for testing has been streamlined and automated with the test navigator to ease bringing tests up and running.(快速开始。从Xcode5开始,XCTest框架的介绍、为了测试而配置项目的过程已经变得高效和自动化,因为测试导航栏使测试集的启动和运行容易些。)

  • Performance Measurement. Xcode 6 and later includes the ability to create tests that allow you to measure and track performance changes against a baseline.(性能测量。Xcode6和后面的版本包括创建那些允许你根据基线测量和跟踪性能改变的功能的测试。)

  • UI Testing. Xcode 7 adds capabilities for writing tests that exercise an app’s UI. It includes the ability to record UI interactions into source code that you can transform into tests.(UI测试。Xcode7增加了那些训练一个app的UI而写的测试功能。它包括的功能有:记录UI的交互到源码,然后转变为测试集用例)

  • Continuous Integration and Xcode Server. Xcode tests can be executed using command-line scripts or configured to be executed by bots on a Mac running Xcode Server automatically.(持续集成和Xcode服务器。Xcode测试集可以通过使用命令行脚本执行自动化的执行,或者被运行Xcode服务器的Mac上的机器自动运行。)

    Quick Start

    The intent of this quick start is to show that you can make testing an integral part of your software development, and that testing is convenient and easy to work with.(这个快速开始章节的目的是为了展示,你可以很方便和简单的测试你的软件开发重要的部分)

  • Introducing the Test Navigator

  • You’ll use the Xcode test navigator often when you are working with tests.(当你经常用的测试时,你会使用到Xcode测试导航)

    The test navigator is the part of the workspace designed to ease your ability to create, manage, run, and review tests. You access it by clicking its icon in the navigator selector bar, located between the issue navigator and the debug navigator. When you have a project with a suite of tests defined, you see a navigator view similar to the one shown here.(测试导航器是工作空间的一部分,来让你更容易创建、管理、运行、回归测试集。你通过点击它在导航选择栏的图标就可以打开它,图标的位置在issue导航器和debug导航器。当你有一个工程有一个已经定义好的测试集后,你会看见一个和这里展示的相似的导航器)

  • ../Art/twx-qs-1.shot/Resources/shot_2x.png

  • The test navigator shown above displays a hierarchical list of the test bundles, classes, and methods included in a sample project. This particular project is a sample calculator app. The calculator engine is implemented as a framework. You can see at the top level of the hierarchy the SampleCalcTests test bundle, for testing the code in the application.(在上面展示的测试导航器展示了在一个简单的工程中一个有层次的包括test bundles、classes、methods的集合。这个特殊的工程时一个简单的计算器app。计算器引擎是作为一个框架实现的。你可以看见在这个层次体系的顶端的SampleCalcTests test bundle,是为了测试这个应用的代码)

  • Note: Xcode test targets produce test bundles that are displayed in the test navigator.(Xcode test targets产生的test bundles是展示在测试导航器的)

    If your tests use assets—data files, images, and so forth—they can be added to the test bundle and accessed at run time using the NSBundle APIs. Using the class method bundleForClass: with your test class ensures that you obtain the correct bundle to retrieve assets.(如果测试使用资产数据文件、图像等,则可以将它们添加到测试包中,并在运行时使用NSBundle api进行访问。对测试类使用类方法bundleforcass:可以确保获得正确的包来检索资产。)

    Xcode schemes control what is built. Schemes also control which of the available test methods to execute for the test action. You can enable and disable test bundles, classes, and methods selectively by Control-clicking the items in the test navigator list and choosing Enable or Disable from the shortcut menu, thereby enabling or disabling the items in the scheme.(Xcode方案控制构建的内容。方案还控制要为测试操作执行的可用测试方法。通过控制单击测试导航器列表中的项并从快捷菜单中选择“启用”或“禁用”,可以有选择地启用和禁用测试束、类和方法,从而启用或禁用方案中的项。)

  • The active test bundle in this view is SampleCalcTestsSampleCalcTests includes one test class, which in turn contains nine test methods. The Run button (../Art/twx-test_navigator_run_button_2x.png) appears to the right of the item name when you hold the pointer over any item in the list. This is a quick way to run all the tests in a bundle, all the tests in the class, or any individual test. Tests return pass or fail results to Xcode. As tests are being executed, these indicators update to show you the results, a green checkmark for pass or a red x for fail. In the test navigator shown here, two of the tests have asserted a failure.(此视图中的活动测试包是SampleCalcTests。SampleCalcTests包含一个测试类,该类又包含九个测试方法。当您将指针放在列表中的任何项上时,“运行”按钮(../Art/twx-test_navigator_run_button_2x.png)将出现在项名称的右侧。这是运行捆绑包中的所有测试、类中的所有测试或任何单个测试的快速方法。测试将通过或失败的结果返回到Xcode。在执行测试时,这些指示器会更新以显示结果,绿色复选标记表示通过,红色x表示失败。在这里显示的测试导航器中,有两个测试断言失败)

  • ../Art/twx-qs-2.shot/Resources/shot_2x.png

  • Clicking any test class or test method in the list opens the test class in the source editor. Test classes and test methods are marked in the source editor gutter with indicators as well, which work in the same way that they do in the test navigator. Test failures display the result string at the associated assertions in the source editor.

    At the bottom of the test navigator is the Add button (+) as well as filtering controls. You can narrow the view to just tests in the active scheme or just failed tests, and you can also filter by name.(单击列表中的任何测试类或测试方法将在源编辑器中打开测试类。测试类和测试方法在源编辑器栏中也用指示器标记,它们的工作方式与在测试导航器中的工作方式相同。测试失败在源编辑器中的关联断言处显示结果字符串。

    在测试导航器的底部是“添加”按钮和筛选控件。您可以将视图缩小到只在活动方案中进行测试或只进行失败的测试,还可以按名称进行筛选。)

  • Add Testing to Your App

  • New app, framework, and library projects created in Xcode 5 or later are preconfigured with a test target. When you start with a new project and open the test navigator, you see a test bundle, a test class, and a template test method. But you might be opening a preexisting project from an earlier version of Xcode that has no test targets defined yet. The workflow presented here assumes a preexisting project with no tests incorporated.(在Xcode 5或更高版本中创建的新应用程序、框架和库项目是使用测试目标预先配置的。当您从一个新项目开始并打开测试导航器时,您将看到一个测试包、一个测试类和一个模板测试方法。但是,您可能正在打开Xcode早期版本中已存在的项目,该版本尚未定义测试目标。这里介绍的工作流假设一个预先存在的项目,没有包含任何测试。)

  • Create a Test Target

  • With the test navigator open, click the Add button (+) in the bottom-left corner and choose New Unit Test Target from the menu.(在测试导航器打开的情况下,单击左下角的Add按钮(+),然后从菜单中选择newunittesttarget。)
  • ../Art/twx-qs-3.shot/Resources/shot_2x.png
  • Pick the macOS or iOS Unit Testing Bundle from the next dialog and click Next. In the new target setup assistant that appears, edit the Product Name and other parameters to your preferences and needs.(从下一个对话框中选择macOS或iOS单元测试包,然后单击next。在出现的新目标安装助手中,根据您的首选项和需要编辑产品名称和其他参数。)
  • ../Art/twx-qs-4.shot/Resources/shot_2x.png
  • Click Finish to add your target, which contains a template test class and two test method templates, to the test navigator view.(单击Finish将包含一个模板测试类和两个测试方法模板的目标添加到test navigator视图中。)
  • ../Art/twx-qs-5.shot/Resources/shot_2x.png
  • Run the Test and See the Results

  • Now that you’ve added testing to your project, you want to develop the tests to do something useful. But first, hold the pointer over the SampleCalcTests test class in the test navigator and click the Run button to run all the test methods in the class. The results are indicated by the green checkmarks next to the function names and in the source editor gutter.(既然您已经将测试添加到项目中,那么您希望开发测试来做一些有用的事情。但首先,将指针放在测试导航器中的SampleCalcTests测试类上,然后单击Run按钮来运行该类中的所有测试方法。结果由函数名旁边和源编辑器边沟中的绿色复选标记表示。)
  • ../Art/twx-qs-6.shot/Resources/shot_2x.png
  • The template unit and performance tests are both empty, which is why they post success indications; no failure was asserted. Notice the gray diamond on line 34 in the figure at the measureBlock: method. Clicking this diamond displays the Performance Result panel.(模板单元和性能测试都是空的,这就是它们发布成功指示的原因;没有断言失败。注意图中第34行measureBlock:method处的灰色菱形。单击此菱形将显示“性能结果”面板。)
  • ../Art/twx-qs-7.shot/Resources/shot_2x.png
  • This panel allows you to set a performance baseline as well as edit the baseline and Max STDDEV parameters. These features will be discussed later.(此面板允许您设置性能基线以及编辑基线和最大STDDEV参数。这些特性将在后面讨论。)

  • Edit the Tests and Run Again

  • Because this sample project is a calculator app, you want to check whether it performs the operations of addition, subtraction, multiplication, and division correctly, as well as test other calculator functions. Because tests are built in the app project, you can add all the context and other information needed to perform tests at whatever level of complexity makes sense for your needs. Creating tests is a matter of adding methods to the unit tests implementation file.(因为这个示例项目是一个计算器应用程序,所以您需要检查它是否正确执行了加法、减法、乘法和除法运算,以及测试其他计算器函数。因为测试是在应用程序项目中构建的,所以您可以添加执行测试所需的所有上下文和其他信息,无论复杂程度如何,都可以满足您的需要。创建测试就是向单元测试实现文件中添加方法。)

  • For example, you insert the following #import and instance variable declarations into the SampleCalcTests.m file.(例如,将以下导入和实例变量声明插入SampleCalcTests.m文件。)

  • #import <XCTest/XCTest.h>
    //
    // Import the application specific header files
    #import "CalcViewController.h"
    #import "CalcAppDelegate.h"
     
    @interface CalcTests : XCTestCase {
    // add instance variables to the CalcTests class
    @private
        NSApplication       *app;
        CalcAppDelegate     *appDelegate;
        CalcViewController  *calcViewController;
        NSView              *calcView;
    }
    @end

    Then give the test method a descriptive name, such as testAddition, and add the implementation source for the method.(然后给测试方法一个描述性的名称,比如testAddition,并为该方法添加实现源。)

  • - (void) testAddition
    {
       // obtain the app variables for test access
       app                  = [NSApplication sharedApplication];
       calcViewController   = (CalcViewController*)[[NSApplication sharedApplication] delegate];
       calcView             = calcViewController.view;
     
       // perform two addition tests
       [calcViewController press:[calcView viewWithTag: 6]];  // 6
       [calcViewController press:[calcView viewWithTag:13]];  // +
       [calcViewController press:[calcView viewWithTag: 2]];  // 2
       [calcViewController press:[calcView viewWithTag:12]];  // =
        XCTAssertEqualObjects([calcViewController.displayField stringValue], @"8", @"Part 1 failed.");
     
       [calcViewController press:[calcView viewWithTag:13]];  // +
       [calcViewController press:[calcView viewWithTag: 2]];  // 2
       [calcViewController press:[calcView viewWithTag:12]];  // =
        XCTAssertEqualObjects([calcViewController.displayField stringValue], @"10", @"Part 2 failed.");
    }

    Notice that the list in the test navigator changed to reflect that the sample test method, testExample, has been replaced by testAddition.(注意,test navigator中的列表已更改,以反映示例测试方法testExample已被testAddition替换。)

  • ../Art/twx-qs-8.shot/Resources/shot_2x.png

  • Now use the Run button in the test navigator (or the indicator in the source editor) to run the testAddition method.(现在使用测试导航器中的Run按钮(或源编辑器中的指示器)来运行testAddition方法。)

  • ../Art/twx-qs-9.shot/Resources/shot_2x.png

  • As you can see, an assertion failed and is highlighted in the test navigator and the source editor. Looking at the source, Part 1 succeeded—it is Part 2 that has a problem. On closer examination, the error is obvious: In line 76, [calcView viewWithTag:11] is off by one, it should be [calcView viewWithTag:12]. Correcting this error fixes the problem and the test succeeds.(如您所见,断言失败,并在测试导航器和源编辑器中突出显示。从源代码来看,第1部分成功了是第2部分有问题。仔细检查,错误显而易见:在第76行中,[calcView viewWithTag:11]被一个关闭,应该是[calcView viewWithTag:12]。更正此错误可解决问题,测试成功。)

  • ../Art/twx-qs-8a.shot/Resources/shot_2x.png

  • Use the setUp() and tearDown() Methods for Common Code

  • Xcode runs test methods one at a time for all the test classes in the active test bundle. In this small example only the one test method was implemented in the test class, and it needed access to three of the calculator app variable objects to function. If you wrote four or five test methods in this same class, you might find yourself repeating the same code in every test method to obtain access to the app object state. The XCTest framework provides you with instance methods for test classes, setUp and tearDown, which you can use to put such common code called before and after runs each test method runs.

    Using setUp and tearDown is simple. From the testAddition source in Mac_Calc_Tests.m, cut the four lines starting with // obtain the app variable for test access and paste them into the default setUp instance method provided by the template.(Xcode一次为活动测试包中的所有测试类运行一个测试方法。在这个小例子中,测试类中只实现了一个测试方法,它需要访问三个计算器应用程序变量对象才能正常工作。如果您在同一个类中编写了四个或五个测试方法,您可能会发现自己在每个测试方法中重复相同的代码以获得对应用程序对象状态的访问。XCTest框架为您提供了测试类、设置和拆卸的实例方法,您可以使用这些方法来放置在运行每个测试方法之前和之后调用的公共代码。

    使用setUp和tearDown很简单。从Mac_Calc_Tests.m中的testAddition源,剪切以//开头的四行,获取用于测试访问的app变量,并将它们粘贴到模板提供的默认安装实例方法中。)

  • - (void)setUp
    {
        [super setUp];
        // Put setup code here. This method is called before the invocation of each test method in the class.
     
       // obtain the app variables for test access
       app                  = [NSApplication sharedApplication];
       calcViewController   = (CalcViewController*)[[NSApplication sharedApplication] delegate];
       calcView             = calcViewController.view;
    }

    Now add more test methods—testSubtraction and others—with minimal duplicated code.(现在添加更多的测试方法testdetraction和其他具有最少重复代码的方法。)

  • ../Art/twx-qs-10.shot/Resources/shot_2x.png

  • Summary

  • As you can see from this short Quick Start, it is simple to add testing to a project. Here are some things to notice:(从这个简短的快速开始可以看出,向项目添加测试非常简单。以下是一些需要注意的事项:)
  • Xcode sets up most of the basic testing configuration. When you add a new test target to a project, Xcode automatically adds it to the scheme for its associated product target. An initial test class with a single test method is added with the target, and can be found in the test navigator.(Xcode设置了大多数基本的测试配置。将新的测试目标添加到项目时,Xcode会自动将其添加到与其关联的产品目标的方案中。带有单个测试方法的初始测试类与目标一起添加,可以在测试导航器中找到。)
  • The test navigator lets you locate and edit test methods easily. You can run tests immediately using the indicator buttons in the test navigator or directly from the source editor when a test class implementation is open. When a test fails, indicators in the test navigator are paired with failure markers in the source editor.(测试导航器允许您轻松地定位和编辑测试方法。当测试类实现打开时,可以使用测试导航器中的指示符按钮或直接从源编辑器运行测试。当测试失败时,测试导航器中的指示器与源编辑器中的失败标记配对。)
  • A single test method can include multiple assertions, resulting in a single pass or fail result. This approach enables you to create simple or very complex tests depending on your project’s needs.(一个测试方法可以包含多个断言,从而产生一个通过或失败的结果。这种方法使您能够根据项目的需要创建简单或非常复杂的测试。)
  • The setup and tearDown instance methods provide you with a way to factor common code used in many test methods for greater consistency and easier debugging.(setup和tearDown实例方法为您提供了一种将许多测试方法中使用的公共代码考虑在内的方法,以提高一致性并简化调试。)

  •  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值