<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michał Kowalski on Medium]]></title>
        <description><![CDATA[Stories by Michał Kowalski on Medium]]></description>
        <link>https://medium.com/@_mkowalski?source=rss-f7c2d695a169------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6fQMy0c8ToyjzWbD.jpg</url>
            <title>Stories by Michał Kowalski on Medium</title>
            <link>https://medium.com/@_mkowalski?source=rss-f7c2d695a169------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:35:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@_mkowalski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Parametrizing unit tests with XCTestParametrizedMacro]]></title>
            <link>https://medium.com/@_mkowalski/parametrizing-unit-tests-with-xctestparametrizedmacro-c44b243888b5?source=rss-f7c2d695a169------2</link>
            <guid isPermaLink="false">https://medium.com/p/c44b243888b5</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Michał Kowalski]]></dc:creator>
            <pubDate>Tue, 21 Nov 2023 16:56:33 GMT</pubDate>
            <atom:updated>2023-11-21T16:56:33.810Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*B5RupRrIOsx_em3OleaCaA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@ikukevk?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Kevin Ku</a> on <a href="https://unsplash.com/photos/closeup-photo-of-eyeglasses-w7ZyuGYNpRQ?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><p>Unit testing is a software testing technique where a software program is dissected into smaller components (units) and each unit undergoes independent testing. Programmers create and execute unit tests during development, considering them as a crucial factor for ensuring a high-quality software product.<br>Integrating unit tests into our product provides numerous benefits, including early bug detection in the development process, reinforcement of good design patterns, effective documentation, and acceleration of the overall development process.</p><p>Well-written unit tests should check the correctness of our code not only for one variant, but it should check all different combinations of input data, so the code is fully covered by unit tests.<br>Such a requirement always results in running our test method multiple times with different arguments. This causes the process of writing unit tests to become boring and developers often resign from testing their code for more than one case.</p><p>In this article, I’d like to show how with a simple swift macro you can reduce the amount of boilerplate code needed to write unit test</p><h3>Real life scenario</h3><p>Now let’s consider a real-life example. Assume we have an account view in our app that presents the name, current balance, and loading indicator while fetching data.<br>We follow well known MVVM pattern so to separate our logic from the view, we created the viewModel class <em>AccountViewModel</em>, and now we want to test its correctness for different kinds of accounts.</p><p>So our first test method would look like this:</p><pre>    func testAccountViewModel_SavingAccount() throws {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                                 accountId: &quot;001&quot;)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>                    expectation.fulfill()<br>                }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, &quot;SavingAccount&quot;)<br>        XCTAssertEqual(viewModel.balance, NSDecimalNumber(string: &quot;3000&quot;))<br>    }</pre><p>But if we want to test for the other two types of account, we have two options: put everything into a single method by copy and paste which results in a very long test method or create a separate method for every test case like this:</p><pre>func testAccountViewModel_SavingAccount() throws {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                                 accountId: &quot;001&quot;)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>                    expectation.fulfill()<br>                }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, &quot;SavingAccount&quot;)<br>        XCTAssertEqual(viewModel.balance, NSDecimalNumber(string: &quot;3000&quot;))<br>    }<br><br>    func testAccountViewModel_CreditAccount() throws {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                                 accountId: &quot;002&quot;)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>                    expectation.fulfill()<br>                }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, &quot;CreditAccount&quot;)<br>        XCTAssertEqual(viewModel.balance, NSDecimalNumber(string: &quot;-15000&quot;))<br>    }<br><br>    func testAccountViewModel_BasicAccount() throws {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                                 accountId: &quot;003&quot;)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>                    expectation.fulfill()<br>                }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, &quot;BasicAccount&quot;)<br>        XCTAssertEqual(viewModel.balance, NSDecimalNumber(string: &quot;2345&quot;))<br>    }</pre><p>No matter what we choose, the result will be a huge amount of duplicated code. So in case of even small refactoring in the future, we would end up changing the same code in a few places.</p><p>But we can be a little smarter and extract test logic to a generic method and pass input and output data as parameters.</p><pre>    func testAccountViewModel_AllAccounts() throws {<br>        genericTestMethod(id: &quot;001&quot;, balance: NSDecimalNumber(string: &quot;3000&quot;), accountName: &quot;SavingAccount&quot;)<br>        genericTestMethod(id: &quot;002&quot;, balance: NSDecimalNumber(string: &quot;-15000&quot;), accountName: &quot;CreditAccount&quot;)<br>        genericTestMethod(id: &quot;003&quot;, balance: NSDecimalNumber(string: &quot;2345&quot;), accountName: &quot;BasicAccount&quot;)<br>    }<br><br>    private func genericTestMethod(id: String, balance: NSDecimalNumber, accountName: String) {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                                 accountId: id)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>                    expectation.fulfill()<br>                }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, accountName)<br>        XCTAssertEqual(viewModel.balance, balance)<br>    }</pre><p>One disadvantage of such a solution is that in case of failure, we lose information for which data test is failing.</p><p>To improve this we could add some messages to the assertion, but I think a better solution would be to create a test method with a meaningful name.<br>So in case of failing tests in Xcode’s test navigator, we know exactly which test method is failing.</p><pre>    func testAccountViewModel_SavingAccount() throws {<br>        genericTestMethod(id: &quot;001&quot;, balance: NSDecimalNumber(string: &quot;3000&quot;), accountName: &quot;Saving Account&quot;)<br>    }<br><br>    func testAccountViewModel_CreditAccount() throws {<br>        genericTestMethod(id: &quot;002&quot;, balance: NSDecimalNumber(string: &quot;-15000&quot;), accountName: &quot;Credit Account&quot;)<br>    }<br><br>    func testAccountViewModel_BasicAccount() throws {<br>        genericTestMethod(id: &quot;003&quot;, balance: NSDecimalNumber(string: &quot;2345&quot;), accountName: &quot;Basic Account&quot;)<br>    }</pre><p>Our test methods look good, but we would like to focus on writing productive code rather than spending time copying and renaming test methods.</p><p>Can we do it better?</p><h3>Meet Swift Macros</h3><p>Swift Macros are a key feature of Swift since version 5.9. This framework gives developers the ability to write code snippets that are expanded at compile time. Macros allow for the creation of reusable and customizable pieces of code, streamlining development processes and making code more expressive by removing boilerplate code.</p><p>It seems to be perfect for our unit test scenario.</p><p>Together with my colleague, <a href="https://medium.com/u/ca3bf4a69765">Michał Myśliwiec</a> from Xebia Poland, we created a Swift macro package called <strong>XCTestParametrizedMacro</strong> designed for this case.</p><p>The <strong>Parametrize</strong> macro takes three parameters:</p><ul><li>An array of input objects, in our case, it is an array of strings representing a list of account IDs.</li><li>An array of output objects (expected values), in this test, there are two expected values (the name of the account and account balance), so the output array contains a list of tuples (String, NSDecimalNumber).</li><li>An array of labels. It is not required, but with labels, we can improve the naming of generated test methods.</li></ul><pre>    @Parametrize(input: [&quot;001&quot;,<br>                         &quot;002&quot;,<br>                         &quot;003&quot;],<br>                 output: [(&quot;Saving Account&quot;, NSDecimalNumber(string: &quot;3000&quot;)),<br>                          (&quot;Credit Account&quot;, NSDecimalNumber(string: &quot;-15000&quot;)),<br>                          (&quot;Basic Account&quot;, NSDecimalNumber(string: &quot;2345&quot;))],<br>                 labels: [&quot;SavingAccount&quot;,<br>                          &quot;CreditAccount&quot;,<br>                          &quot;BasicAccount&quot;])<br>    func testAccountViewModel(input accountId: String,<br>                              output expected: (String, NSDecimalNumber)) throws {<br>        let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                         accountId: accountId)<br>        XCTAssertTrue(viewModel.isLoading)<br>        let expectation = expectation(description: &quot;refresh call&quot;)<br>        viewModel.refresh {<br>            expectation.fulfill()<br>        }<br>        wait(for: [expectation])<br>        XCTAssertFalse(viewModel.isLoading)<br>        XCTAssertEqual(viewModel.accountName, expected.0)<br>        XCTAssertEqual(viewModel.balance, expected.1)<br>    }</pre><p>Such code will be expanded to three test methods like this one.</p><pre>func testAccountViewModel_SavingAccount() throws {<br>    let accountId: String = &quot;001&quot;<br>    let expected: (String, NSDecimalNumber) = (&quot;Saving Account&quot;, NSDecimalNumber(string: &quot;3000&quot;))<br>    let viewModel = AccountViewModel(accountService: accountServiceMock,<br>                                             accountId: accountId)<br>    XCTAssertTrue(viewModel.isLoading)<br>    let expectation = expectation(description: &quot;refresh call&quot;)<br>    viewModel.refresh {<br>                expectation.fulfill()<br>            }<br>    wait(for: [expectation])<br>    XCTAssertFalse(viewModel.isLoading)<br>    XCTAssertEqual(viewModel.accountName, expected.0)<br>    XCTAssertEqual(viewModel.balance, expected.1)<br>}</pre><p>Using <strong>XCTestParametrizedMacro</strong>, developers can concentrate on creating effective test methods and eliminate the need for boilerplate code. With the macro, our tests are more readable; input and output parameters are separated from the test methods. With this tool, your tests are more in line with <a href="https://en.wikipedia.org/wiki/Data-driven_testing">Data-driven testing</a>.</p><p>This macro can of course be used for tests on other levels, not only unit tests - whenever you need to use the same body of the test with different input values it will save you time implementing these tests. QA Engineers in our mobile teams are thrilled to use it for their UI tests written in Swift and XCUITest.</p><p><strong>XCTestParametrizedMacro</strong> is available on GitHub</p><p><a href="https://github.com/PGSSoft/XCTestParametrizedMacro">https://github.com/PGSSoft/XCTestParametrizedMacro</a></p><p>We are looking forward to any feedback or contribution. If you like the idea please leave a star.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c44b243888b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>