iOS 和 XCode:了解自动测试
iOS and XCode: understanding automatic testing
我创建了一个测试项目并注意到 XCode 生成了一些名为 class 的测试:YourProjectName-ProjectTests
这是它的样子:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
谁能告诉我如何使用这些自动化测试方法?有这方面的官方文档吗?
这些测试是用作 UI 测试还是应用代码测试?或两者?
这就是 单元测试 在 Xcode 中的实现方式。
setUp 和 TearDown 方法用于准备测试单元所需的任何模拟数据(这是代码的 select 部分)。
上面的两种测试方法只是一个例子。通常作为代码单元的作者,您很清楚该单元应该做什么和不应该做什么。
使用测试方法,您可以测试单元如何满足假设和期望。惯例是为单元的每个 assumption/expectation 行为设置单独的测试方法。
例如,给定 class 处理具有使所有字符串大写的方法的字符串,您将测试传递任何随机非大写字符串仍然 return 全部大写。将 none truing 作为参数将 return nil 或错误。
所以基本上你测试你的单元是否按预期运行。
单元测试背后有一整套理论,超出了本主题的范围。只是 Google 它。
您通过 XCTest
执行 UI 测试,但它是为单元测试而设计的。 Instruments 有 UI Automation 专为 UI 测试而设计。
打开 XCTestAssertions.h
将为您提供有关 XCTest 中包含的内容的很好参考。通常所有断言都遵循类似的模式:
XCTAssertSomething(somethingBeingTested, @"A message to display if the test fails");
点击 ⌘ 5 将调出测试导航器,它可以让您 运行 所有测试、所有失败的测试或单个测试。您还可以通过单击编辑器中方法名称左侧的菱形来 运行 测试。
以下是我在使用 XCTest
进行单元测试时遇到的一些提示:
断言中的最后一个参数是可选的,但在某些情况下它对描述您正在寻找的行为很有用。
测试没有运行任何特定的顺序,但是无论如何你的测试不应该相互依赖。
Mock 对象让您进行的测试远远超过基本断言。我喜欢 OCMock.
您可以 use instruments 在您的测试中进行调试。
最后,如果您可以访问另一台机器,您可以将 OSX 服务器设置为每天或在每次提交时 automatically run your tests,并在任何测试失败时通知您。
我创建了一个测试项目并注意到 XCode 生成了一些名为 class 的测试:YourProjectName-ProjectTests
这是它的样子:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
谁能告诉我如何使用这些自动化测试方法?有这方面的官方文档吗?
这些测试是用作 UI 测试还是应用代码测试?或两者?
这就是 单元测试 在 Xcode 中的实现方式。
setUp 和 TearDown 方法用于准备测试单元所需的任何模拟数据(这是代码的 select 部分)。
上面的两种测试方法只是一个例子。通常作为代码单元的作者,您很清楚该单元应该做什么和不应该做什么。
使用测试方法,您可以测试单元如何满足假设和期望。惯例是为单元的每个 assumption/expectation 行为设置单独的测试方法。
例如,给定 class 处理具有使所有字符串大写的方法的字符串,您将测试传递任何随机非大写字符串仍然 return 全部大写。将 none truing 作为参数将 return nil 或错误。
所以基本上你测试你的单元是否按预期运行。
单元测试背后有一整套理论,超出了本主题的范围。只是 Google 它。
您通过 XCTest
执行 UI 测试,但它是为单元测试而设计的。 Instruments 有 UI Automation 专为 UI 测试而设计。
打开 XCTestAssertions.h
将为您提供有关 XCTest 中包含的内容的很好参考。通常所有断言都遵循类似的模式:
XCTAssertSomething(somethingBeingTested, @"A message to display if the test fails");
点击 ⌘ 5 将调出测试导航器,它可以让您 运行 所有测试、所有失败的测试或单个测试。您还可以通过单击编辑器中方法名称左侧的菱形来 运行 测试。
以下是我在使用 XCTest
进行单元测试时遇到的一些提示:
断言中的最后一个参数是可选的,但在某些情况下它对描述您正在寻找的行为很有用。
测试没有运行任何特定的顺序,但是无论如何你的测试不应该相互依赖。
Mock 对象让您进行的测试远远超过基本断言。我喜欢 OCMock.
您可以 use instruments 在您的测试中进行调试。
最后,如果您可以访问另一台机器,您可以将 OSX 服务器设置为每天或在每次提交时 automatically run your tests,并在任何测试失败时通知您。