因为有时候测试的需要,想要改变主代码里的某些全局变量,这时候就想要一个 testing 类似 debug 的宏就解决了不是。
updated,自己感觉没有用起来,然后把代码放到setup方法里规避掉了。
xcode code coverage json 报告
xcode 自己的代码覆盖率集成在 xcode 上,如何取得文字版的报告呢。
如下,每次 command + u
后都会生成一个 *.xcresult
在 build log 里可以找到路径,然后执行如下命令生成。
xcrun xccov view --report --json xx.xcresult > ~/Desktop/test.json
xcode test plans
解决多个target测试的问题(比如unittest 和 uiunittest),这样覆盖率可以计算到一起了。
单元测试宏方法
方法一,Xcode Conditional Compilation for Unit-Test Targets
借由这个机会熟悉下xcode相关的定制。
When writing unit test cases, you need to distinguish between real use and test runs in your code from time to time. For example, you might want to send all requests to a test server, instead of the production server.
One possibility to do this is to introduce a boolean property to the class, which you use in all methods that need to differ for testing:
-(NSString *)serverUrl |
That’s what I’ve been doing until now, but I don’t like this approach. In order to be able to set the property in the test case, it has to be publicly writeable, which gives rise to potential subtle bugs in case it’s incorrectly set. A better solution would be to define a separate testing interface with a category. Still, you’d have an extra state in your class especially for the tests.全局变量的风险有点高
Preprocessor-Macros
Originally, I tried to use a preprocessor statement for this purpose:
-(NSString *)serverUrl |
I just could not figure out how to configure Xcode such that TESTING had the correct value for both, normal compilation and unit tests, though. I’ve asked this question on stackoverflow, but to no avail.
If you want to know what the difficulties were, checkout my question on stackoverflow.
Basically, it comes down to this: The unit-test target has the whole app as a target dependency, so if you define a preprocessor macro for the unit-test target, it has no effect on the source files that are compiled in the app target.参考我试的第二种方式,就是这样,不起作用
I was watching WWDC Session 401 (Xcode Core Concepts) today, and when they talked about schemes, it dawned on me that this could be the solution to my problem.
Schemes to the Rescue
So here’s how you set up Xcode:
Add a new configuration named ‘Testing’: Go to the project settings, make sure to select the project (and not a target yet). Open up the Info-pane and click the ‘+’
Add a preprocessor macro to the build settings: Now go to the Build Target, open up the Build Settings and find the Preprocessor Macros option. Add the define TESTING=0 for the configurations Debug and Release and TESTING=1 for the configuration Testing
Edit the test-scheme to use testing as build configuration: Open up the Edit Schemes view, select the Test scheme and choose the Info tab. As Build Configuration choose the new Testing configuration that you created in step 1.(updated,这一步好像可以不用设置,自己设置的以后,测试报错了,一直提示Could not find test host for xxx: 但是明明测试target已经钩上main target了)
I’ve provided a simple sample project on github that demonstrates the complete set-up.
方法二,建立了unittest的target的pch文件
不行。
在pch文件里定义类似 UNITTEST,然后在主target使用。
这个方案想想简单,但是实际用起来没有效果,每次都用的 UNITTEST 相关的值,主target虽然没有直接定义 UNITTEST 但是还是会走这个判断。
参考
Managing iOS Build Configurations and Schemes for XCTest
Xcode Conditional Compilation for Unit-Test Targets
Faster App Setup For Unit Tests