Skip to content

nazmulkp/Swift-Testing-Cheat-Sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

Swift Testing Cheat Sheet

A comprehensive guide to help you get started with Swift Testing. This cheat sheet covers basic syntax, assertions, setup and teardown, parameterized tests, asynchronous testing, performance testing, grouping tests, avoiding redundancies, and a comparison with XCTest.

Table of Contents

  1. Setting Up Swift Tests
  2. Basic Syntax
  3. Assertions
  4. Setup and Teardown
  5. Parameterized Tests
  6. Asynchronous Testing
  7. Performance Testing
  8. Grouping Tests
  9. Avoiding Redundancies
  10. Comparison with XCTest
  11. Migrating from XCTest to Swift Testing

0. Setting Up Swift Tests

Create a Swift Test Target:

// Open your project in Xcode.
// Go to File > New > Target.
// Choose "Unit Testing Bundle".
// Name your test target (e.g., MyProjectTests).

1. Basic Syntax

Define a Test Suite:

@Suite
struct MyTestSuite {
    // Test cases go here
}

Define a Test Case:

@Test
func myTestCase() throws {
    // Test logic goes here
    expect(1 + 1 == 2)
}

2. Assertions

Basic Assertions:

expect(x == y)  // Check for equality
expect(x != y)  // Check for inequality
expect(x > y)   // Check greater than
expect(x >= y)  // Check greater than or equal
expect(x < y)   // Check less than
expect(x <= y)  // Check less than or equal

Nil Checks:

expect(x == nil)    // Check if nil
expect(x != nil)    // Check if not nil

Boolean Checks:

expect(condition)  // Check if true

3. Setup and Teardown

Setup before each test:

@Suite
struct MyTestSuite {
    init() async throws {
        // Setup code
    }

    @Test
    func myTestCase() throws {
        // Test logic
    }
}

Teardown after each test::

@Suite
struct MyTestSuite {
    deinit {
        // Teardown code
    }

    @Test
    func myTestCase() throws {
        // Test logic
    }
}

4. Parameterized Tests

Define Parameterized Test:

@ParameterizedTest
@Test
func addition(_ a: Int, _ b: Int, _ expected: Int) {
    expect(a + b == expected)
}

// Calling the parameterized test
addition(1, 1, 2)
addition(2, 2, 4)

5. Asynchronous Testing

:

@Test
func testAsyncFunction() async throws {
    let result = try await asyncFunction()
    expect(result == expectedValue)
}

Using #require for Immediate Assertion Failure:

@Test
func myTestCase() throws {
    try #require(x == y)
    #expect(z.isEnabled)
}

6. Performance Testing

Measure Performance:

@PerformanceTest
func testPerformance() throws {
    measure {
        // Code to measure
    }
}

7. Grouping Tests

Nested Test Suites:

@Suite
struct ParentSuite {
    @Suite
    struct ChildSuite {
        @Test
        func childTest() throws {
            expect(true)
        }
    }

    @Test
    func parentTest() throws {
        expect(true)
    }
}

8. Avoiding Redundancies

Remove Redundant test Prefix:

@Test
func example() throws {
    expect(true)
}

9. Comparison with XCTest

Screenshot 2024-06-11 at 6 51 18 PM

10. Migrating from XCTest to Swift Testing

Supported Functionality:

Use UI automation APIs (e.g., XCUIApplication). Use performance testing APIs (e.g., XCTMetric). Continue using Objective-C only tests with XCTest. Avoid using XCTAssert(...) from Swift Testing tests, use #expect(...) instead.

Recommended Practices:

Share a single unit test target. Swift Testing tests can coexist with XCTest. Consolidate similar XCTest cases into parameterized tests. Migrate XCTest classes with a single test method to a global @Test function. Remove redundant test prefixes from method names.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published