-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2df08a
commit 5ba57f6
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+65.6 KB
content/posts/series/swift-testing-vs-xctest/6-parametrized-tests/images/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+351 KB
...nt/posts/series/swift-testing-vs-xctest/6-parametrized-tests/images/example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions
56
content/posts/series/swift-testing-vs-xctest/6-parametrized-tests/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: '#6 XCTest vs Swift Testing - Parameterized tests in the fight for more reusable code' | ||
date: 2024-12-05 | ||
tags: ['Swift', 'XCTest', 'Swift Testing', 'unit testing', 'Testing'] | ||
series: "XCTest vs. Swift Testing" | ||
cover: | ||
image: 'images/cover.png' | ||
alt: '#6 XCTest vs Swift Testing - Parameterized tests in the fight for more reusable code' | ||
--- | ||
|
||
### What’s the difference? | ||
|
||
XCTest doesn't provide a built-in solution for parameterized tests. To achieve this, we create test cases as structs or tuples, defining test inputs and expected results. Then, we write a loop to iterate through these test cases and execute the necessary assertions. | ||
|
||
Swift Testing simplifies this process by allowing tests to be parameterized directly. Using the `@Test` macro, you can pass test cases as an argument. What's the benefit? While you still need to define your test cases, the iteration code is no longer needed — Swift Testing handles it for you. | ||
|
||
### My thoughts | ||
|
||
I'm obviously glad to see the code simplification! As a software developer, I enjoy writing less code to achieve the same results. Looking at it with a skeptical eye — it's not a groundbreaking change that will revolutionize how we write tests. However, it's a satisfying improvement that may encourage developers to rethink how they write tests and potentially adopt this pattern where it makes sense. | ||
|
||
### Examples | ||
|
||
![Example](images/example.gif) | ||
|
||
Code ⤵️ | ||
|
||
XCTest | ||
```swift | ||
static let testCases = [ | ||
(text: "ABC", result: false), | ||
(text: "🚀", result: true), | ||
(text: "ABC 👾", result: true) | ||
] | ||
|
||
func testTextContainsEmoji() { | ||
Self.testCases.forEach { test in | ||
XCTAssertEqual(test.text.containsEmoji, test.result) | ||
} | ||
} | ||
``` | ||
|
||
Swift Testing | ||
```swift | ||
static let testCases = [ | ||
(text: "ABC", result: false), | ||
(text: "🚀", result: true), | ||
(text: "ABC 👾", result: true) | ||
] | ||
|
||
@Test(arguments: testCases) | ||
func textContainsEmoji(testCase: (text: String, result: Bool)) { | ||
#expect(testCase.text.containsEmoji == testCase.result) | ||
} | ||
``` | ||
|
||
{{< footer >}} |