Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension Attachment where AttachableValue == _AttachableURLWrapper {
///
/// When you call this initializer and pass it the URL of a file, it reads or
/// maps the contents of that file into memory. When you call this initializer
/// and pass it the URL of a directory, it creates a temporary zip file of the
/// and pass it the URL of a directory, it creates a temporary ZIP file of the
/// directory before reading or mapping it into memory. These operations may
/// take some time, so this initializer suspends the calling task until they
/// are complete.
Expand Down
23 changes: 23 additions & 0 deletions Sources/Testing/Testing.docc/Attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ extension SalesReport: Encodable, Attachable {}
your test target imports the [Foundation](https://developer.apple.com/documentation/foundation)
module.

### Attach files and directories

If you have a file you want to save as an attachment, you can attach it using
its file URL. The testing library must read or map the file before attaching it
to your test, and those operations can fail, so you must explicitly create an
instance of ``Attachment`` before you record it.
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
its file URL. The testing library must read or map the file before attaching it
to your test, and those operations can fail, so you must explicitly create an
instance of ``Attachment`` before you record it.
its file URL. The testing library needs to read or map the file before attaching it
to your test, and those operations can fail, so you need to explicitly create an
instance of ``Attachment`` before you record it.


```swift
import Foundation

@Test func `sales report adds up`() async throws {
let salesReport = await generateSalesReport()
try salesReport.validate()
let salesReportURL = try salesReport.save()
let attachment = try await Attachment(contentsOf: salesReportURL)
Attachment.record(attachment)
}
```

You can also attach a directory to a test using its file URL. When you attach a
directory to a test, the testing library creates a ZIP file containing the
directory's contents, then attaches that ZIP file in place of the directory.

### Attach images

You can attach instances of the following system-provided image types to a test:
Expand Down