swift-corelibs-xctest

repository·main·Indexed 22 days ago

https://github.com/swiftlang/swift-corelibs-xctest

A framework for writing unit tests in Swift for packages and applications, providing the majority of XCTest APIs from Xcode 7+ across all Swift-supported platforms. Includes documentation for integration with Swift Package Manager, running as a standalone executable via XCTMain, and build instructions using the Swift build script, CMake, Docker, or Xcode.

Tokens
1K
Snippets
7
Records
8
Agent score
29%

What's inside swift-corelibs-xctest

  1. Build XCTest using the Swift Build Script

    main

    The most reliable way to build XCTest is using the Swift build script. This requires that you have already built the Swift project and can successfully run utils/build-script -R.

    To build XCTest and run its tests, run:

    cd swift-corelibs-xctest
    ../swift/utils/build-script --preset corelibs-xctest
  2. Run XCTest as a standalone executable

    main

    When not using SwiftPM, you must create a custom executable that links against libXCTest.so. In your main.swift file, you must invoke the XCTMain function. Pass an array of tests to XCTMain, where each test is wrapped by the testCase helper function.

    XCTMain does not return; it will cause the executable to exit with code 0 for success or 1 for failure.

    XCTMain([
        testCase(TestNSString.allTests),
        testCase(TestNSArray.allTests),
        testCase(TestNSDictionary.allTests),
    ])
  3. Use Xcode to browse and run XCTest

    main

    To use Xcode for browsing the source code, open XCTest.xcworkspace.

    • Build Scheme: SwiftXCTest
    • Test Scheme: SwiftXCTestFunctionalTests

    Warning: You must use an Xcode toolchain with an extremely recent version of Swift to build successfully. If the standard toolchains are insufficient, you may need to download development snapshots or build your own toolchain using the utils/build-toolchain script in the Swift repository.

  4. Build XCTest using CMake and Docker

    main

    You can use CMake and Docker to build the project. This method assumes you have cloned the entire Swift project into a folder named swift-project located one level above your current directory.

    1. Build the Docker image:
    docker build -t swift-corelibs-xctest-docker:latest .
    1. Run the container to build and test:
    docker run -it -v ../../swift-project:/swift-project -w /swift-project/swift-corelibs-xctest swift-corelibs-xctest-docker \
          sh -c "cmake -G Ninja -B .build && cmake --build .build && \
          ./build_script.py test \
          --swiftc=/usr/bin/swiftc \
          --foundation-build-dir .build/ \
          .build/"
    docker build -t swift-corelibs-xctest-docker:latest .
    
    docker run -it -v ../../swift-project:/swift-project -w /swift-project/swift-corelibs-xctest swift-corelibs-xctest-docker \
          sh -c "cmake -G Ninja -B .build && cmake --build .build && \
          ./build_script.py test \
          --swiftc=/usr/bin/swiftc \
          --foundation-build-dir .build/ \
          .build/"
  5. Command line arguments for standalone XCTest

    main

    When running a standalone XCTest executable, you can use specific arguments to filter or inspect tests:

    • Select specific tests: Provide the test name or class name to run a single method, an entire class, or a comma-separated list of methods.

      • ./FooTests Tests.FooTestCase/testFoo (Single method)
      • ./FooTests Tests.FooTestCase (All tests in class)
      • ./FooTests Tests.FooTestCase/testFoo,Tests.FooTestCase/testBar (Multiple specific methods)
    • List tests:

      • --list-tests: Prints a list of all available tests.
      • --dump-tests-json: Outputs the test hierarchy in JSON format.
    $ ./FooTests --list-tests
    $ ./FooTests --dump-tests-json