Workaround for unit testing Core Data in Swift

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?

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.

It can be extremely hard to unit test code that relies on complex system frameworks, even for very experienced programmers. I would try to unit test as much other code as you can first.

(also, never put it code that you have to manually change between debug and release versions! It’s inevitable you’ll make a mistake somewhere)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật