| name | generate-unit-tests |
| description | Generate comprehensive unit tests for ProjectX Apple platforms app using Swift Testing (@Test, @Suite, #expect) and XCTest. Covers Swift 6 ViewModels, UseCases, Services, and Mappers with concurrency-first patterns and full branch coverage. |
Generate Unit Tests — ProjectX Apple Platforms
Framework
- Swift Testing:
@Suite, @Test, #expect, #require, @Test("description")
- XCTest:
XCTestCase for UI tests only
- Async:
async throws test functions
Workflow
1. Analyze Target
Read the file to test. Identify:
- All public/internal methods
- All code branches (if/switch/guard/do-catch)
- Dependencies (protocols to fake)
- State transitions (for ViewModels)
2. Create Fakes
For each protocol dependency, create a fake:
struct FakeEntityService: EntityServiceProtocol {
var getResult: Result<[EntityDTO], Error> = .success([])
var createResult: Result<EntityDTO, Error> = .success(EntityDTO(id: "1", name: "Test"))
func getAll() async throws -> [EntityDTO] { try getResult.get() }
func create(_ dto: EntityDTO) async throws -> EntityDTO { try createResult.get() }
}
3. Write Tests
ViewModel Tests
@Suite("FeatureViewModel Tests")
struct FeatureViewModelTests {
@Test("initial state")
func initialState() { #expect(sut.state == .initial) }
@Test("fetch success updates items")
func fetchData_success() async throws { }
@Test("fetch error sets error state")
func fetchData_error() async throws { }
}
UseCase Tests
@Suite("FeatureUseCase Tests")
struct FeatureUseCaseTests {
@Test("returns mapped entities from repository")
func execute_success() async throws { }
@Test("propagates repository errors")
func execute_error() async throws { }
}
Mapper Tests
@Suite("FeatureMapper Tests")
struct FeatureMapperTests {
@Test("maps DTO to Entity correctly")
func toEntity() {
let dto = EntityDTO(id: "1", name: "Test")
let entity = EntityMapper.toEntity(dto)
#expect(entity.id == "1")
#expect(entity.name == "Test")
}
}
Service Tests
@Suite("FeatureRequest Tests")
struct FeatureRequestTests {
@Test("getAll has correct path and method")
func getAll_config() {
let request = FeatureRequest.getAll
#expect(request.path == "entities")
#expect(request.method == .get)
#expect(request.body == nil)
}
}
Coverage Requirements
- All ViewModel State enum cases reachable
- All Intent enum cases handled
- All error paths (APIError cases)
- All mapper field mappings
- Empty data edge cases
- Nil/optional handling
Test Organization
ProjectXTests/
├── Domain/
│ ├── UseCases/FeatureUseCaseTests.swift
├── Data/
│ ├── Mappers/FeatureMapperTests.swift
│ ├── Services/FeatureRequestTests.swift
├── Presentation/
│ ├── Features/FeatureViewModelTests.swift
├── Fakes/
│ ├── FakeEntityService.swift
│ ├── FakeEntityRepository.swift
Run Tests
xcodebuild test -scheme ProjectX -destination 'platform=iOS Simulator,OS=26.2,name=iPhone 17' -parallel-testing-enabled NO