|
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,49 @@ 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 | + await runTest(for: TestsWithCustomTraitWithStrongOrdering.self, configuration: .init()) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: - Fixtures |
| 67 | + |
| 68 | +private struct CustomTrait: CustomExecutionTrait, TestTrait { |
| 69 | + var before: Confirmation |
| 70 | + var after: Confirmation |
| 71 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
| 72 | + before() |
| 73 | + defer { |
| 74 | + after() |
| 75 | + } |
| 76 | + try await function() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private struct CustomThrowingErrorTrait: CustomExecutionTrait, TestTrait { |
| 81 | + fileprivate struct CustomTraitError: Error {} |
| 82 | + |
| 83 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Test, testCase: Test.Case?) async throws { |
| 84 | + throw CustomTraitError() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct DoSomethingBeforeAndAfterTrait: CustomExecutionTrait, SuiteTrait, TestTrait { |
| 89 | + static let state = Locked(rawValue: 0) |
| 90 | + |
| 91 | + func execute(_ function: @escaping @Sendable () async throws -> Void, for test: Testing.Test, testCase: Testing.Test.Case?) async throws { |
| 92 | + #expect(Self.state.increment() == 1) |
| 93 | + |
| 94 | + try await function() |
| 95 | + #expect(Self.state.increment() == 3) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +@Suite(.hidden, DoSomethingBeforeAndAfterTrait()) |
| 100 | +struct TestsWithCustomTraitWithStrongOrdering { |
| 101 | + @Test(.hidden) func f() async { |
| 102 | + #expect(DoSomethingBeforeAndAfterTrait.state.increment() == 2) |
| 103 | + } |
79 | 104 | }
|
0 commit comments