Description
Motivation
As a test case author, I want to explicitly enable, or disable, a test when:
- all conditional traits evaluate to
true
- any conditional training evaluate to
true
I always prefer explicit vs implicit. Take the following example
@Test(
.enabled(if: conditionA),
.enabled(if: conditionB),
)
func myTest() {
// test implementation
}
It is unclear by ready the code whether the various conditions are ||
(OR-ed), or whether they are &&
(AND-ed). A developer must be aware of Swift Testing behaviour to know when the test will be enabled or disabled.
Proposed solution
One option is to introduce a .any
and .all
traits, that would accept a list of traits, or any number of traits.
Consider the following, which is more explicit. In the first, conditionA && conditionB
must be true to enable the test, while the conditionA || conditionB
must be true to enable the second test
@Test(
.all(
.enabled(if: conditionA),
.enabled(if: conditionB),
)
)
func myTestBothConditionsMustBeTrue() {
// test implementation
}
@Test(
.any(
.enabled(if: conditionA),
.enabled(if: conditionB),
)
)
func myTestAnyConditionMustBeTrue() {
// test implementation
}
The .any
and .all
become especially useful when custom condition traits are added, where using the single .enabled(if: condition)
is more challenging.
extension Trait where Self == Testing.ConditionTrait {
public static var enableOnlyOnMacOS: Self {
enabled(if: isMacOS(), "Test is only supported on MacOS")
}
public static var enableIfRealSigningIdentityTestEnabled: Self {
enabled(if: isRealSigningIdentityTestEnabled(), "Test only enabled during real signing indentity")
}
public static func enableIfEnvVarSet(_ envVarName: String) -> Self {
let envKey = EnvironmentKey(envVarName)
return enabled(
if: isEnvironmentVariableSet(envKey),
"'\(envVarName)' env var is not set"
)
}
}
@Test (
.all(
.enableOnlyOnMacOS,
.enableIfRealSigningIdentityTestEnabled,
.enableIfEnvVarSet("MY_ENV_VARIABLE"),
)
)
funct myTest() {
}
Alternatives considered
I'm open to other solution
Additional information
No response