Unit-testing – Workaround for unit testing Core Data in Swift

iosswift-languageunit testingxcode

I am still pretty new to programming, but my first app was recently approved and is now for sale on the App Store. My app uses Core Data and is written in Swift. After some initial difficulties, I decide to write the app without unit tests. Now I'd like to implement unit tests to prevent regression.

My managed objects are created inside class methods in my NSManagedObject subclasses and use a global variable "context" which I declare in AppDelegate to store the NSManagedObjectContext created in Apple's default code. I know that global variables are generally discouraged but this approach makes the most sense to me, is easy to write, and hasn't let to any bugs or other issues inside the app itself. Unfortunately, this makes it difficult to unit test. I've tried several different approaches to creating a Core Data stack inside my test target, but I can't seem to get anything to work without rewriting lots of my app code. I really don't want to do that just to make it testable. I've considered using frameworks such as Quick/Nimble or Magical Record but I don't see how that would help my problem.

I did figure out a workaround, but I'm curious if people think this is a bad idea: I created a class in my primary target (non testing) called TestingClass. In my first ViewController's viewDidAppear, I call the startTests method. TestingClass is written in strait Swift with no testing frameworks. I have several if statements which are supposed to be false. If they are true they add a string to an array. When the tests are complete, if the array count is > 0, it prints out the contents and aborts.

I am planning on using this until my update is ready to ship. When it is, I will disable the TestingClass and remove my call to it. I'm curious if anyone has ever done anything like this. Should I just figure out a way to do tests the "right" way?

Best Answer

Let's look at the broader question first. In some ways, your question boils down to the balance of pragmatism versus the theoretical "best way" for coding. I'm in the pragmatic programming camp, especially with your particular situation. Pragmatic coding means we work around the limitations that the current software stack imposes.

In your particular case, with your particular understanding of the stack, you're running into a challenge. You've identified a work-around and you've made things work for the time being. Part of those challenges are possibly introduced by your use of global variables, but as you stated, it made sense for the information that needed to be stored.

I think using a TestingClass is a perfectly valid approach since you're fulfilling the purpose of testing (making sure things operate as expected) and you're also working within the constraints, as you understand them, of the application you created. You certainly wouldn't be the first to put a test harness in place during development and then remove it prior to releasing the app.

You do run some risks with that approach. Specifically, if you forget to disable that class before releasing to production, that could create problems for you. Likewise, you effectively have to duplicate all of your code between the main path and the testing path. So you run a risk of a bug escaping if the testing path doesn't exactly reflect how the main path operates. Finally, it does build up complex test cases which can be more difficult to debug because you have to unwind things to understand where it broke.


So, as far as the particular approach that you've taken - it's valid enough.

It may not be "pure" in the sense of following traditional, automated unit testing, but I think that pragmatism needs to rule the day. People don't buy your application because of the unit tests, they buy your app because it works. Your approach to unit testing provides regression testing and allows you to keep focused on developing new features.

Related Topic