Stacks Image 11

Testing Tips

This article is about performing tests on your Swift application. It will be updated from time to time.

Testing frameworks

Xcode has two frameworks to perform tests:

  • XCTest: perform unit tests and integration tests on your business code.
  • XCUITest: perform integration tests on your graphical interface.

To fully test an application you should use both.

XCTest consideration

For the SolvoKu application project I need to perform many test on the algorithms used to generate and solve sudoku grids.

To write those tests I had to find some way to fastly write initialization of objects. I found two ways doing so, both are based on writing a string representation of an object.

Building an object from a string representation

First thing I did was to both:

  • write a function to representant a sudoku grid as a string
  • write a function to build a sudoku grid from a string representation
    "{1456}{26}{156}{137}{134}89{1346}{1247}9{68}75…"

This string representation allow me to represents both values and candidates in the grid.

Building an object from a string representation, second version

The second way I use is to implement the CustomStringConvertible protocol to retrieve a description representing the call to perform to construct an object. Can be used only on a class where properties are immutable after initialization, or on structures.

    class AClass: CustomStringConvertible {
    let prop1: Int

    init(prop1: Int) {
            self.prop1 = prop1
        }

        var description: String {
            return "AClass(prop1: \(prop1))"
        }
    
    }

After manually testing a piece of code I can use this to fastly build a series of tests to ensure non-regression. I use this technique not to build entry parameters, but to check results of my algorithms.