在 Cocoapods Development Pod 中加入 Test

當使用 Cocoapods 的 pod lib create YourLib 後,只會得到 Example Project 下的 Test 資料夾,這 Test 是包含 Host App 的,而在 Development Pod 中並不會包含 Test 資料夾。 這有點不方便也不直覺,不過經由設定即可得到以下比較好的檔案結構

Development pods with Tests

如要在 Development Pod 中加入 Test,則需要額外設定 (以CCRegionSelector為例):

CCRegionSelector.podspec 中需要增加這段

s.test_spec 'Tests' do |test_spec|
test_spec.source_files = 'CCRegionSelector/Tests/*.swift'
end

在 Example 中的 Podfile 中也需要增加 :testspecs => ['Tests']

target 'CCRegionSelector_Example' do
pod 'CCRegionSelector', :path => '../', :testspecs => ['Tests']
end

這邊要注意的是必須先在 Tests 資料夾內先隨便新增一個檔案,不然會得到以下錯誤… 有點雷

Unable to install the CCRegionSelector pod, because the CCRegionSelector-Unit-Tests target in Xcode would have no sources to compile.

最後在 Example 資料夾內跑完 pod install 後如果沒看到新的 scheme,則在 Xcode 中 manage scheme 內將 CCRegionSelector-Unit-Tests show 打勾就完成了

add_unit_test_scheme

不過這樣即使還沒有 Host App,但跑測試還是會默默需要模擬器而無法更快測試,需要再看看有沒有解法可以加速

在 CocoaPods 1.4 時支援設定是否需不需要 app host

s.test_spec 'Tests' do |test_spec|
test_spec.requires_app_host = true
test_spec.source_files = 'Tests/*.{h,m}'
end

在 CocoaPods 1.8 時支援 UITest bundle

s.test_spec 'UITests' do |test_spec|
test_spec.requires_app_host = true
test_spec.test_type = :ui
test_spec.source_files = 'UITests/**/*.swift'
end

Ref:

  1. Testing with CocoaPods
  2. Support Custom Configuration in Local Pod
  3. CocoaPods 1.8
文章目錄
|