<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTAttachment",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "c:objc(cs)XCTAttachment"
  },
  "title" : "XCTAttachment"
}
-->

# XCTAttachment

Data from a test method’s execution, such as a file, image, screenshot, data blob, or ZIP file.

```
class XCTAttachment
```

## Topics

### Creating Attachments from Data

[`+  attachmentWithData:`](/documentation/XCTest/XCTAttachment/init(data:))

Creates an attachment containing the provided data payload.

[`+  attachmentWithData:uniformTypeIdentifier:`](/documentation/XCTest/XCTAttachment/init(data:uniformTypeIdentifier:))

Creates an attachment containing the provided data payload, with a custom UTI.

[`-  initWithUniformTypeIdentifier:name:payload:userInfo:`](/documentation/XCTest/XCTAttachment/init(uniformTypeIdentifier:name:payload:userInfo:))

Creates an attachment containing the provided data payload, optionally with a custom UTI, name, and user-provided metadata dictionary.

[`+  attachmentWithUniformTypeIdentifier:name:payload:userInfo:`](/documentation/XCTest/XCTAttachment/attachmentWithUniformTypeIdentifier:name:payload:userInfo:)

Creates an attachment containing the provided data payload, optionally with a custom UTI, name, and user-provided metadata dictionary.

### Creating Attachments from Files and Folders

[`init(contentsOfFileAtURL:)`](/documentation/XCTest/XCTAttachment/init(contentsOfFileAtURL:))

Creates an attachment from the contents of an existing file on disk.

[`init(contentsOfFileAtURL:uniformTypeIdentifier:)`](/documentation/XCTest/XCTAttachment/init(contentsOfFileAtURL:uniformTypeIdentifier:))

Creates an attachment from the contents of an existing file on disk, with a custom UTI.

[`init(compressedContentsOfDirectoryAtURL:)`](/documentation/XCTest/XCTAttachment/init(compressedContentsOfDirectoryAtURL:))

Creates an attachment containing a zipped archive of an existing directory on disk.

### Creating Attachments from Images and Screenshots

[`+  attachmentWithImage:`](/documentation/XCTest/XCTAttachment/init(image:))

Creates an attachment containing a PNG representation of the provided image.

[`+  attachmentWithImage:quality:`](/documentation/XCTest/XCTAttachment/init(image:quality:))

Creates an attachment containing a representation of the provided image at the requested image quality.

[`+  attachmentWithScreenshot:`](/documentation/XCTest/XCTAttachment/init(screenshot:))

Creates an attachment containing a PNG representation of the provided screenshot.

[`+  attachmentWithScreenshot:quality:`](/documentation/XCTest/XCTAttachment/init(screenshot:quality:))

Creates an attachment containing a representation of the provided screenshot at the requested image quality.

  <doc://com.apple.documentation/documentation/XCUIAutomation/XCUIScreenshot>

[`ImageQuality`](/documentation/XCTest/XCTAttachment/ImageQuality)

Compression quality options for image-based attachments.

### Creating Attachments from Objects

[`+  attachmentWithPlistObject:`](/documentation/XCTest/XCTAttachment/init(plistObject:))

Creates an attachment from an object that can be represented in an XML property list.

[`+  attachmentWithArchivableObject:`](/documentation/XCTest/XCTAttachment/init(archivableObject:))

Creates an attachment from an object that conforms to `NSSecureCoding`.

[`+  attachmentWithArchivableObject:uniformTypeIdentifier:`](/documentation/XCTest/XCTAttachment/init(archivableObject:uniformTypeIdentifier:))

Creates an attachment from an object that conforms to `NSSecureCoding`, with a custom UTI.

### Creating Attachments from Strings

[`+  attachmentWithString:`](/documentation/XCTest/XCTAttachment/init(string:))

Creates an attachment containing the provided string.

### Setting an Attachment’s Lifetime

[`lifetime`](/documentation/XCTest/XCTAttachment/lifetime-swift.property)

Indicates whether the attachment is kept or discarded when its associated test passes.

[`Lifetime`](/documentation/XCTest/XCTAttachment/Lifetime-swift.enum)

The possible lifetime values for a test attachment.

### Attachment Metadata

[`name`](/documentation/XCTest/XCTAttachment/name)

The attachment’s name, or `nil` if the attachment is unnamed.

[`uniformTypeIdentifier`](/documentation/XCTest/XCTAttachment/uniformTypeIdentifier)

The Uniform Type Identifier (UTI) of the data represented by the attachment.

[`userInfo`](/documentation/XCTest/XCTAttachment/userInfo)

User-provided metadata associated with the attachment.

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCoding`](/documentation/Foundation/NSCoding)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)