Sharing UITests via Cocoapods

Integration Testing

Writing tests for any piece of code is important for maintaining the quality of software which brings more confidence to product delivery. In today’s world most of the code are modularized and bundled to a library/pod making it easier to share, reuse across, managing deployment and release activities etc.. Writing unit tests for modularized code could be achieved comfortably along side with feature development by adopting techniques like TDD. However, the main challenge is how to share integration/ui tests to hosting apps for modularized code. Luckily, there’s a way to do it with “test-specs” in Podfile via Cocoapods.

Cocoapods can extend the capability of providing UI test specifications, a convenient way of shipping UI tests along with its sources to any of the hosting apps, so that app’s consuming this library no longer need to write any UI tests for the features delivered.

Follow below steps for setup

  1. Add below code in the podspec file 
Pod::Spec.new do |spec|
  spec.name         = 'SampleLibrary'
  spec.version      = '1.0'
  spec.test_spec 'Tests' do |test_spec|
     test_spec.requires_app_host = true
     test_spec.test_type = :ui
     test_spec.source_files = 'SampleApp/SampleApp-UITests/SampleTest.swift'
end

2. Bring the test specification to Podfile, tie it with :testspecs, the name should match with the one added in podspec file

target 'SampleApp' do
  use_frameworks!
  pod 'SampleLibrary', '~> 1.0', :testspecs => ['Tests'] 
end

3. Perform pod install , examine Pods.xcodeproj , a new target for UI test will be generated with name SampleLibrary-UI-Tests, if you notice test target name would be set to AppHost-SampleLibrary-UI-Tests in Build Settings like below.

No alt text provided for this image

4. Next, we need to set the update the TEST_TARGET_NAME to “SampleApp” to execute the UI tests shared as part of :testspec, in order to achieve this, add post_install step in podfile to update the build setting configuration and then perform “ pod install” which regenerates the Pods.xcodeproj with proper target setting.

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
       if target.name == "SampleLibrary-UI-Tests"
           config.build_settings['TEST_TARGET_NAME'] = "SampleApp"
       end
     end
   end
end

5. To execute tests, first enable the scheme by pressing “Command + 6” -> Right Click on “SampleLibrary-UI-Tests” -> Enable “SampleLibrary-UI-Tests”, once it is enabled click on play button.

No alt text provided for this image


要查看或添加评论,请登录

社区洞察

其他会员也浏览了