Allow inheriting test functions from superclasses used as test suites#1633
Open
Allow inheriting test functions from superclasses used as test suites#1633
Conversation
This PR adds experimental functionality that allows you to write a test function
in a test suite of class type, and then inherit that test function in subclasses
of the test suite automatically. For example:
```swift
class AnimalTests {
func makeNoise() -> Noise? {
return nil // nothing by default
}
@test open func `Animal makes noise`() {
#expect(self.makeNoise() != nil)
}
}
class DogTests: AnimalTests {
override func makeNoise() -> Noise? {
return .bark
}
}
class CatTests: AnimalTests {
override func makeNoise() -> Noise? {
return .meow
}
}
```
We leverage the `open` keyword for this purpose for a few reasons:
1. It has no semantic meaning in test suites today, so it's unlikely to be in
use already.
2. It implies that there will be subclasses of the class in which it is found.
3. The compiler enforces that it can only be applied to member functions of
classes, and can do so with type system knowledge rather than just syntax.
We could spell it differently, e.g. with another attribute macro like
`@inherited`, but we wouldn't be able to statically determine when it is
incorrectly applied to something that isn't a member function of a class. So for
the purposes of the experimental feature, `open` it is!
This makes the `override` keyword ambiguous, so I've blocked it when used with
`@Test`, but there is probably a way to resolve that so we should consider that
constraint to be temporary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds experimental functionality that allows you to write a test function in a test suite of class type, and then inherit that test function in subclasses of the test suite automatically. For example:
We leverage the
openkeyword for this purpose for a few reasons:We could spell it differently, e.g. with another attribute macro like
@inherited, but we wouldn't be able to statically determine when it is incorrectly applied to something that isn't a member function of a class. So for the purposes of the experimental feature,openit is!This makes the
overridekeyword ambiguous, so I've blocked it when used with@Test, but there is probably a way to resolve that so we should consider that constraint to be temporary.To try this feature out, set the Swift build condition
SWIFT_TESTING_EXPERIMENTAL_TEST_INHERITANCE_ENABLEDin your test target.Checklist: