美文网首页
iOS 单元测试-UnitTest

iOS 单元测试-UnitTest

作者: 木马不在转 | 来源:发表于2017-05-27 12:05 被阅读48次
一:在iOS中单元测试主要使用场景

1、是写完代码以后:想要验证一下自己写的代码是否有问题。
2、写代码之前:就是写代码之前所有的功能分模块的设计好,测试通过了再写。(我反正是没用过)。
3、修复某个bug后:一般修复完某个bug,为了确保修复是成功的,会写测试。

二:创建具有单元功能的工程

1、如果是新建工程,直接勾选include Unit Tests就可以了,目前新建项目这个都是默认勾选的。


2、如果已有的工程不包含测试用例可以通过File-->new-->target-->iOS-->iOS Unit Testing Bundle来添加UnitTest。
三:XCTestCase了解

1、方法作用

   - (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.
  // Use XCTAssert and related functions to verify your tests produce the correct results.
  // 测试用例的例子,注意测试用例一定要test开头
  }
  - (void)testPerformanceExample {
  // This is an example of a performance test case.
  // 测试性能例子
  [self measureBlock:^{
      // Put the code you want to measure the time of here.
  // 需要测试性能的代码
  }];
  }

2、XCTest常用的断言及解释。

XCTFail(format…) 生成一个失败的测试; 
XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过;
XCTAssertNotNil(a1, format…)不为空判断,a1不为空时通过,反之不通过;
XCTAssert(expression, format...)当expression求值为TRUE时通过;
XCTAssertTrue(expression, format...)当expression求值为TRUE时通过;
XCTAssertFalse(expression, format...)当expression求值为False时通过;
XCTAssertEqualObjects(a1, a2, format...)判断相等,[a1 isEqual:a2]值为TRUE时通过,其中一个不为空时,不通过;
XCTAssertNotEqualObjects(a1, a2, format...)判断不等,[a1 isEqual:a2]值为False时通过;
XCTAssertEqual(a1, a2, format...)判断相等(当a1和a2是 C语言标量、结构体或联合体时使用, 判断的是变量的地址,如果地址相同则返回TRUE,否则返回NO);
XCTAssertNotEqual(a1, a2, format...)判断不等(当a1和a2是 C语言标量、结构体或联合体时使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判断相等,(double或float类型)提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判断不等,(double或float类型)提供一个误差范围,当在误差范围以内不等时通过测试;
XCTAssertThrows(expression, format...)异常测试,当expression发生异常时通过;反之不通过;(很变态) XCTAssertThrowsSpecific(expression, specificException, format...) 异常测试,当expression发生specificException异常时通过;反之发生其他异常或不发生异常均不通过;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression发生具体异常、具体异常名称的异常时通过测试,反之不通过;
XCTAssertNoThrow(expression, format…)异常测试,当expression没有发生异常时通过测试;
XCTAssertNoThrowSpecific(expression, specificException, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
四:简单使用

1.功能逻辑的测试

  • 在ViewController中写一个简单的方法
   - (int)testInt;//声明
   - (int)testInt;//实现
      {
          return 100;
      }
  • 在测试的文件中导入ViewController.h,并且定义一个vc属性,然后就可以实现测试了


command+u快捷方式运行,或者produce-->test都行,工程就跑起来了我们可以在在控制台清晰的看到我们要测试的用例子通过了,测试通过的测试方法会有绿色的钩。

这时候我们改下断言,把100随便改成一个数,120.再comand+u运行下,看下什么情况


提示测试不通过了,因为我们要测试的方法返回值是90,

2、代码性能测试
比如我们要测试

NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"cs"]);
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"cs"],

这二段代码执行的时间,我们可以在testPerformanceExample方法里测试。

end

相关文章

网友评论

      本文标题:iOS 单元测试-UnitTest

      本文链接:https://www.haomeiwen.com/subject/vlfqfxtx.html