When you create an XCode 4 project, it asks you if you want to include unit testing. But how do you add it to a project that's been created without them?
I started a project with Unit Tests to try to figure it out, and it looks like the only difference between it and my existing project are the Test.h and Test.m files in the new project.
But when I move them into my old project and try to build, it says no such file or directory: SenTestingKit/SenTestingKit.h. That looks like a framework to me, but if I go to build phases and try to add the framework, there isn't one available called SenTestingKit :( And the new project only links the usual suspects: UIKit, CoreGraphics, and Foundation, but no SenTestingKit.
You can be a very productive programmer by "conventional" standards and still struggle to write unit tests in a way that scales in a larger project. Also, and specifically for your situation, adding unit tests to an existing code base that has no tests is also a specialized skill in itself.
To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.
To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.
Answer updated for Xcode 10.2.
Glad you asked this question — it's never too late to add tests to a project!
It's that simple. Xcode will generate a target, some boilerplate files such as Info.plist and an initial test case file for you. Happy testing!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With