Attachments
Attachments are a powerful feature of Allure reports that can help make your tests much more informative. You can attach text logs, screenshots, configuration details, CSV files, and much more to your tests. These attachments can help you diagnose and debug failing tests.
There are several ways to add attachments to a test:
- Using the
allure.attachment
function - Using the
allure.createAttachment
function - Using the
@Attachment
decorator
Built-in attachment types
The simplest way to start with attachments is to use the built-in ones:
- DSL
import { allure } from 'jest-allure2-reporter/api';
test('Sample test', async () => {
await allure.attachment('My attachment', 'This is a simple text attachment', 'text/plain');
});
The allure.attachment
function takes three arguments: name of the attachment, content, and the content type. The content type must be a valid MIME type.
TODO: add screenshot
Custom attachments
For advanced use cases, you may want to create your own custom attachments:
- attachment
- createAttachment
- @Attachment
Using allure.attachment
function is the most straightforward way to add a custom attachment:
import { allure } from 'jest-allure2-reporter/api';
test('Sample test', async () => {
const myData = JSON.stringify({a: 1, b: 2});
await allure.attachment('My JSON attachment', myData, 'application/json');
});
The disadvantage of this approach is that it is less flexible and more verbose for complex data.
The allure.createAttachment
function provides a more advanced way to define a custom attachment:
import { allure } from 'jest-allure2-reporter/api';
const attachJson = allure.createAttachment('JSON attachment', (data) => {
return JSON.stringify(data);
}, 'application/json');
test('Sample test', async () => {
const myData = {a: 1, b: 2};
attachJson(myData);
});
The allure.createAttachment
function is particularly useful for reusable attachments.
For aspect-oriented programmers, there is a decorator-based approach:
import { Attachment } from 'jest-allure2-reporter/api';
class DeviceHelper {
@Attachment('device-status-%s.json', 'application/json')
getStatus(deviceId) {
return { status: 'OK' };
}
}
The @Attachment
decorator is similar to allure.createAttachment
but works only with class methods.
Attachments allow you to enrich your test reports with additional context, which can be very useful when diagnosing and debugging failing tests.
TODO: add screenshot