# Approaches to testing macros

**URL:** <https://forums.swift.org/t/approaches-to-testing-macros/72330>\
**Category:** Macros\
**Tags:** testing, macros, snippets\
**Created:** [June 10, 2024, 2:21am UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330 "2024-06-10T02:21:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumyamahunt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soumyamahunt/32/23427_2.png) [@soumyamahunt](https://forums.swift.org/u/soumyamahunt)\
**Post date:** [June 10, 2024, 2:21am UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330/1 "2024-06-10T02:21:39Z")

</div>

Hey everyone! I created [`MetaCodable`](https://github.com/SwiftyLab/MetaCodable) to handle common `Codable` implementation generation use-cases. Currently the issue I face is better testability with the huge set of features this library supports, one of the thing I feel sorely lacking in macro testing approaches is **asserting generated macro code compiles**.

To address this I was thinking of creating snippets for each tests and grouping snippets based on test cases. [Going through the Snippets pitch](https://github.com/apple/swift-evolution/blob/main/proposals/0356-swift-snippets.md), I couldn't find any commands to run all the snippets or run snippets in parallel. Is running all the snippets possible with a single command?

What are the approaches you follow to test macro generated code compiles?

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [June 10, 2024, 2:31am UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330/2 "2024-06-10T02:31:03Z")

</div>

In swift-mmio I include sample usages of the macros in a test target to ensure the expanded code type checks and compiles properly: [swift-mmio/Tests/MMIOTests/ExpansionTypeCheckTests.swift at main · apple/swift-mmio · GitHub](https://github.com/apple/swift-mmio/blob/main/Tests/MMIOTests/ExpansionTypeCheckTests.swift)

Personally I find myself wanting to see [@pointfree.co's macro testing support](https://github.com/pointfreeco/swift-macro-testing) added to swift-syntax. I like that their library can autofill the expanded code which reduces the mechanical work needed for complex macros.

---

<div class="post-metadata">

**Author:** ![soumyamahunt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soumyamahunt/32/23427_2.png) [@soumyamahunt](https://forums.swift.org/u/soumyamahunt)\
**Post date:** [June 19, 2024, 6:04pm UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330/3 "2024-06-19T18:04:51Z")

</div>

Thanks @rauhul, but I wanted to exactly avoid the approach suggested as the seer amount of combinations supported by [`MetaCodable`](https://github.com/SwiftyLab/MetaCodable) will require multiple different samples all with different names. This will also be difficult to maintain as the library scales with more features.

With the new Swift testing framework here is the approach I went with structuring tests:

```swift
struct TestCase {
    @Test
    func testErrorsWithMacro() {}

    struct MacroUseCase {
        struct Sample {}

        @Test
        func assertSampleExpansion() {}
    }
}

```

- Here `TestCase` represents a specific macro/use-case scenario I am validating.

- It can contain multiple `testErrorsWithMacro` which asserts proper diagnostic errors/warnings generated by the macro in invalid usages.

- For each usage scenario of the macro one `MacroUseCase` type can be created.

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [June 19, 2024, 6:43pm UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330/4 "2024-06-19T18:43:34Z")

</div>

We have found that a hybrid approach typically needs to be taken, as @rauhul mentioned. We will have a [file](https://github.com/pointfreeco/swift-composable-architecture/blob/26ae4fdd7bad2adf514cd9f439e2da9f184fc6e5/Tests/ComposableArchitectureTests/MacroTests.swift) that just lists out a bunch of usages of our macros to make sure that they at least compile, though there is no verification they are "correct". And then another [file](https://github.com/pointfreeco/swift-composable-architecture/blob/26ae4fdd7bad2adf514cd9f439e2da9f184fc6e5/Tests/ComposableArchitectureMacrosTests/ReducerMacroTests.swift) that uses our `assertMacro` helper to ensure that the expanded macro code looks correct.

I'm not sure if there is really much one can do to improve on this hybrid approach, but if there is I'd love to see it!

---

<div class="post-metadata">

**Author:** ![soumyamahunt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soumyamahunt/32/23427_2.png) [@soumyamahunt](https://forums.swift.org/u/soumyamahunt)\
**Post date:** [June 20, 2024, 9:55am UTC](https://forums.swift.org/t/approaches-to-testing-macros/72330/5 "2024-06-20T09:55:29Z")

</div>

@mbrandonw I am not denying hybrid approach needs to be taken which is what I want to have as well. I have updated [my answer](https://forums.swift.org/t/approaches-to-testing-macros/72330/3) with more details how it allows me improve upon the hybrid approach suggested.
