### Basic XCTest Example Source: https://github.com/skiptools/skip-unit/blob/main/README.md A standard Skip unit test using the XCTest framework. This example demonstrates a simple assertion to verify basic logic. ```swift import XCTest final class MyUnitTests: XCTestCase { func testSomeLogic() throws { XCTAssertEqual(1 + 2, 3, "basic test") } } ``` -------------------------------- ### Implement XCTestCase Interface Source: https://context7.com/skiptools/skip-unit/llms.txt Define a test class inheriting from XCTestCase. Use setUp and tearDown for test fixture setup and cleanup. Implement test methods to contain your test logic and assertions. ```swift import XCTest final class MyUnitTests: XCTestCase { var calculator: Calculator? override func setUp() { calculator = Calculator() } override func tearDown() { calculator = nil } func testAddition() throws { let result = calculator?.add(2, 3) XCTAssertEqual(result, 5, "Addition should return correct sum") } func testDivision() throws { let result = calculator?.divide(10, 2) XCTAssertEqual(result, 5.0, accuracy: 0.001, "Division should be accurate") } } ``` -------------------------------- ### Define Swift Testing Suites and Tests Source: https://github.com/skiptools/skip-unit/blob/main/README.md Use @Suite and @Test to organize tests, with #expect for assertions. ```swift import Testing @Suite struct MathTests { @Test func addition() { #expect(1 + 1 == 2) } @Test func inequality() { #expect(3 != 5) } } ``` -------------------------------- ### Use Require Functions for Preconditions Source: https://context7.com/skiptools/skip-unit/llms.txt Assertion functions for #require() macros, intended for validating preconditions where failure should halt the test. ```swift import Testing @Suite struct RequireTests { @Test func testRequirements() { // Boolean requirements requireTrue(true) // Equality requirements requireEqual(2 * 2, 4) requireNotEqual(1, 2) // Comparison requirements requireGreaterThan(100, 50) requireGreaterThanOrEqual(10, 10) requireLessThan(5, 10) requireLessThanOrEqual(5, 5) // Unwrap requirement (returns unwrapped value) let optional: Int? = 42 let value = requireNotNil(optional, "Value must exist") requireEqual(value, 42) } @Test func testRequireThrows() { requireThrows(throws: ValidationError.self) { throw ValidationError.invalidInput } } } ``` -------------------------------- ### Customize the Test Harness Source: https://github.com/skiptools/skip-unit/blob/main/README.md Override the auto-generated test harness by providing a custom XCSkipTests.swift file. ```swift #if os(macOS) // Skip transpiled tests only run on macOS targets import SkipTest /// This test case will run the transpiled tests for the Skip module. @available(macOS 13, *) final class XCSkipTests: XCTestCase, XCGradleHarness { public func testSkipModule() async throws { try await runGradleTests(device: .none) // set device ID to run in Android emulator vs. Robolectric } } #endif ``` -------------------------------- ### SkipUnit Overview Source: https://context7.com/skiptools/skip-unit/llms.txt SkipUnit is a Gradle plugin module for Skip apps that bridges Swift's XCTest framework to Kotlin's JUnit testing framework. It enables automatic transpilation of XCTest test cases to JUnit tests, allowing developers to write tests once in Swift and run them on both iOS (via XCTest) and Android (via JUnit). The library provides a `skip.unit` Kotlin package containing a Swift XCTest interface that maps to Java/Kotlin JUnit assertions. This enables parity testing—a central aspect of Skip development—which ensures that Swift code and transpiled Kotlin code behave identically across platforms. SkipUnit also supports Swift Testing's `@Test` and `@Suite` annotations with `#expect` and `#require` macros. ```APIDOC ## SkipUnit Overview SkipUnit is a Gradle plugin module for Skip apps that bridges Swift's XCTest framework to Kotlin's JUnit testing framework. It enables automatic transpilation of XCTest test cases to JUnit tests, allowing developers to write tests once in Swift and run them on both iOS (via XCTest) and Android (via JUnit). The library provides a `skip.unit` Kotlin package containing a Swift XCTest interface that maps to Java/Kotlin JUnit assertions. This enables parity testing—a central aspect of Skip development—which ensures that Swift code and transpiled Kotlin code behave identically across platforms. SkipUnit also supports Swift Testing's `@Test` and `@Suite` annotations with `#expect` and `#require` macros. ``` -------------------------------- ### Configure Test Harness for Gradle Source: https://context7.com/skiptools/skip-unit/llms.txt Defines a test harness to drive Gradle test execution for transpiled tests. Custom harnesses allow specifying device targets like Robolectric or physical emulators. ```swift #if os(macOS) // Skip transpiled tests only run on macOS targets import SkipTest /// This test case will run the transpiled tests for the Skip module. @available(macOS 13, *) final class XCSkipTests: XCTestCase, XCGradleHarness { public func testSkipModule() async throws { // Run in Robolectric (simulated Android) try await runGradleTests(device: .none) } } #endif // To run on Android emulator, set device ID: // try await runGradleTests(device: "emulator-5554") ``` -------------------------------- ### Define Tests and Suites with Swift Testing Source: https://context7.com/skiptools/skip-unit/llms.txt Utilize @Test and @Suite attributes alongside #expect and #require macros for modern test definitions. ```swift import Testing // Freestanding test function (auto-wrapped in generated test class) @Test func addition() { #expect(1 + 1 == 2) } @Test func stringComparison() { #expect("swift" != "kotlin") } // Test suite with multiple tests @Suite struct MathTests { @Test func equality() { #expect(1 == 1) #expect(1 != 2) } @Test func comparisons() { #expect(1 < 2) #expect(2 <= 2) #expect(2 >= 2) #expect(3 > 2) } @Test func booleanExpressions() { let value = 10 #expect(value > 5) #expect(value < 20) } } // Class-based suite @Suite class StringTests { @Test func testNotEmpty() { let str = "Hello" #expect(!str.isEmpty) } } ``` -------------------------------- ### Use Expect Functions for Assertions Source: https://context7.com/skiptools/skip-unit/llms.txt Assertion functions that serve as targets for transpiled #expect() macros, covering equality, nil checks, and exception testing. ```swift import Testing @Suite struct ExpectTests { @Test func testExpectations() { // Boolean expectations expectTrue(5 > 3) expectFalse(5 < 3) // Equality expectEqual(1 + 1, 2) expectNotEqual("a", "b") // Comparisons expectGreaterThan(10, 5) expectGreaterThanOrEqual(5, 5) expectLessThan(1, 2) expectLessThanOrEqual(2, 2) // Nil checks let optional: String? = nil expectNil(optional) let value: String? = "test" let unwrapped = expectNotNil(value) } @Test func testExpectThrows() { expectThrows(throws: MyError.self) { throw MyError.somethingWentWrong } } } ``` -------------------------------- ### Use XCTAssertNotEqual for Inequality Checks Source: https://context7.com/skiptools/skip-unit/llms.txt Assert that two values are not equal. This test fails if the provided values are identical. ```swift import XCTest class InequalityTests: XCTestCase { func testInequality() { XCTAssertNotEqual(1, 2) XCTAssertNotEqual("swift", "kotlin", "Languages should differ") XCTAssertNotEqual([1, 2], [1, 2, 3], "Arrays of different sizes") } } ``` -------------------------------- ### Transpiled Kotlin Test Case Structure Source: https://github.com/skiptools/skip-unit/blob/main/README.md Shows the structure of a generated Kotlin test class inheriting from XCTestCase. ```kotlin package app.module.name import skip.lib.* import skip.unit.* internal class MyUnitTests: XCTestCase { @Test internal fun testSomeLogic() = XCTAssertEqual(1 + 2, 3, "basic test") } ``` -------------------------------- ### Set Android Device/Emulator for Tests Source: https://github.com/skiptools/skip-unit/blob/main/README.md Configure the ANDROID_SERIAL environment variable to specify the target device or emulator for running transpiled Kotlin/Gradle tests. This can be set in Xcode scheme arguments or as a command-line environment variable. ```shell > ANDROID_SERIAL=emulator-5554 swift test ``` -------------------------------- ### Perform Comparison Assertions with XCTest Source: https://context7.com/skiptools/skip-unit/llms.txt Use these assertions to verify relationships between comparable values in XCTestCase classes. ```swift import XCTest class ComparisonTests: XCTestCase { func testComparisons() { XCTAssertGreaterThan(10, 5) XCTAssertGreaterThan(3.14, 3.0, "Pi is greater than 3") XCTAssertGreaterThanOrEqual(5, 5) XCTAssertGreaterThanOrEqual(10, 5) XCTAssertLessThan(1, 2) XCTAssertLessThan(-5, 0, "Negative is less than zero") XCTAssertLessThanOrEqual(2, 2) XCTAssertLessThanOrEqual(1, 2) } } ``` -------------------------------- ### XCTestCase Interface Source: https://context7.com/skiptools/skip-unit/llms.txt The `XCTestCase` interface provides the foundation for writing cross-platform unit tests. It adapts Swift's XCTestCase to JUnit 4 with setUp/tearDown lifecycle methods and assertion functions that map to JUnit assertions. ```APIDOC ## XCTestCase Interface The `XCTestCase` interface provides the foundation for writing cross-platform unit tests. It adapts Swift's XCTestCase to JUnit 4 with setUp/tearDown lifecycle methods and assertion functions that map to JUnit assertions. ### Request Example ```swift import XCTest final class MyUnitTests: XCTestCase { var calculator: Calculator? override func setUp() { calculator = Calculator() } override func tearDown() { calculator = nil } func testAddition() throws { let result = calculator?.add(2, 3) XCTAssertEqual(result, 5, "Addition should return correct sum") } func testDivision() throws { let result = calculator?.divide(10, 2) XCTAssertEqual(result, 5.0, accuracy: 0.001, "Division should be accurate") } } ``` ``` -------------------------------- ### Measure Performance with XCTest Source: https://context7.com/skiptools/skip-unit/llms.txt Executes a block for performance measurement within an XCTestCase. In Kotlin transpilation, the block executes normally as measurement is handled by the native framework. ```swift import XCTest class PerformanceTests: XCTestCase { func testPerformance() { measure { // Code to measure var sum = 0 for i in 1...1000 { sum += i } XCTAssertEqual(sum, 500500) } } } ``` -------------------------------- ### Use XCTAssertTrue and XCTAssertFalse for Boolean Conditions Source: https://context7.com/skiptools/skip-unit/llms.txt Validate boolean expressions. XCTAssertTrue passes if the condition is true, while XCTAssertFalse passes if the condition is false. Both support an optional failure message. ```swift import XCTest class BooleanTests: XCTestCase { func testBooleanAssertions() { let isEnabled = true let isEmpty = false XCTAssertTrue(isEnabled) XCTAssertTrue(5 > 3, "5 should be greater than 3") XCTAssertFalse(isEmpty) XCTAssertFalse("hello".isEmpty, "String should not be empty") } } ``` -------------------------------- ### XCTAssertNotEqual Source: https://context7.com/skiptools/skip-unit/llms.txt Asserts that two values are not equal. Returns failure if the values match. ```APIDOC ## XCTAssertNotEqual Asserts that two values are not equal. Returns failure if the values match. ### Request Example ```swift import XCTest class InequalityTests: XCTestCase { func testInequality() { XCTAssertNotEqual(1, 2) XCTAssertNotEqual("swift", "kotlin", "Languages should differ") XCTAssertNotEqual([1, 2], [1, 2, 3], "Arrays of different sizes") } } ``` ``` -------------------------------- ### Implement XCTAssertEqual in Kotlin Source: https://github.com/skiptools/skip-unit/blob/main/README.md Maps the Swift XCTAssertEqual function to the JUnit assertEquals method. ```kotlin fun XCTAssertEqual(a: Any?, b: Any?): Unit = org.junit.Assert.assertEquals(b, a) ``` -------------------------------- ### Use XCTAssertIdentical and XCTAssertNotIdentical for Reference Equality Source: https://context7.com/skiptools/skip-unit/llms.txt Assert that two variables refer to the exact same object instance in memory. XCTAssertNotIdentical asserts that they are different instances. ```swift import XCTest class IdentityTests: XCTestCase { func testIdentity() { class MyClass {} let obj1 = MyClass() let obj2 = obj1 let obj3 = MyClass() XCTAssertIdentical(obj1, obj2, "Should be same instance") XCTAssertNotIdentical(obj1, obj3, "Should be different instances") } } ``` -------------------------------- ### XCTAssertTrue and XCTAssertFalse Source: https://context7.com/skiptools/skip-unit/llms.txt Assert boolean conditions. `XCTAssertTrue` passes when the condition is true; `XCTAssertFalse` passes when the condition is false. ```APIDOC ## XCTAssertTrue and XCTAssertFalse Assert boolean conditions. `XCTAssertTrue` passes when the condition is true; `XCTAssertFalse` passes when the condition is false. ### Request Example ```swift import XCTest class BooleanTests: XCTestCase { func testBooleanAssertions() { let isEnabled = true let isEmpty = false XCTAssertTrue(isEnabled) XCTAssertTrue(5 > 3, "5 should be greater than 3") XCTAssertFalse(isEmpty) XCTAssertFalse("hello".isEmpty, "String should not be empty") } } ``` ``` -------------------------------- ### Use XCTAssertEqual for Equality Checks Source: https://context7.com/skiptools/skip-unit/llms.txt Verify that two values are equal. This assertion supports generic types, floating-point comparisons with an optional accuracy, and includes an optional failure message. ```swift import XCTest class EqualityTests: XCTestCase { func testBasicEquality() { // Basic equality check XCTAssertEqual(1 + 2, 3) // With descriptive message XCTAssertEqual("hello", "hello", "Strings should match") // Array comparison let expected = [1, 2, 3] let actual = Array(1...3) XCTAssertEqual(actual, expected, "Arrays should be equal") // Floating-point with accuracy let pi = 3.14159 XCTAssertEqual(pi, 3.14, accuracy: 0.01, "Pi approximation") // Float accuracy let floatVal: Float = 2.5 XCTAssertEqual(floatVal, 2.5, accuracy: Float(0.001)) } } ``` -------------------------------- ### Conditionally Skip Tests with XCTSkip Source: https://context7.com/skiptools/skip-unit/llms.txt Throw an assumption violation to skip tests based on platform constraints or feature flags. ```swift import XCTest class SkipTests: XCTestCase { func testPlatformSpecific() throws { #if os(iOS) throw XCTSkip("This test only runs on macOS") #endif // macOS-specific test code XCTAssertTrue(true) } func testFeatureFlag() throws { guard FeatureFlags.newFeatureEnabled else { throw XCTSkip("New feature not enabled") } // Test new feature } } ``` -------------------------------- ### XCTAssertNil and XCTAssertNotNil Source: https://context7.com/skiptools/skip-unit/llms.txt Assert that a value is nil or not nil. `XCTAssertNotNil` returns the unwrapped value when successful. ```APIDOC ## XCTAssertNil and XCTAssertNotNil Assert that a value is nil or not nil. `XCTAssertNotNil` returns the unwrapped value when successful. ### Request Example ```swift import XCTest class NilTests: XCTestCase { func testNilAssertions() { var optionalValue: String? = nil XCTAssertNil(optionalValue, "Value should be nil initially") optionalValue = "Hello" let unwrapped = XCTAssertNotNil(optionalValue) XCTAssertEqual(unwrapped, "Hello") let dict: [String: Int] = ["key": 42] XCTAssertNil(dict["missing"], "Missing key should return nil") XCTAssertNotNil(dict["key"], "Existing key should have value") } } ``` ``` -------------------------------- ### XCTAssertEqual Source: https://context7.com/skiptools/skip-unit/llms.txt Asserts that two values are equal. Supports generic types, with optional accuracy parameter for floating-point comparisons and an optional failure message. ```APIDOC ## XCTAssertEqual Asserts that two values are equal. Supports generic types, with optional accuracy parameter for floating-point comparisons and an optional failure message. ### Request Example ```swift import XCTest class EqualityTests: XCTestCase { func testBasicEquality() { // Basic equality check XCTAssertEqual(1 + 2, 3) // With descriptive message XCTAssertEqual("hello", "hello", "Strings should match") // Array comparison let expected = [1, 2, 3] let actual = Array(1...3) XCTAssertEqual(actual, expected, "Arrays should be equal") // Floating-point with accuracy let pi = 3.14159 XCTAssertEqual(pi, 3.14, accuracy: 0.01, "Pi approximation") // Float accuracy let floatVal: Float = 2.5 XCTAssertEqual(floatVal, 2.5, accuracy: Float(0.001)) } } ``` ``` -------------------------------- ### XCTAssertIdentical and XCTAssertNotIdentical Source: https://context7.com/skiptools/skip-unit/llms.txt Assert reference identity—whether two objects are the same instance in memory (not just equal values). ```APIDOC ## XCTAssertIdentical and XCTAssertNotIdentical Assert reference identity—whether two objects are the same instance in memory (not just equal values). ### Request Example ```swift import XCTest class IdentityTests: XCTestCase { func testIdentity() { class MyClass {} let obj1 = MyClass() let obj2 = obj1 let obj3 = MyClass() XCTAssertIdentical(obj1, obj2, "Should be same instance") XCTAssertNotIdentical(obj1, obj3, "Should be different instances") } } ``` ``` -------------------------------- ### Use XCTAssertNil and XCTAssertNotNil for Optional Values Source: https://context7.com/skiptools/skip-unit/llms.txt Check if an optional value is nil or not nil. XCTAssertNotNil returns the unwrapped value upon success, which can be used in subsequent assertions. ```swift import XCTest class NilTests: XCTestCase { func testNilAssertions() { var optionalValue: String? = nil XCTAssertNil(optionalValue, "Value should be nil initially") optionalValue = "Hello" let unwrapped = XCTAssertNotNil(optionalValue) XCTAssertEqual(unwrapped, "Hello") let dict: [String: Int] = ["key": 42] XCTAssertNil(dict["missing"], "Missing key should return nil") XCTAssertNotNil(dict["key"], "Existing key should have value") } } ``` -------------------------------- ### Define Freestanding Swift Tests Source: https://github.com/skiptools/skip-unit/blob/main/README.md Freestanding @Test functions are automatically wrapped in a generated class for JUnit compatibility. ```swift import Testing @Test func addition() { #expect(1 + 2 == 3) } ``` -------------------------------- ### Force Test Failure with XCTFail Source: https://context7.com/skiptools/skip-unit/llms.txt Mark unreachable code paths or failed logic states by triggering an unconditional test failure. ```swift import XCTest class FailTests: XCTestCase { func testUnreachableCode() { let result = performOperation() switch result { case .success: XCTAssertTrue(true) case .failure: XCTFail("Operation should not fail") case .unknown: XCTFail() } } } ``` -------------------------------- ### XCTUnwrap Source: https://context7.com/skiptools/skip-unit/llms.txt Unwraps an optional value, failing the test if the value is nil. Returns the unwrapped value for continued use. ```APIDOC ## XCTUnwrap Unwraps an optional value, failing the test if the value is nil. Returns the unwrapped value for continued use. ### Request Example ```swift import XCTest class UnwrapTests: XCTestCase { func testUnwrap() throws { let optional: Int? = 42 let value = XCTUnwrap(optional) XCTAssertEqual(value, 42) let dict = ["name": "SkipUnit"] let name = XCTUnwrap(dict["name"], "Name should exist in dictionary") XCTAssertEqual(name, "SkipUnit") } } ``` ``` -------------------------------- ### Use XCTUnwrap to Safely Unwrap Optionals Source: https://context7.com/skiptools/skip-unit/llms.txt Unwrap an optional value, causing the test to fail if the value is nil. The unwrapped value is returned for further use within the test. ```swift import XCTest class UnwrapTests: XCTestCase { func testUnwrap() throws { let optional: Int? = 42 let value = XCTUnwrap(optional) XCTAssertEqual(value, 42) let dict = ["name": "SkipUnit"] let name = XCTUnwrap(dict["name"], "Name should exist in dictionary") XCTAssertEqual(name, "SkipUnit") } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.