3

In tests target -> General -> Testing: set Host Application to None, so that no app gets launched.

But in that case I cannot use Bundle.main.resourcePath and access resources of my main application (in which some command files are included as resources and I need to run them using Process()).

Could anyone suggest a solution?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Annie Dev
  • 282
  • 1
  • 10

2 Answers2

1

Bundle(identifier: "com.something.app") will get you the bundle for a specific bundle ID. The problem is that if your bundle ID changes for your main target this will fail.

You can also try getting the bundle for a specific class:

let bundle = Bundle(for: SomeClass.self)

Where SomeClass is a class in your main bundle.

Kane Cheshire
  • 1,654
  • 17
  • 20
  • I had tried 'Bundle(identifier: mainappbundleid)', but while test target is run, this gives nil, and 'Bundle(for: SomeClass.self)' gives bundle of test target itself. – Annie Dev Jan 22 '18 at 12:11
  • I am still stuck. While test target is run, will the main app bundle load (since host app is set to none) ? may be thats why Bundle(identifier) gives nil ? – Annie Dev Jan 22 '18 at 12:19
-1

Added file to 'Copy Bundle Resources' of Test target also, then used following code to get path for resource , instead of "\(Bundle.main.resourcePath!)/test.command"

let testBundle = Bundle(for: type(of: self)) let path = "\(testBundle.resourcePath!)/test.command""

Annie Dev
  • 282
  • 1
  • 10