How to exclude NSManagedObject autogenerated classes of Coredata Entity from UnitTest in Xcode?

Viewed 84

I am working on a Swift project's test coverage. The final coverage shows Entity+CoredataProperties classes as not covered. These are autogenerated files of a Core Data entity. Is there any way to exclude these files from test coverage? Or should I try to write test cases for them too?

1 Answers

I did solve this issue. Created an NSInMemoryStoreType store container. Then simply created and fetched the managed objects.

//Create object
 
let _ = entity.init(context: self.coreDataStack.backgroundContext)

try! coreDataStack.backgroundContext.save()

//Fetch object    
let request : NSFetchRequest<ClassEntity> = ClassEntity.fetchRequest() //This will cover the managed object class with fetch request.
             
let items = try! self.coreDataStack.backgroundContext.fetch(request)

Tried this method for all the entity classes. Then got 100% coverage for all the autogenerated entity classes. I am not sure that this is the perfect method somehow it solved my issue.

Related