0
0
iOS Swiftmobile~20 mins

XCTest framework in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Simple Calculator Tests
Create unit tests for a simple calculator class using XCTest framework to verify addition and subtraction functions work correctly.
Target UI
-------------------------
| Simple Calculator Tests |
-------------------------
| Test Addition          |
| Test Subtraction       |
-------------------------
Create a Calculator class with add and subtract methods.
Write XCTest unit tests to check if add and subtract return correct results.
Use XCTAssertEqual to compare expected and actual results.
Include setup and teardown methods in the test class.
Starter Code
iOS Swift
import XCTest
@testable import YourAppModule

class Calculator {
    func add(_ a: Int, _ b: Int) -> Int {
        // TODO: Implement addition
        return 0
    }
    func subtract(_ a: Int, _ b: Int) -> Int {
        // TODO: Implement subtraction
        return 0
    }
}

class CalculatorTests: XCTestCase {
    var calculator: Calculator!

    override func setUp() {
        super.setUp()
        // TODO: Initialize calculator
    }

    override func tearDown() {
        // TODO: Deinitialize calculator
        super.tearDown()
    }

    func testAddition() {
        // TODO: Test add method
    }

    func testSubtraction() {
        // TODO: Test subtract method
    }
}
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
iOS Swift
import XCTest
@testable import YourAppModule

class Calculator {
    func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
    func subtract(_ a: Int, _ b: Int) -> Int {
        return a - b
    }
}

class CalculatorTests: XCTestCase {
    var calculator: Calculator!

    override func setUp() {
        super.setUp()
        calculator = Calculator()
    }

    override func tearDown() {
        calculator = nil
        super.tearDown()
    }

    func testAddition() {
        let result = calculator.add(2, 3)
        XCTAssertEqual(result, 5, "Addition should return the sum of two numbers")
    }

    func testSubtraction() {
        let result = calculator.subtract(5, 3)
        XCTAssertEqual(result, 2, "Subtraction should return the difference of two numbers")
    }
}

This solution creates a simple Calculator class with two methods: add and subtract. Each method returns the correct arithmetic result.

The CalculatorTests class inherits from XCTestCase. It uses setUp() to create a new calculator instance before each test and tearDown() to clean up after tests.

Two test methods check the calculator's behavior: testAddition verifies that adding 2 and 3 returns 5, and testSubtraction verifies that subtracting 3 from 5 returns 2. The XCTAssertEqual function compares expected and actual results, showing a message if the test fails.

This approach helps ensure the calculator works correctly and teaches how to write basic unit tests with XCTest.

Final Result
Completed Screen
-------------------------
| Simple Calculator Tests |
-------------------------
| [check] Test Addition Passed  |
| [check] Test Subtraction Passed|
-------------------------
Run tests in Xcode test navigator or command line.
See green checkmarks if tests pass.
See error messages if tests fail.
Stretch Goal
Add a test for multiplication method in Calculator and implement it.
💡 Hint
Create a multiply(_:_:) method in Calculator and write testMultiplication() using XCTAssertEqual.