Join Ron Buencamino for an in-depth discussion in this video Unit testing basics, part of Automated Testing in Xcode.
- [Instructor] Unit testing inside of Xcode has been around since Xcode-5. It's performed using the XCTest framework, which is Apple's framework that allows you to create and run unit tests and performance tests. As of Xcode-7 it's been extended to allow you to perform UI tests against your project. Test assert that certain conditions are met as a result of your testing. In Xcode failures can be recorded if conditions are not satisfied.
The primary classes that you're going to be working with in your testing schemes are XCTest, XCTestCase, and XCTAssert. With XCTest this is your abstract class for creating, managing, and executing tests. You can also measure performance of blocks of code using this class. As an added bonus, you can interact with user interfaces to allow the validation of user flows.
XCTestCase is your primary class for defining test cases, methods, and performance tests. For assertions, your primary class is going to be XCTAssert. This is your primary assertion class, however it can assert a number of instances, such as booleans, nil or non-nil assertions, equality, comparables, and error assertions. For asynchronous assertions you can use XCTestExpectation.
What this is is an expectation that hopes to be fulfilled once an asynchronous task is completed. You can wait for these exceptions using XCTWaiter. Even more, you can do asynchronous testing not only on URL connections, but also key value observation and notifications as well. Other bonuses that you get for using unit testing inside of Xcode is that Xcode gives you the ability to see code coverage for your project.
You could see just how much of your code is actually being tested. Also, it's a familiar IDE, so debugging tests within Xcode is the same as debugging your application code. You can set breakpoints to catch failures. It should also be noted that you can run unit tests to both your application code, as well as the dynamic framework libraries that you may have created.
- What are unit and UI tests?
- Unit tests in Xcode
- Creating test targets
- Running tests
- Debugging tests
- Writing UI tests
- Code coverage