13

I want to test the contract of my API so if, for example, an object is created with some parameter to nil an assertion is raised, but if the assertions are disabled (Release config) it simply returns nil.

My test scheme has the build configuration to Debug, so in my API contract tests I check if the creation of an object with some parameters to nil returns a nil object. But the assertion of the constructor is raised before the XCTAssertNil is executed, so my test always fails.

Can I disable the assertions while testing? I tried to add NS_BLOCK_ASSERTIONS to the scheme arguments passed on launch but that doesn't work.

emenegro
  • 6,901
  • 10
  • 45
  • 68
  • have you tried adding an argument to pass at launch, but also check for it in your code.. so at launch pass TESTS=1... in your code surround the assertions in an if statement and check for if(TESTS)... – JDM May 06 '15 at 17:26
  • 2
    @Jay, thanks, I didn't thought that but I prefer not to make this kind of things, if this can be achieved other way the better. – emenegro May 07 '15 at 05:19

2 Answers2

17

Solved, I added a new configuration, duplicated from Debug, called Test.

enter image description here

Then added NS_BLOCK_ASSERTIONS=1 in the build settings preprocessor macros.

enter image description here

Finally, change the test action in the test's scheme.

enter image description here

Now it works :)

emenegro
  • 6,901
  • 10
  • 45
  • 68
  • You can also overwrite the assertion macros for your tests, e.g., letting them throw an exception and than test that an exception was raised. – Sulthan May 07 '15 at 14:13
  • I the case you run into an issue: Unable to load contents of file list: 'Test-input-files.xcfilelist' here we go: https://stackoverflow.com/questions/55505991/xcode-10-2-update-issue-build-system-error-1-unable-to-load-contents-of-file-l – Tai Le Apr 08 '21 at 03:53
0

As explained in your self-answer, it is useful to create a configuration dedicated to unit testing

In Objective-C you'll need to play with NS_BLOCK_ASSERTIONS and the more recent ENABLE_NS_ASSERTIONS indeed.

For Swift assertions, I found out from Apple Developer it depends on the compiler optimization level you chosed.

With -Onone the assertions will be triggered, but not with -O

SWIFT_OPTIMIZATION_LEVEL

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64