Carthage-built frameworks in test targets

In this lesson

In this lesson, I will show you what you need to do in order to run your test targets that reference Carthage-built frameworks.


Tap on time to skip ahead


Hello everyone. In this lesson, I’m going to help you solve one more problem with Carthage. That is, how we reference frameworks from Carthage in our tests. The first thing I want to do is try and import one of our testing frameworks. You’ll notice Nimble, which was in the Cartfile.private, is not found. That’s not surprising. We did not import it and I’m going to show you how to do that.


In order to do that, I’m going to go to the test target of where I want to reference these new libraries. It’s saying Add Frameworks and Libraries here and I don’t have any listed for this target. I’m going to go ahead and navigate, just like I did on the main bundle, and find Quick and Nimble. I’m also going to add this test framework here which I’m not actually going to use at the moment. But there’s Quick and there’s Nimble. I’m going to add those three. I can repeat the same for my UI test target. Let’s go ahead and do that so they’re available to both test targets. I’ll do just Quick and Nimble for these. Now I’ve added the frameworks to both test targets and we should be able to now import them.


Let’s head back over and see if we can import that. Nimble, and there it is, it’s found. We were able to successfully reference our test frameworks and our test bundles or our test targets. The next thing we want to try to do is run this test and you’ll see we have one final problem that I’m going to help you solve.


Let’s go ahead and try to run this test. I’m just going to click the test run button here in Xcode. It’s going to compile. Let me launch and show the simulator so we can see what’s happening. It’s going to try to run my tests but what you’re going to see here is that the test actually failed. That’s very odd because there’s no code in the test. Let me look at the console to see what may have happened here. Expand this. There’s all kinds of stuff here but we’re going to zone in on one thing. This should look familiar to you.


“Library not loaded”. It couldn’t find the Nimble framework. Even though we are linking against it, we have to take one more step for our test targets to make sure that any frameworks from Carthage are going to be available when we run our tests on the simulator or the device.


Now that we’ve done that, let’s go ahead and run our tests and see if that solved our problem. Hopefully it did. It’s going to build again. Copy those frameworks to the frameworks folder. Let me open the simulator here. The build succeeded. It’s going to launch the app on the simulator, run our tests, and it says, hopefully, “Test Succeeded.” Yes, test succeeded and we can see that that solved our problem. Here’s the test log. Everything worked.


That pretty much sums up how to use Carthage in your production bundles and your test bundles. Have fun using Carthage.