|
| 1 | +# Testing Guide for TableCapture |
| 2 | + |
| 3 | +This guide explains how to write and run tests for the TableCapture app. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +The project uses two types of tests: |
| 8 | + |
| 9 | +### 1. **Unit Tests** (`TableCaptureTests/`) |
| 10 | +- **Framework:** Swift Testing (modern, introduced in Swift 5.9+) |
| 11 | +- **Purpose:** Test individual functions, logic, and data transformations |
| 12 | +- **Speed:** Fast (no UI, no app launch) |
| 13 | +- **Location:** `TableCaptureTests/TableCaptureTests.swift` |
| 14 | + |
| 15 | +### 2. **UI Tests** (`TableCaptureUITests/`) |
| 16 | +- **Framework:** XCTest + XCUITest |
| 17 | +- **Purpose:** Test user interactions and full app behavior |
| 18 | +- **Speed:** Slower (launches full app) |
| 19 | +- **Location:** `TableCaptureUITests/TableCaptureUITests.swift` |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Running Tests |
| 24 | + |
| 25 | +### In Xcode |
| 26 | + |
| 27 | +**Run all tests:** |
| 28 | +- Press `⌘U` (Command + U) |
| 29 | +- Or: **Product → Test** |
| 30 | + |
| 31 | +**Run a single test:** |
| 32 | +1. Click the diamond icon next to the test function |
| 33 | +2. Or: Put cursor in test function and press `⌘U` |
| 34 | + |
| 35 | +**Run a specific test file:** |
| 36 | +- Click the diamond icon next to the `struct` or `class` name |
| 37 | + |
| 38 | +### From Command Line |
| 39 | + |
| 40 | +```bash |
| 41 | +# Run all tests |
| 42 | +xcodebuild test -project TableCapture.xcodeproj -scheme TableCapture |
| 43 | + |
| 44 | +# Run only unit tests |
| 45 | +xcodebuild test -project TableCapture.xcodeproj -scheme TableCapture -only-testing:TableCaptureTests |
| 46 | + |
| 47 | +# Run only UI tests |
| 48 | +xcodebuild test -project TableCapture.xcodeproj -scheme TableCapture -only-testing:TableCaptureUITests |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Writing Unit Tests (Swift Testing) |
| 54 | + |
| 55 | +### Basic Structure |
| 56 | + |
| 57 | +```swift |
| 58 | +import Testing |
| 59 | +@testable import TableCapture |
| 60 | + |
| 61 | +struct MyTests { |
| 62 | + @Test("Description of what this tests") |
| 63 | + func testSomething() async throws { |
| 64 | + // Arrange: Set up test data |
| 65 | + let input = "test" |
| 66 | + |
| 67 | + // Act: Perform the action |
| 68 | + let result = someFunction(input) |
| 69 | + |
| 70 | + // Assert: Verify the result |
| 71 | + #expect(result == "expected") |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Key Features |
| 77 | + |
| 78 | +**Assertions:** |
| 79 | +```swift |
| 80 | +#expect(value == expected) // Basic equality |
| 81 | +#expect(value != unwanted) // Inequality |
| 82 | +#expect(array.contains(item)) // Contains check |
| 83 | +#expect(value > 0) // Comparison |
| 84 | +#expect(value == nil) // Nil check |
| 85 | +``` |
| 86 | + |
| 87 | +**Async tests:** |
| 88 | +```swift |
| 89 | +@Test func testAsync() async throws { |
| 90 | + let result = await someAsyncFunction() |
| 91 | + #expect(result.isSuccess) |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +**Test with parameters:** |
| 96 | +```swift |
| 97 | +@Test(arguments: [1, 2, 3, 4, 5]) |
| 98 | +func testMultipleInputs(number: Int) { |
| 99 | + #expect(number > 0) |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Writing UI Tests (XCTest) |
| 106 | + |
| 107 | +### Basic Structure |
| 108 | + |
| 109 | +```swift |
| 110 | +import XCTest |
| 111 | + |
| 112 | +final class MyUITests: XCTestCase { |
| 113 | + var app: XCUIApplication! |
| 114 | + |
| 115 | + override func setUpWithError() throws { |
| 116 | + continueAfterFailure = false |
| 117 | + app = XCUIApplication() |
| 118 | + app.launch() |
| 119 | + } |
| 120 | + |
| 121 | + @MainActor |
| 122 | + func testSomething() throws { |
| 123 | + // Find UI elements |
| 124 | + let button = app.buttons["My Button"] |
| 125 | + |
| 126 | + // Interact with them |
| 127 | + button.tap() |
| 128 | + |
| 129 | + // Verify results |
| 130 | + XCTAssertTrue(button.exists) |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Key Features |
| 136 | + |
| 137 | +**Finding elements:** |
| 138 | +```swift |
| 139 | +app.buttons["Button Title"] // Button by label |
| 140 | +app.textFields["Email"] // Text field |
| 141 | +app.windows["Window Title"] // Window |
| 142 | +app.staticTexts["Label"] // Text label |
| 143 | +``` |
| 144 | + |
| 145 | +**Interactions:** |
| 146 | +```swift |
| 147 | +element.tap() // Click |
| 148 | +element.typeText("hello") // Type text |
| 149 | +element.swipeLeft() // Swipe gesture |
| 150 | +``` |
| 151 | + |
| 152 | +**Assertions:** |
| 153 | +```swift |
| 154 | +XCTAssertTrue(element.exists) |
| 155 | +XCTAssertEqual(element.label, "Expected") |
| 156 | +XCTAssertFalse(element.isEnabled) |
| 157 | +XCTAssertNotNil(value) |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## Current Test Coverage |
| 163 | + |
| 164 | +### Unit Tests (`TableCaptureTests.swift`) |
| 165 | + |
| 166 | +**CSV Formatting:** |
| 167 | +- ✅ Escaping commas |
| 168 | +- ✅ Escaping quotes |
| 169 | +- ✅ Handling empty cells |
| 170 | + |
| 171 | +**Markdown Formatting:** |
| 172 | +- ✅ Table structure (headers, separators) |
| 173 | +- ✅ Escaping pipe characters |
| 174 | +- ✅ Handling uneven row lengths |
| 175 | + |
| 176 | +**Grid Management:** |
| 177 | +- ✅ Adding columns/rows |
| 178 | +- ✅ Removing selected lines |
| 179 | +- ✅ Clearing all lines |
| 180 | + |
| 181 | +### UI Tests (`TableCaptureUITests.swift`) |
| 182 | + |
| 183 | +**App Launch:** |
| 184 | +- ✅ Launches without crashing |
| 185 | +- ✅ Stays running after launch |
| 186 | + |
| 187 | +**Performance:** |
| 188 | +- ✅ Launch time measurement |
| 189 | +- ✅ Memory usage measurement |
| 190 | + |
| 191 | +**Accessibility:** |
| 192 | +- ✅ VoiceOver support checks |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## Best Practices |
| 197 | + |
| 198 | +### Unit Tests |
| 199 | + |
| 200 | +1. **Keep tests isolated** - Each test should be independent |
| 201 | +2. **Use descriptive names** - Test names should explain what they verify |
| 202 | +3. **Test one thing** - Each test should verify a single behavior |
| 203 | +4. **Use `@testable import`** - Access internal types for testing |
| 204 | + |
| 205 | +### UI Tests |
| 206 | + |
| 207 | +1. **Use accessibility identifiers** - Make elements easier to find |
| 208 | +2. **Wait for elements** - Use `waitForExistence(timeout:)` |
| 209 | +3. **Test user journeys** - Simulate real user workflows |
| 210 | +4. **Keep tests stable** - Avoid flaky tests with proper waits |
| 211 | + |
| 212 | +### General |
| 213 | + |
| 214 | +1. **Run tests before commits** - Ensure nothing breaks |
| 215 | +2. **Write tests for bugs** - Prevent regressions |
| 216 | +3. **Test edge cases** - Empty strings, nil values, extreme inputs |
| 217 | +4. **Keep tests maintainable** - Refactor test code too |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## Adding New Tests |
| 222 | + |
| 223 | +### For a new function in `TableEditorViewModel`: |
| 224 | + |
| 225 | +```swift |
| 226 | +@Test("What this function should do") |
| 227 | +func testNewFunction() async throws { |
| 228 | + let viewModel = TableEditorViewModel(image: createTestImage()) |
| 229 | + |
| 230 | + // Test your function |
| 231 | + viewModel.newFunction() |
| 232 | + |
| 233 | + #expect(viewModel.someProperty == expectedValue) |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +### For a new UI feature: |
| 238 | + |
| 239 | +```swift |
| 240 | +@MainActor |
| 241 | +func testNewUIFeature() throws { |
| 242 | + let app = XCUIApplication() |
| 243 | + app.launch() |
| 244 | + |
| 245 | + // Find and interact with your UI |
| 246 | + let newButton = app.buttons["New Feature"] |
| 247 | + newButton.tap() |
| 248 | + |
| 249 | + // Verify the result |
| 250 | + XCTAssertTrue(app.staticTexts["Success"].exists) |
| 251 | +} |
| 252 | +``` |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +## Troubleshooting |
| 257 | + |
| 258 | +**Tests won't run:** |
| 259 | +- Clean build folder: `⌘⇧K` |
| 260 | +- Delete derived data: `Xcode → Settings → Locations → Derived Data` |
| 261 | + |
| 262 | +**UI tests can't find elements:** |
| 263 | +- Add accessibility identifiers to SwiftUI views: |
| 264 | + ```swift |
| 265 | + Button("My Button") { } |
| 266 | + .accessibilityIdentifier("myButton") |
| 267 | + ``` |
| 268 | +- Use `po app.debugDescription` in debugger to see all elements |
| 269 | + |
| 270 | +**Tests are flaky:** |
| 271 | +- Add explicit waits: |
| 272 | + ```swift |
| 273 | + let element = app.buttons["My Button"] |
| 274 | + XCTAssertTrue(element.waitForExistence(timeout: 5)) |
| 275 | + ``` |
| 276 | + |
| 277 | +--- |
| 278 | + |
| 279 | +## Resources |
| 280 | + |
| 281 | +- [Swift Testing Documentation](https://developer.apple.com/documentation/testing) |
| 282 | +- [XCTest Documentation](https://developer.apple.com/documentation/xctest) |
| 283 | +- [UI Testing Guide](https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html) |
0 commit comments