KickstarterBrandon WilliamsProblem & solution
Why you should co-locate your Xcode tests
Summary
Xcode projects with unit tests commonly separate implementation and test files into two directories, often mirroring the source structure in the tests directory. The post argues that this arrangement creates maintenance overhead, slows navigation, obscures how well-tested a codebase is when browsed or reviewed, and can leave tests with lower coding standards. It proposes co-locating each unit-test file beside its implementation file while retaining the test target association, accepting more files per directory in exchange for clearer relationships and easier movement between code and tests. The same SwiftLint configuration is used for implementation and test code, treating tests as first-class code. Tests without a single-file correspondence, including integration or UI tests, should remain in separate directories and may warrant separate targets; the authors say their codebase has adopted the style.
Context
Xcode projects with unit tests commonly use separate implementation and test directories, often with a mirrored file structure. The post identifies maintenance overhead, difficult navigation, less visible test coverage during code browsing and pull-request review, and lower coding standards for tests as drawbacks of this arrangement.
Approach / What changed
Co-locate unit-test files with their corresponding implementation files while keeping each test associated with its existing test target. Use the same SwiftLint configuration for implementation and test code. Keep tests that cover multiple files, such as integration or UI tests, in separate directories, potentially with their own target separate from unit tests.
Takeaways
Co-locating unit tests makes the corresponding implementation easier to find and makes navigation between the two files more direct.
The test files remain associated with the test target even after moving into the same directory as implementation files.
Integration and UI tests that do not correspond to a single implementation file should remain in a separate directory and may use a separate target.
Source and freshness
- Source
- medium.com
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.