To test a function in Swift, you can use Xcode's built-in testing framework called XCTest.
First, create a new test target from File > New > Target > Test Case.
In the newly created file, import the module that contains the function you want to test.
main.swift26 chars2 lines
main.swift263 chars11 lines
Use XCTest assertions to check whether the function returns the correct output. There are several types of assertions available, such as XCTAssertEqual, XCTAssertTrue, and XCTAssertFalse.
Run the test by clicking the diamond icon next to the test method. The icon will turn green or red depending on whether the test passed or failed, respectively.
By writing tests for your functions, you can use Test-Driven Development (TDD) to ensure your code does what it's supposed to do and avoid regressions when you refactor or extend it in the future.
gistlibby LogSnag