Swift — 4 — Core Data — Part 4 Writing unit test cases

Hi, i am back with my favorite core data with unit testing  😀 . I hope you have some basic knowledge about writing unit test cases. We will use XCTestCase class and XCTest framework to write unit tests and test them.

Unit tests are very helpful in testing code without having to launch code on device or simulator and go to specific view controller. We can test the code we want very quickly without running on device/simulator. It will save a lot of development time.

I will continue from part 3. Please download part 3 source code from here.

I have added one more method in CoreDataManager class. Just copy below method and paste in CoreDataManager class. This method uses fetch request for fetching Person records and delete them.

Now let’s come to the point  😯

We need to add PersonData.xcdatamodeld in test target also. Please refer attached screen shot & look closely at Target Membership at right side bottom:

Let’s copy below code and paste it in PersonDataTests.swift. It should look like this:

Let’s break above code and understand it:

NOTE : Every test method MUST start with test word.

import XCTest

We will use this framework for unit testing.

import CoreData

We will use CoreData framework for accessing core data specific class.

@testable import PersonData

We will test PersonData target.

PersonDataTests : XCTestCase

We will have to subclass XCTestCase for writing unit test cases.

var coreDataManager: CoreDataManager!Creating a CoreDataManager object, we will use this object to test operations like insert, update & delete.

This is a life cycle method which is called first every time you run a test case. We are initializing coreDataManager object in this method.

This is a life cycle method which is called after every time you run a test case.

This test case test for the proper initialization of CoreDataManager class :). It will failure in case of nil instance. If passes it means CoreDataManager.sharedManager is returning valid instance.

Let’s test and see it visually:

Let’s test test_coreDataStackInitialization

This test case test if NSPersistentContainer(the actual core data stack) initializes successfully or not. In case it fails in getting a proper instance It will generate a failure. If passes it means we are getting valid persistentContainer.

Let’s test and see it visually:

Let’s test test_create_person

In this test case we are inserting person records. Generates a failure when expression == nil. If it passes it means coreDataManager.insertPerson is returning valid person object. If inserts are successful then we must get them in fetch. Let’s try to fetch person records. As we have already save three person records in core data, we should fetch them. We are using XCTAssertEqual.

let’s test and see it visually:

Let’s test fetchAll method of CoreDataManager class.

Let’s test and see it visually:

Lets dig into test_remove_person.

In this test case we are fetching all the persons and deleting first object from returned items.  In XCTAssertEqual we are testing for equality.

Let’s test and see it visually:

Let’s test test_remove_person_ssn

Our app has facility where a person can be deleted by his ssn. Firstly we are fetching all person records. Then we are calling delete(ssn: “789”). This will delete the person with ssn number “789”. In XCTAssertEqual we are testing for equality.

Let’s test and see it visually:

I hope now you should be able to test last unit test case flush data.

Just test it the way i have discussed above.  XCTAssertEqual equality checks for total count to be zero. After calling coreDataManager’s flushData , Person entity should be deleted from core data. That is, fetch will give us 0 results.

Hope you like reading it. Please follow, comment and claps and keep on motivating us. 😀

Here is the final source code :).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.