-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[ST-NNNN] Targeted Interoperability between Swift Testing and XCTest #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
2afaf8e
dc24e27
1e721f8
4325ef5
3ecd884
b275e55
bb5f67e
ed7e09a
07f45fa
f119329
4819c12
0829ac1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,292 @@ | ||||||||
# Targeted Interoperability between Swift Testing and XCTest | ||||||||
|
||||||||
- Proposal: [ST-NNNN](NNNN-xctest-interoperability.md) | ||||||||
- Authors: [Jerry Chen](https://github.com/jerryjrchen) | ||||||||
- Review Manager: TBD | ||||||||
- Status: **Awaiting implementation** | ||||||||
- Implementation: [swiftlang/swift-testing#NNNNN](https://github.com/swiftlang/swift-testing/pull/NNNNN) | ||||||||
- Review: ([pitch](https://forums.swift.org/...)) | ||||||||
|
||||||||
## Introduction | ||||||||
|
||||||||
Many projects want to migrate from XCTest to Swift Testing, and may be in an | ||||||||
intermediate state where test helpers written using XCTest API are called from | ||||||||
Swift Testing. Today, the Swift Testing and XCTest libraries stand mostly | ||||||||
independently, which means an `XCTAssert` failure in a Swift Testing test is | ||||||||
silently ignored. To address this, we formally declare a set of interoperability | ||||||||
|
||||||||
principles and propose updates to specific APIs that will enable users to | ||||||||
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
migrate with confidence. | ||||||||
|
||||||||
## Motivation | ||||||||
|
||||||||
Unfortunately, mixing an API call from one framework with a test from the other | ||||||||
framework may not work as expected. As a more concrete example, if you take an | ||||||||
stmontgomery marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
existing test helper function written for XCTest and call it in a Swift Testing | ||||||||
test, it won't report the assertion failure: | ||||||||
|
||||||||
```swift | ||||||||
func assertUnique(_ elements: [Int]) { | ||||||||
if Set(elements).count != elements.count { | ||||||||
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
XCTFail("\(elements) has non unique elements") | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// XCTest | ||||||||
|
||||||||
class FooTests: XCTestCase { | ||||||||
func testDups() { | ||||||||
assertUnique([1, 2, 1]) // Fails as expected | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// Swift Testing | ||||||||
|
||||||||
@Test func `Duplicate elements`() { | ||||||||
assertUnique([1, 2, 1]) // Passes? Oh no! | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
Generally, we get into trouble today when ALL the following conditions are met: | ||||||||
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
||||||||
- You call XCTest API in a Swift Testing test, or call Swift Testing API in a | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this list describe the conditions under which trouble is got into, or is it describing the trouble itself? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly the latter. I tried rewording so it focuses more on today's API limitation |
||||||||
XCTest test | ||||||||
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
- The API doesn't function as expected in some or all cases, and | ||||||||
- You get no notice at build time or runtime about the malfunction | ||||||||
|
||||||||
For the remainder of this proposal, we’ll describe tests which exhibit this | ||||||||
problem as **lossy without interop**. | ||||||||
|
||||||||
If you've switched completely to Swift Testing and don't expect to use XCTest in | ||||||||
the future, this proposal includes a mechanism to **prevent you from | ||||||||
inadvertently introducing XCTest APIs to your project**, including via a testing | ||||||||
library. | ||||||||
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
||||||||
## Proposed solution | ||||||||
|
||||||||
- **XCTest APIs which are lossy without interop will work as expected when | ||||||||
called in Swift Testing.** In addition, runtime diagnostics will be included | ||||||||
to highlight opportunities to adopt newer Swift Testing equivalent APIs. | ||||||||
|
||||||||
- Conversely, **Swift Testing API called in XCTest will work as expected if | ||||||||
XCTest already provides similar functionality.** You should always feel | ||||||||
empowered to choose Swift Testing when writing new tests or test helpers, as | ||||||||
it will work properly in both types of tests. | ||||||||
|
||||||||
We don't propose supporting interoperability for APIs without risk of data loss, | ||||||||
|
We don't propose supporting interoperability for APIs without risk of data loss, | |
We don't propose supporting interoperability for APIs which are not lossy without interop, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't not hate the double negative here, but can't think of better wording at the moment :)
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this point about traits or about issue handling in general? (Since we're not translating traits.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we're not proposing that you could attach a Swift Testing trait to an XCTest test function. This is about the issue handling trait in a Swift Testing test, and what happens when it's asked to handle a XCTAssert
failure.
stmontgomery marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we think of other XCTest features that are explicitly not supported and should be called out here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XCTestAssertion
and XCTWaiter
, I guess!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I assume you meant XCTestExpectation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm so tired…
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Includes [exit testing][] | |
- Includes `#expect(throws:)` | |
- Includes [exit testing][] |
Assuming #expect(throws:)
will work! If it won't work, we need to call that out too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my understanding of that API, we're catching thrown errors and recording issues if not thrown. Since we'd need to add interop support for Swift Testing Issue as part of the default #expect
, it seems to me the check throws variant should work too
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
stmontgomery marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jerryjrchen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This environment variable affects the behaviour of XCTest API, right? Then I think we want to put it in the XCTEST_
namespace. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider including a link here to the documentation for
XCTAssert()
.