Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic "test host" or bundle loader for iOS Unit Testing?

How do I make the test host/bundle loader dynamic based on the current scheme? Right now the value is set to:

$(BUILT_PRODUCTS_DIR)/MyApp1.app/MyApp1

The problem is I have 4 apps in the workspace and I would like to use the same unit testing suite for all of them. How do I dynamically change the "MyApp1" part based on the current scheme? Is it an environment variable based during build? I tried setting it to things like $(PROJECT_NAME) but those seem to get the name of the test suite.

like image 885
Luke The Obscure Avatar asked Jul 15 '13 16:07

Luke The Obscure


People also ask

What is unit testing in iOS?

A unit test is a function you write that tests something about your app. A good unit test is small. It tests just one thing in isolation. For example, if your app adds up the total amount of time your user spent doing something, you might write a test to check if this total is correct.

What is difference between unit tests and UI test in Xcode?

The really short version is that unit tests have access to the code in your app (or whatever kind of module you are building) and UI tests do not have access to the code. A unit test only tests one single class per test.

How do I run unit test cases in Xcode?

⌘U will build and run all your test cases. It is the most commonly used shortcut when creating unit test cases. It is equivalent to ⌘R (build & run) while doing app development. You can use this shortcut to build your test target and run all the test cases in your test target.

How do I run XCTest in Xcode?

To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.


1 Answers

To do this you need to have a variable inside the build settings - which seems simple, but it's not. If you set an environment variable through a Pre- or Post- step in the application or test scheme, it does not seem as though it will be picked up here. The build settings, after all, happen before the build. Same is true of a preprocessor macro, though doing this using xcodebuild and passing in a custom option may work.

The only way I know of to do this is to use an xcconfig file. Create one and apply it to (at the least) your test target. The content should include something like this: THINGUNDERTEST=FooBar

Now in your project settings, wether in an xcconfig or the project file, set BUNDLER_LOADER to: $(BUILT_PRODUCTS_DIR)/$(THINGUNDERTEST).app/$(THINGUNDERTEST)

That will work. Now you can change THINGUNDERTEST through various means and get at least some dynamic behavior. This may work for you or may not, depending on your needs - but it probably only a starting point.

like image 149
quellish Avatar answered Oct 16 '22 09:10

quellish