1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| SPEC_END
``` ##### 3. XCTest 开发步骤 Xcode 自带的测试系统是 `XCTest` ,使用简单。开发步骤如下 在 `Tests` 目录下为被测的类创建一个继承自 `XCTestCase` 的测试类。 删除新建的测试代码模版里面的无用方法 `- (void)testPerformanceExample` 、 `- (void)testExample` 。 跟普通类一样,可以继承,可以写私有属性、私有方法。所以可以在新建的类里面,根据需求写一些私有属性等 在 `- (void)setUp` 方法里面写一些初始化、启动设置相关的代码。比如测试数据库功能的时候,写一些数据库连接池相关代码 为被测类里面的每个方法写测试方法。被测类里面可能是 n 个方法,测试类里面可能是 m 个方法(m >= n),根据我们在第三部分:单元测试编码规范里讲过的 一个测试用例只测试一个分支,方法内部有 if、switch 语句时,需要为每个分支写测试用例 为测试类每个方法写的测试方法有一定的规范。命名必须是 `test+被测方法名` 。函数无参数、无返回值。比如 `- (void)testSharedInstance` 。 测试方法里面的代码按照 `Given->When->Then` 的顺序展开。测试环境所需的先决条件准备;调用所要测试的某个方法、函数;使用断言验证输出和行为是否符合预期。 在 `- (void)tearDown` 方法里面写一些释放掉资源或者关闭的代码。比如测试数据库功能的时候,写一些数据库连接池关闭的代码 断言相关宏 /*! * @function XCTFail(...) * Generates a failure unconditionally. * @param ... An optional supplementary description of the failure. A literal NSString, optionally with string format specifiers. This parameter can be completely omitted. */ #define XCTFail(...) \ _XCTPrimitiveFail(self, __VA_ARGS__)
/*! * @define XCTAssertNil(expression, ...) * Generates a failure when ((\a expression) != nil). * @param expression An expression of id type. */ #define XCTAssertNil(expression, ...) \ _XCTPrimitiveAssertNil(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssertNotNil(expression, ...) * Generates a failure when ((\a expression) == nil). */ #define XCTAssertNotNil(expression, ...) \ _XCTPrimitiveAssertNotNil(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssert(expression, ...) * Generates a failure when ((\a expression) == false). * @param expression An expression of boolean type. */ #define XCTAssert(expression, ...) \ _XCTPrimitiveAssertTrue(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssertTrue(expression, ...) */ #define XCTAssertTrue(expression, ...) \
/*! * @define XCTAssertFalse(expression, ...) * Generates a failure when ((\a expression) != false). */ #define XCTAssertFalse(expression, ...) \ _XCTPrimitiveAssertFalse(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssertEqualObjects(expression1, expression2, ...) * Generates a failure when ((\a expression1) not equal to (\a expression2)). * @param expression1 An expression of id type. * @param expression2 An expression of id type. */ #define XCTAssertEqualObjects(expression1, expression2, ...) \ _XCTPrimitiveAssertEqualObjects(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertNotEqualObjects(expression1, expression2, ...) * Generates a failure when ((\a expression1) equal to (\a expression2)). */ #define XCTAssertNotEqualObjects(expression1, expression2, ...) \ _XCTPrimitiveAssertNotEqualObjects(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertEqual(expression1, expression2, ...) * Generates a failure when ((\a expression1) != (\a expression2)). * @param expression1 An expression of C scalar type. * @param expression2 An expression of C scalar type. */ #define XCTAssertEqual(expression1, expression2, ...) \ _XCTPrimitiveAssertEqual(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertNotEqual(expression1, expression2, ...) * Generates a failure when ((\a expression1) == (\a expression2)). */ #define XCTAssertNotEqual(expression1, expression2, ...) \ _XCTPrimitiveAssertNotEqual(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, ...) * Generates a failure when (difference between (\a expression1) and (\a expression2) is > (\a accuracy))). * @param accuracy An expression of C scalar type describing the maximum difference between \a expression1 and \a expression2 for these values to be considered equal. */ #define XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, ...) \ _XCTPrimitiveAssertEqualWithAccuracy(self, expression1, @#expression1, expression2, @#expression2, accuracy, @#accuracy, __VA_ARGS__)
/*! * @define XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, ...) * Generates a failure when (difference between (\a expression1) and (\a expression2) is <= (\a accuracy)). */ #define XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, ...) \ _XCTPrimitiveAssertNotEqualWithAccuracy(self, expression1, @#expression1, expression2, @#expression2, accuracy, @#accuracy, __VA_ARGS__)
/*! * @define XCTAssertGreaterThan(expression1, expression2, ...) * Generates a failure when ((\a expression1) <= (\a expression2)). */ #define XCTAssertGreaterThan(expression1, expression2, ...) \ _XCTPrimitiveAssertGreaterThan(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertGreaterThanOrEqual(expression1, expression2, ...) * Generates a failure when ((\a expression1) < (\a expression2)). */ #define XCTAssertGreaterThanOrEqual(expression1, expression2, ...) \ _XCTPrimitiveAssertGreaterThanOrEqual(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertLessThan(expression1, expression2, ...) * Generates a failure when ((\a expression1) >= (\a expression2)). */ #define XCTAssertLessThan(expression1, expression2, ...) \ _XCTPrimitiveAssertLessThan(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertLessThanOrEqual(expression1, expression2, ...) * Generates a failure when ((\a expression1) > (\a expression2)). */ #define XCTAssertLessThanOrEqual(expression1, expression2, ...) \ _XCTPrimitiveAssertLessThanOrEqual(self, expression1, @#expression1, expression2, @#expression2, __VA_ARGS__)
/*! * @define XCTAssertThrows(expression, ...) * Generates a failure when ((\a expression) does not throw). * @param expression An expression. */ #define XCTAssertThrows(expression, ...) \ _XCTPrimitiveAssertThrows(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssertThrowsSpecific(expression, exception_class, ...) * Generates a failure when ((\a expression) does not throw \a exception_class). * @param exception_class The class of the exception. Must be NSException, or a subclass of NSException. */ #define XCTAssertThrowsSpecific(expression, exception_class, ...) \ _XCTPrimitiveAssertThrowsSpecific(self, expression, @#expression, exception_class, __VA_ARGS__)
/*! * @define XCTAssertThrowsSpecificNamed(expression, exception_class, exception_name, ...) * Generates a failure when ((\a expression) does not throw \a exception_class with \a exception_name). * @param exception_name The name of the exception. */ #define XCTAssertThrowsSpecificNamed(expression, exception_class, exception_name, ...) \ _XCTPrimitiveAssertThrowsSpecificNamed(self, expression, @#expression, exception_class, exception_name, __VA_ARGS__)
/*! * @define XCTAssertNoThrow(expression, ...) * Generates a failure when ((\a expression) throws). */ #define XCTAssertNoThrow(expression, ...) \ _XCTPrimitiveAssertNoThrow(self, expression, @#expression, __VA_ARGS__)
/*! * @define XCTAssertNoThrowSpecific(expression, exception_class, ...) * Generates a failure when ((\a expression) throws \a exception_class). */ #define XCTAssertNoThrowSpecific(expression, exception_class, ...) \ _XCTPrimitiveAssertNoThrowSpecific(self, expression, @#expression, exception_class, __VA_ARGS__)
/*! * @define XCTAssertNoThrowSpecificNamed(expression, exception_class, exception_name, ...) * Generates a failure when ((\a expression) throws \a exception_class with \a exception_name). */ #define XCTAssertNoThrowSpecificNamed(expression, exception_class, exception_name, ...) \ _XCTPrimitiveAssertNoThrowSpecificNamed(self, expression, @#expression, exception_class, exception_name, __VA_ARGS__)
``` 经验小结 XCTestCase 类和其他类一样,你可以定义基类,这里面封装一些常用的方法。 ```java
|