Skip to content

A tool converts test cases written in XCTest to swift-testing

License

Notifications You must be signed in to change notification settings

giginet/swift-testing-revolutionary

Repository files navigation

swift-testing-revolutionary

GitHub Workflow Status (with event) Swift 6.0 Swift 5.10 Xcode 16.0 Beta Xcode 15.4 SwiftPM Platforms License

swift-testing-revolutionary converts test cases written in XCTest to swift-testing

Usage

This tool provides three ways to use. As an Xcode Plugin, as a Command Plugin, or as a command line tool.

In default, all test files will be overwritten unless you passed --dry-run flag. Please make sure to backup your test files before running this tool.

Xcode Plugin

For Xcode project, it's better to use as a Xcode plugin.

1. Add package to your Xcode project

2. Right click on your project root and select RevolutionaryPlugin.

3. Choose the test target you want to convert.

4. Allow change access if needed.

Package Command Plugin

For Swift packages, it's better to use as a package plugin.

1. Add package plugin to your package

let package = Package(
    name: "MyPackage",
    dependencies: [
        .package(url: "https://github.com/giginet/swift-testing-revolutionary", from: "0.1.0"),
    ]
)

2. Run the plugin on your package

You can pass the directories or files you want to convert.

$ swift package plugin --allow-writing-to-package-directory swift-testing-revolutionary Tests/ MyTests1.swift MyTests2.swift

Command Line Tool

Install this tool and run it from the command line.

$ swift-testing-revolutionary path/to/Tests

Using Mint

$ mint install giginet/swift-testing-revolutionary 

Using experimental-install

$ git clone https://github.com/giginet/swift-testing-revolutionary
$ cd swift-testing-revolutionary
$ swift package experimental-install

Using Artifact Bundle

Download swift-testing-revolutionary.artifactbundle.zip from Releases and unzip it.

Options

You can pass some options to the command.

Option Description Default
--dry-run Run as a dry run mode. All test files are not overwritten
--enable/disable-struct-conversion Whether converting test classes to structs or not Enabled
--enable/disable-strip-test-prefix Whether stripping test prefix from each test method or not Enabled
--enable/disable-adding-suite Whether adding @Suite annotation to each test class or not Enabled

Struct Conversion

In the default, all test classes would be converted to structs.

// Before
final class MyModelTests: XCTestCase { }

// After
@Suite struct MyModelTests { } // Enabled (Default)
@Suite final class MyModelTests { } // Disabled

Note

If the test case contains tearDown method. This tool always converts them to de-initializers. However, deinit can't be defined in the struct so this option should be passed for such cases.

Strip Test Prefix

In the default, all test methods would be stripped test prefix.

// Before
func testProperty() { }

// After
@Test func property() { } // Enabled (Default)
@Test func testProperty() { } // Disabled

Adding @Suite

In the default, all test classes would be annotated with @Suite.

// Before
final class MyModelTests: XCTestCase { }

// After
@Suite struct MyModelTests { } // Enabled (Default)
struct MyModelTests { } // Disabled

How to migrate tests for swift-testing

See this article in swift-testing documentation.

Migrating a test from XCTest | Documentation

Supporting Conversions

Currently, this tool supports following conversions referred above documentation.

  • Import statements of XCTest (import XCTest)
  • Test classes to structs (final class XXXTests: XCTestCase)
  • Setup and teardown functions (setUp, tearDown)
  • Test methods func testXXX()
  • Assertion functions (XCTAssert~)
  • Check for optional values (XCTUnwrap)
  • Record Issues (XCTFail)
  • Continue or halt after test failures (continueAfterFailure)
  • Validate asynchronous behaviors (XCTExpectation)
  • Control whether a test runs (XCTSkipIf, XCTSkipUnless)
  • Annotate known issues (XCTExpectFailure)

Unsupported features have to be converted manually.

License

MIT License

About

A tool converts test cases written in XCTest to swift-testing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages