common icon indicating copy to clipboard operation
common copied to clipboard

Support attachments in BeforeAll/AfterAll hooks

Open flywojt opened this issue 5 years ago • 8 comments

I want to add a log file as an attachment to report after tests execution. I wanted to use AfterAll hook but according to this comment: https://github.com/cucumber/cucumber-js/issues/1109#issuecomment-400542113 this is impossible. How can I achieve that? Is this possible to import attach method directly to AfterAll hook?

// This is not working as AfterAll hook dosn't have acces to World instance 
AfterAll(async function () { 
      const file = await fs.readFileSync('./features/support/scp_util/file.txt') 
      this.attach(file, 'text/plain'); 
  });```

flywojt avatar Jul 23 '20 13:07 flywojt

@flywojt Did you find a solution to your problem or do you still require assistance?

kamolins avatar Sep 15 '20 15:09 kamolins

No, it is not currently possible. This is mainly due to the idea that attachments are added to a particular test and we don't have the concept of adding a file to the run as whole. What is the use case that you want to attach it in after all instead of after?

charlierudolph avatar Sep 16 '20 02:09 charlierudolph

@kamolins no, I didn't

@charlierudolph I am adding log files with request/response from REST API and another file with information about testing env. For now, I am using step at the end of every feature file but this is not a clean solution, as I am attaching these same files many times.

flywojt avatar Sep 16 '20 06:09 flywojt

Are you using a custom reporter or an open source one to generate the report?

In our project we are using TS and we have a static class for storing anything related to the "Test Run" itself, so you could implement something along the lines of that

kamolins avatar Sep 16 '20 22:09 kamolins

This would be a great addition as I've ran into the same scenario that @flywojt mentioned where I need to attach some API request result to the report after all test cases have finished.

Andrewngabriel avatar Sep 24 '21 16:09 Andrewngabriel

The core team discussed this and we agreed the following changes to messages could work:

  • Add TestRunStarted.id
  • Add Attachment.testRunStartedId

This way, an attachment made in a global hook could reference the test run rather than a particular step.

aslakhellesoy avatar Mar 10 '22 15:03 aslakhellesoy

Moved to the common repo so we can track the implementation progress across our libraries and runners.

davidjgoss avatar Sep 01 '25 08:09 davidjgoss

Implementation:

  • [x] messages
  • [x] compatibility-kit https://github.com/cucumber/compatibility-kit/pull/162
  • [x] query https://github.com/cucumber/query/pull/109
  • [ ] formatters
    • [ ] xml
    • [ ] pretty
    • [ ] html
  • [ ] implementations
    • [ ] cucumber-node
    • [ ] cucumber-js
    • [ ] cucumber-jvm
    • [ ] cucumber-ruby
    • [ ] reqnroll

davidjgoss avatar Sep 01 '25 08:09 davidjgoss