Most Xcode projects should have some level of test coverage, and any time unit tests are involved it’s nice to have a framework for generating mock objects. However, we don’t want to bloat our production app package with these test frameworks, so it’s nice to link these libraries only with the test target. Configuring this properly can be tricky, and the CocoaPods documentation isn’t very clear on exactly how to accomplish this in the podfile.
The cleanest solution I’ve found to this problem involves specifying the pods for each target individually and using a Ruby function to import the common pods. Here is a sample podfile that accomplishes this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Note that you may run into a few issues with this if you’re adding this to an existing project and the previous podfile did not explicitly specify the non-test target. In that case, you may need to delete the old Pods.xcconfig and libPods.a files then regenerate your workspace with
As of CocoaPods 0.34, there’s a cleaner solution built-in:
1 2 3 4 5 6 7 8 9