This video shows how to install the library using CocoaPods in your Xcode workspace. Learn how to set up the LayoutTest library in your workspace and how to create a unit testing target to run your LayoutTests. If you're already familiar with these tools and have written unit tests before, this video will be a review; however, it is still recommended that you watch this short video.
- [Instructor] In this video, we'll go over how to install Cocoapods and set up your project to use LayoutTest. Even if you're familiar with these concepts, it'll be useful for you to see some of the details of the setup in this video. So let's take a look at Cocoapods.org. As you see, there's a easy spot here to install. All you have to do is copy this text right here, go to your terminal, and paste, and hit Enter. If you encounter any errors while installing Cocoapods, please visit the link shown on the screen at Cocoapods.org for troubleshooting tips.
You'll need to type in your administrator password here, and hit Enter. If you already had Cocoapods installed, you can skip through this section of this video. Great, now Cocoapods is installed. We can check this by using the pod command, and just make sure it works. Now let's go over how to set up LayoutTest in a new project. Afterwards, we will look at the slight differences in setting up LayoutTest on an existing project. So let's go to Xcode.
Let's create a new Xcode project. It doesn't really matter what we choose here, so I'm going to choose an iOS single view application. We'll give it a name, say, MyApp. We can ignore Organization. And we want to make sure we check the box to include Unit Tests, and hit Next. And we'll save it on the desktop. We're not going to have a Git repo for this. As you see, there's a folder for tests called MyAppTests, and Products has the app and the xctest product for the MyAppTests.
You can run all the tests by hitting Command + U, or going to Product, Test, as you see, the shortcut is here. We will enable Developer Mode if you haven't done this already on your Mac. And it will build my app and run all the tests. All files created in this course that are LayoutTests will be in this test folder. You'll need to make sure that every file created is a member of the test target. You can do this by clicking on this file, or any of the files, and checking in the right sidebar here in the Document Inspector.
You can hide or show this Document Inspector or this right sidebar, by clicking this button here. You can also do so by hitting Command + Option + 0. The option we're looking for here is the Target Membership option. As you see, there's a box checked next to MyAppTests, so that means it is part of that target, and that target only, which is what we want. Now we're going to create the dependency on LayoutTest. We're going to close this project, and now we're going to create a pod file in the root directory of the project.
We'll go to the terminal. We're going to clear this by either typing Clear or Control + L. Now, we created this on the desktop, so we're going to change directories into the desktop. We're going to list the files, and MyApp is what we want to go into, and we see here have our Xcode project. Now we're going to create this pod file, which is what Cocoapods uses to define all of your dependencies. We're going to use vim to do this, and we just call it podfile.
It's important that it is named this. In here, we're going to type the following. The platform we're using, which is iOS. You can get more info on how to use the pod file on the Cocoapods website as well. We're going to use frameworks, since we are doing Swift in this project. And we're going to have our target, which we called MyAppTests.
We create a pod LayoutTest, which is the name of it, and it's very important if we're using Swift, that we do /Swift, and end that target. If your project uses Objective-C, then you would change this to not have the /Swift, that would be the only difference. Now we can get out of this, save and quit. Now we're going to run pod install, which will use the dependency we just made, and create an XC workspace, so an Xcode workspace, instead of a project.
The initial time you use pod install, it's going to take a little bit longer because Cocoapods needs to set up the master repository on your computer. Don't worry if it hangs here for a while, this does take some time. Okay, Cocoapods has finished installing itself, and has set up our dependencies. Now, we see that there's some warnings here about always embed Swift standard libraries. At the time of recording, the current versions of Xcode and Cocoapods require you to make one other change if you are working in a Swift project, in order for LayoutTest to work properly.
As you see, these warnings are kind of telling you about it, so we're going to make this change now. We're going to go back to Xcode, go into open our project again. We're going to click on the project itself. I'm going to make sure we select the app test target. Now we click on Build Settings, and we're going to make sure this is on All, and search for Always Embed Swift Standard Libraries.
We're going to click this, hit Other, and change this to the following string, $(inherited). That should make that go away. Let's go to the terminal again, and run pod install one more time just to make sure. And there we go. Now, whenever we're using Cocoapods, we need to make sure to always open the workspace and set up the project. So now we're going to go back to here, close this project, and instead, we're going to open the workspace, and we see that Cocoapods generated a workspace file for us.
We're going to double-click that, instead of the project itself, and you'll see that Cocoapods added a pods project to our workspace. Need to make sure to always open the workspace instead of your project, or else your project will not build properly. As you see here, if we twirl down the Pods directory, we see the LayoutTest files themselves, and we can explore the LayoutTest code if we'd like to. Now, we're going to do one other nice thing for the setup to make it a little easier to work with.
We're going to change the directory where test failure information will be stored. In order to do this, we're going to edit the scheme. To do so, you can click on the target that we're currently targeting, we hit Edit Scheme, we go to Test, we click Arguments, and under Environment Variables we want to click the plus, but in order to do so, we need to make sure to uncheck this box to use the Run actions arguments and environment variables. We're going to hit the plus button now, and we're going to add the following.
All caps LYT_FAILING_SNAPSHOT_DIR, and we're going to give it the following value. You can put anything you want, you can put any directory, but what I'm going to do for convenience is put $(SRCROOT), so that's going to put it in the same directory as my project file. Now we can hit Close, and we're ready to go. If you don't set this, the default value is going to be in the Simulator directories.
LayoutTest will print the directory whenever there is a failure. When your tests fail, information will go in the LayoutTest images folder. You probably want to add this folder to your Get Ignored file, or however your chosen DCS or version control system ignores files you don't want to commit. You are ready to begin writing tests with LayoutTest. Now, let's look at how you can add unit tests to an existing project that doesn't have any. In order to simulate this, we're going to close this project.
We're going to create a new project, File, New Project. We'll pick a single-view application once again, but this time we're going to uncheck the unit tests. We'll also uncheck UI tests just to make it easier to see. We'll call this MyApp2, we'll click Next, and we'll save it once again on the desktop. Now we're going to click on the project in the project navigator on the left here, we're going to click on the drop-down where the target is, and we're going to hit Add Target.
We're going to click on iOS Unit Testing Bundle. You can scroll through here, or you can just filter, and hit Unit. Hit Next. Now we can name this target. Easiest thing is to just do the app name with Tests appended which we will do, and make sure it's in this project, and we are targeting the MyApp2 target to be tested, and hit Finish. Then, you just follow the same procedure for setting up LayoutTest as a dependency with Cocoapods like we did previously, and you're ready to get testing.
Cocoapods is the easiest way to get started and keep up to date with LayoutTest, however, if you don't want to use Cocoapods, you can learn about Git Submodules, or just copy the entire project into a new workspace with your project.
Released
3/9/2017- Installing the library
- Specifying test data
- Reviewing property-based testing
- Using test data and writing the LayoutTest test
- Testing views at different sizes
- Debugging with snapshots
- Dealing with common errors
- Advanced debugging tips
- Exploring catalog view
Share this video
Embed this video
Video: Installing the library