I want to do an Application test that parses some json, stores to core data, and reads out some objects.
How can my code know if it's being run as part of a test or normal run? Just some way to know "are we in test target"? Because the app when it fires up now kicks off a bunch of requests to populate my coredata with info from the server. I don't want it to do this during my tests. I want to fire up the App, read HARDCODED json from a file and store this using the same methods as otherwise into coredata, and verify the results.
If someone could explain how to pass specific key-value pairs on a per target basis that can be read from within the app, I would be even more delighted.
There two situations to deal with:
Tests
is selected, and#import
some files for a certain target such asTests
.Target Code for Test Target:
Create a macro in your
ProjectName-Prefix.pch
file as following:and then call it anywhere in the app:
Conditional
#import
:To
#import
certain files whenTests
target is selected, you do need to add aPreprocessor Macro
to yourTest
target and use it as:Here is how you can add a
Preprocessor Macro
:Never mind... figured out that it is in "Schemes" that you set this.
For example if you want TARGET=TEST to be available during Test and TARGET=RUN to show during run, just set that in your Scheme > Environment Variables > Name/Value.
Then from your app you can do:
Using build settings with preprocessor macros DID NOT work for me b/c my test target (for application/integration testing) is dependent on my main (not test) target, so the main target is built first and that's what runs, and you end up with main target preprocessor macros even though you are after the ones defined in the target you ran. If I missed something here someone feel free to explain please.
You can use the below function.
Usually in Unit Test programmers are using mocking classes and functionalities. You can create a class with target membership only for the test target.
Then in the application code you can check if class exist using
NSClassFromString
function (which will returnNil
for target not included in the class's target membership, in this case - non test target.And you can of curse function it
If by "test target" you mean your unit tests (i.e.
Product > Test
or⌘U
), you can add a preprocessor macro to the target and check for that macro in your code. This allows something like the following:To do this, click on your project file in the project navigator, select your test target, click the
Build Settings
tab, search for "macros", double click thePreprocessor Macros
option, and add one!