iOS Testing Tips #1 — Track of failing tests in helper functions

Cristian Madrid
2 min readApr 21, 2018

--

This is the first post of a series of tiny posts with tips that can help you to improve your testing skills. You may check the other posts here:
iOS Testing Tips #2 — Improving the readability of your tests

Using helper function for assertions

Despite being important for code reuse, helper functions for assertions helps us to create more readable tests.

The reason behind using helper functions for this particular test case is not the focus here.

A problem

If you perform an assertion outside a test function, and the test fails, Xcode displays message only where the assertion method was called, as you may see below:

TaxCalculatorTests.swift

This behaviour occurs because the function XCTAssertEqual has two default arguments that specify the file and the line that the test was executed.

It’s not a huge problem for your project, but if you are using TDD (Test-Driven Development), it’s likely that it will slow down your development speed, because every time that more than one error occurs, you will have to debug to find out what happened.

A solution:

To fix it, it is quite simple, all you have to do is pass to the XCTAssert the #file and #line of your test, as you can see below:

TaxCalculatorTests.swift

The #file and #line are literal values which are also useful for logging.

PS: This happens in Nimble too, so you can fix with the same approach.

That's all folks, thanks for reading!

--

--

Cristian Madrid

iOS developer at Globo.com, In love with coffe, quality and design patterns.