While XCTest remains the preferred way to create tests in Xcode, the new Swift Testing framework attempts to introduce an expressive and intuitive API for the definition of tests that applies to all platforms where Swift is supported. The framework also enables parametrizing, parallelizing, categorizing, and associating tests with bugs.
Swift Testing makes extensive use of macros to provide an idiomatic way to declare complex behaviors. For example, this is how you declare a test with a name and a set of alternative inputs it should be run against:
The above test will be executed three times, with each call receiving one of the provided arguments through the sampleInput
parameter. When parametrizing tests, you can also handle more than one input. In this case, you will provide a list of collections for @Test
‘s arguments
and the test function will take an argument from each collection for every possible combination of values from the two collections:
Under specific conditions for the arguments
type, it is possible to run parametrized tests selectively, so you do not have to run tests for all possible combinations of input values when just a few of them fail.
Besides optionally specifying a name and a list of arguments, the @Test
macro supports the possibility of associating traits to a test function. Existing traits make it possible to enable or disable tests based on a runtime condition, limit the running time of tests, run tests serially or in parallel, associate tests to bugs, and more. For example, this is how you can run a test only if a given app feature is enabled:
Developers can also define their own traits by implementing types conforming to TestTrait.
Parametrizing tests has the additional advantage that the diagnostics produced in case of failure clearly indicates the input that failed.
Test functions can be organized into test suites. The traditional way of doing this is by placing them in the same test class
. Swift Testing also provides a specific macro to that aim, @Suite
. All test functions belonging to a test suite inherit the same traits from the suite, including tags(_:)
or disabled(_:sourceLocation:)
.
To validate test outcomes, Swift Testing employs two essential macros expect(_:_:sourceLocation:)
and require(_:_:sourceLocation:)
. The macro require
immediately halts test execution by throwing an exception if its condition fails, whereas expect
provides thorough reporting.
An essential precaution is that test functions remain in binary files even for release builds. Consequently, Apple emphasizes that the testing library should be incorporated solely within a test target that is unlinked from your primary application.
Swift Testing is accessible on GitHub and the Swift Package Index, necessitating Swift 6. XCTest and Swift Testing can exist together in the same project, allowing you the flexibility to not shift your current tests if you decide to utilize this new framework.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.