You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -207,7 +207,90 @@ by navigating to the root of the repository and running the following:
207
207
By running tests locally with the `test` script you will be best prepared for
208
208
automated testing in CI as well.
209
209
210
-
### Testing in Xcode
210
+
### Adding new tests
211
+
212
+
Please use [Swift Testing](https://developer.apple.com/documentation/testing) when you add new tests.
213
+
Currently there are few existing tests to draw inspiration from, so here are a few recommendations:
214
+
215
+
- Prefer small test inputs that ideally use a virtual file system for both reading and writing.
216
+
217
+
For example, if you want to test a behavior related to a symbol's in-source documentation and its documentation extension file, you only need one symbol for that.
218
+
You can use `load(catalog:...)`, `makeSymbolGraph(...)`, and `makeSymbol(...)` to define such inputs in a virtual file system and create a `DocumentationContext` from it:
219
+
220
+
```swift
221
+
let catalog = Folder(name: "Something.docc", content: [
This is the in-source documentation for this class.
225
+
""")
226
+
])),
227
+
228
+
TextFile(name: "Something.md", utf8Content: """
229
+
# ``SomeClass``
230
+
231
+
This is additional documentation for this class
232
+
"""),
233
+
])
234
+
let context = try await load(catalog: catalog)
235
+
// Test rest of your test
236
+
```
237
+
238
+
- Consider using parameterized tests if you're making the same verifications in multiple configurations or on multiple elements.
239
+
240
+
You can find some examples of this if you search for `@Test(arguments:`.
241
+
Additionally, you might encounter a `XCTestCase` test that loops over one or more values and performs the same validation for all combinations:
242
+
```swift
243
+
for withExplicitTechnologyRoot in [true, false] {
244
+
for withPageColor in [true, false] {
245
+
...
246
+
```
247
+
Such `XCTestCase` tests can sometimes be expressed more nicely as parameterized tests in Swift Testing.
248
+
249
+
- Think about what information would be helpful to someone else who might debug that test case if it fails in the future.
250
+
251
+
In an open source project like Swift-DocC, it's possible that a person you've never met will continue to work on code that you wrote.
252
+
It could be that they're working on the same feature as you, or it could also be that they're working on something entirely different but their changes broke a test that you wrote.
253
+
To help make their experience better, we appreciate any time that you spend considering if there's any information that you would have wanted to tell that person, as if they were a colleague.
254
+
255
+
One way to convey this information could be to verify assumptions (like "this test content has no user-facing warnings") using `#expect`.
256
+
Additionally, if there's any information that you can surface right in the test failure that will save the next developer from needing to add a breakpoint and run the test again to inspect the value,
257
+
that's a nice small little thing that you can dofor the developer coming after you:
Similarly, code comments or `#expect` descriptions can be a way to convey information about _why_ the test is expecting a _specific_ value.
263
+
```swift
264
+
#expect(graph.cycles(from: 0) == [
265
+
[7,9], // through breadth-first-traversal, 7 is reached before 9.
266
+
])
267
+
```
268
+
That reason may be clear to you, but could be a mystery to a person who is unfamiliar with that part of the code base---or even a future you that may have forgotten certain details about how the code works.
269
+
270
+
- Use `#require` rather that force unwrapping forbehaviors that would change due to unexpected bugsin the code you're testing.
271
+
272
+
If you know that some value will always be non-`nil` only _because_ the rest of the code behaves correctly, consider writing the test more defensively using `#require` instead of force unwrapping the value.
273
+
This has the benefit that if someone else working on Swift-DocC introduces a bug in that behavior that the test relied on, then the test will fail gracefully rather than crashing and aborting the rest of the test execution.
274
+
275
+
A similar situation occurs when you "know" that an array contains _N_ elements. If your test accesses them through indexed subscripting, it will trap if that array was unexpectedly short due to a bug that someone introduced.
276
+
In this situation you can use `problems.dropFirst(N-1).first` to access the _Nth_ element safely.
277
+
This could either be used as an optional value in a `#expect` call, or be unwrapped using `#require` depending on how the element is used in the test.
278
+
279
+
- Use a descriptive and readable phrase as the test name.
280
+
281
+
It can be easier to understand a test's implementation if its name describes the _behavior_ that the test verifies.
282
+
A phrase that start with a verb can often help make a test's name a more readable description of what it's verifying.
283
+
For example: `sortsSwiftFirstAndThenByID`, `raisesDiagnosticAboutCyclicCuration`, `isDisabledByDefault`, and `considersCurationInUncuratedAPICollection`.
284
+
285
+
### Updating existing tests
286
+
287
+
If you're updating an existing test case with additional logic, we appreciate if you also modernize that test while updating it, but we don't expect it.
288
+
If the testcase is part of a large file, you can create new test suite which contains just the testcase that you're modernizing.
289
+
290
+
If you modernize an existing test case, consider not only the syntactical differences between Swift Testing and XCTest,
291
+
but also if there are any Swift Testing features or other changes that would make the test case easier to read, maintain, or debug.
292
+
293
+
### Testing DocC's integration with Xcode
211
294
212
295
You can test a locally built version of Swift-DocC in Xcode 13 or later by setting
213
296
the `DOCC_EXEC` build setting to the path of your local `docc`:
0 commit comments