|
10 | 10 |
|
11 | 11 | @testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
|
12 | 12 |
|
13 |
| -private struct CustomTrait: CustomExecutionTrait, TestTrait { |
14 |
| - var before: Confirmation |
15 |
| - var after: Confirmation |
16 |
| - func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
17 |
| - before() |
18 |
| - defer { |
19 |
| - after() |
20 |
| - } |
21 |
| - try await function() |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -private struct CustomThrowingErrorTrait: CustomExecutionTrait, TestTrait { |
26 |
| - fileprivate struct CustomTraitError: Error {} |
27 |
| - |
28 |
| - func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
29 |
| - throw CustomTraitError() |
30 |
| - } |
31 |
| -} |
32 |
| - |
33 | 13 | @Suite("CustomExecutionTrait Tests")
|
34 | 14 | struct CustomExecutionTraitTests {
|
35 | 15 | @Test("Execute code before and after a non-parameterized test.")
|
@@ -76,4 +56,53 @@ struct CustomExecutionTraitTests {
|
76 | 56 | }.run(configuration: configuration)
|
77 | 57 | }
|
78 | 58 | }
|
| 59 | + |
| 60 | + @Test("Teardown occurs after child tests run") |
| 61 | + func teardownOccursAtEnd() async throws { |
| 62 | + var configuration = Configuration() |
| 63 | + configuration.eventHandler = { event, _ in |
| 64 | + print(event) |
| 65 | + } |
| 66 | + await runTest(for: TestsWithCustomTrait.self, configuration: configuration) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// MARK: - Fixtures |
| 71 | + |
| 72 | +private struct CustomTrait: CustomExecutionTrait, TestTrait { |
| 73 | + var before: Confirmation |
| 74 | + var after: Confirmation |
| 75 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
| 76 | + before() |
| 77 | + defer { |
| 78 | + after() |
| 79 | + } |
| 80 | + try await function() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private struct CustomThrowingErrorTrait: CustomExecutionTrait, TestTrait { |
| 85 | + fileprivate struct CustomTraitError: Error {} |
| 86 | + |
| 87 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
| 88 | + throw CustomTraitError() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +struct DoSomethingBeforeAndAfterTrait: CustomExecutionTrait, SuiteTrait, TestTrait { |
| 93 | + static let state = Locked(rawValue: 0) |
| 94 | + |
| 95 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Testing.Test, testCase: Testing.Test.Case?) async throws { |
| 96 | + #expect(Self.state.increment() == 1) |
| 97 | + |
| 98 | + try await function() |
| 99 | + #expect(Self.state.increment() == 3) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +@Suite(.hidden, DoSomethingBeforeAndAfterTrait()) |
| 104 | +struct TestsWithCustomTrait { // Trait should only excecute once for each test since it is a suite trait, if we want to execute trait logic for each test set isRecursive to true |
| 105 | + @Test(.hidden) func f() async { |
| 106 | + #expect(DoSomethingBeforeAndAfterTrait.state.increment() == 2) |
| 107 | + } |
79 | 108 | }
|
0 commit comments