接口测试小结

最近负责组里所有 iOS 组件的接口测试并产出接口测试的覆盖率,写了将近一个月,从入门到踩坑,特此做个小结。

Kiwi

Kiwi ,通俗来说,就是用断言方式来判断是否符合接口测试的对应条件。如果符合就表示对应判断的接口是测试成功的,反之亦然。

​ 形式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SPEC_BEGIN(XXXSpec)

describe(@"Example", ^{
beforeAll(^{
// Put setup code here. This method is called before the invocation of All Block in the class.
});
context(@"when ", ^{
beforeEach(^{
// Put setup code here. This method is called before the invocation of each test method in the class.
});
afterEach(^{
// Put teardown code here. This method is called after the invocation of each test method in the class.
});
it(@"should ", ^{
// This is an example of a functional test case.
...
[[expectFutureValue(theValue(entity.token)) shouldEventually] equal:theValue(@"K18vvdPIW1BwYr8nyJykoMVP2aqYZ72bZCOlts8ejTU9BihD9gt7T/tPFOI6wBHxmOv58Th7YP8q85/Eeko2AA==")];
// Test Success
});
});
});

SPEC_END

遇到的问题

  1. 非标准库构建的组件不支持 Kiwi 接口测试的问题
  2. 通过接口测试发现的一些问题
  3. 接口测试覆盖率问题
  4. 一些组件无法测试,比如 UI 相关、Performance 相关的等需要真实机器或逻辑中有断言。

对应解决方案

  1. 使用 pod lib create XXX 来创建 CocoaPods 的标准库
  2. 未判空导致 crash,组件版本依赖问题等
  3. iOS 采用 XcodeCoverage 来计算覆盖率
  4. 暂无解决方案,跳过(希望有知道的,不吝赐教)

踩过的坑

No.1

Q: build 出错,提示

1
2
3
4
duplicate symbol _OBJC_CLASS_$_MGJImage
...
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

A: Kiwi Spec 名不能与现有的组件名或测试的组件重复

1
SPEC_BEGIN(MGJImage)

应改为

1
SPEC_BEGIN(MGJImageSpec)

No.2

Q: build 报错,提示

1
'XCTest/XCTest.h' file not found

A: 对应 Kiwi Spec 文件的 Target Membership 要勾选到对应的 Test

No.3

Q: 每次添加 Kiwi 测试文件后,如果 build 失败,提示

1
'XXX' file not exist

A: 记得要 pod update成功后,再去 build。

No.4

Q: 组件数量很多时,需要不停地为每个组件重复地手动创建测试文件?

A: Kiwi-Template 可以为 Kiwi Test 提供默认的模板,方便书写接口测试

文章目录
  1. 1. Kiwi
  2. 2. 遇到的问题
  3. 3. 对应解决方案
  4. 4. 踩过的坑
    1. 4.1. No.1
    2. 4.2. No.2
    3. 4.3. No.3
    4. 4.4. No.4