Weston
Weston

Reputation: 1491

How do you Unit Test CocoaPod Code?

I am trying to bring the magic of Cocoapods to my company, but it has hit a tiny snag. We need to be able to unit test code that comes from Cocoapods.

So, my question is, has anyone else encountered this in their company (or is anyone just paranoid enough to care about unit testing this code and figured out how?). It may not be a deal breaker for us, but it will definitely help smooth things over with management if I have this sorted ahead of time.

We could probably insert the tests in the client app, but thats an awful lot of manual work considering we would use pods to reduce manual work. It would be nice to do it once somehow.

Upvotes: 1

Views: 786

Answers (3)

epologee
epologee

Reputation: 11319

I use Cocoapods to centralize the core functionality of a whole suite of over 20 apps, that are whitelabeled version of the same 'base' app. For this, I've setup a structure of two Xcode projects.

The first contains the core functionality including the unit tests verifying the expected behavior (Kiwi in my case). The specs as well as the xcodeproj don't show up in the podspec, but the project is tested under CI (Jenkins).

The second project contains the whitelabeled app (one project per app), and it has the dependency to the first project setup via the Podfile. In this light weight app are also Kiwi specs, that only test any custom code for that specific app. It does not test the core classes anymore, because they are already covered. This project is also under CI.

So the main answer-to-your-question part is that your library does not merely contain the source files necessary for the depending project, but also a compiling Xcode project (app or library) that runs the unit tests.

Upvotes: 0

Fabio
Fabio

Reputation: 3516

If you would like to use a setup similar to the one created by Xcode (where the testing target depends on the one under test), another alternative is:

pod 'ObjectiveSugar', '~> 0.5'

target :test, :exclusive => true do
  pod 'OCMock', '~> 2.0.1'
end

Marking a target exclusive indicates that it shouldn't inherit the dependencies of the parent. This prevents the duplicate symbol error with this setup.

Upvotes: 1

Keith Smiley
Keith Smiley

Reputation: 63984

In my Podfile I use something like this: link_with ['Sail', 'Sail-Tests']

This links all my Pods with both my normal target and my test target. Then from my tests I can import and test whatever I want. This may overlap with 'inserting the tests in the client app' though.

Upvotes: 3

Related Questions