Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable some code lines for Unit Test

I have issue with disabling some code lines for testing purpose.

I have 2 targets - 1st my Product, 2nd my KIF test target. In KIF test target, macro is defined as KIF=1 In Product target, macro is defined as KIF=0

Then code was included in following:

#ifndef KIF
.... some code.....
#endif

Unfortunately, this code doesn't work for any target.

Maybe it happens because Product target is set in target dependency for KIF target?

Could you please help me to solve my issue?

like image 840
user2738882 Avatar asked Nov 24 '25 13:11

user2738882


2 Answers

It was more easy than i can imagine. Go to your Project > Configurations and create new configuration via tapping on plus '+' button > Duplicate Debug Configuration, then you can create new variable like TESTING and define it only for your configuration. The next step is include in #if your code and change configuration in your scheme.

#if TESTING
{
...some code for testing needs...
}
#else
{
...your regular code...
}
#endif

In Product > Scheme > Edit Scheme > Test, change Build Configuration to your own one and test it.

like image 90
user2738882 Avatar answered Nov 26 '25 03:11

user2738882


That works for me for UnitTest as prebuild preprocessor macro.

#ifdef TEST
#endif

If you want a custom variables for custom builds do that:

  1. Go to Target / Build Settings / Preprocessor Macros
  2. Add desired variable: MYTESTVARIABLE
  3. Now in code you can use that

.

#ifdef MYTESTVARIABLE 
//some code here
#endif
like image 34
Grzegorz Krukowski Avatar answered Nov 26 '25 03:11

Grzegorz Krukowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!