Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get test status on afterEach() method ? #840

Closed
subash-canapathy opened this issue May 17, 2016 · 20 comments · Fixed by #2429
Closed

Is there a way to get test status on afterEach() method ? #840

subash-canapathy opened this issue May 17, 2016 · 20 comments · Fixed by #2429
Labels
enhancement new functionality 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted scope:test-interface

Comments

@subash-canapathy
Copy link

subash-canapathy commented May 17, 2016

Issuehunt badges

mocha has a way of getting the test status on the after* hooks to do customized teardowns. currentTest.status
this is required for UI tests running on saucelabs and that feature in mocha was born out of the same need: mochajs/mocha#797

It would be super useful to have the test status exposed as metadata OR contextual information

Proposal A:
t.context.result - to contain the result object (the one which goes to the reporter)

Proposal B:
t.getTestStatus() - a method available only on the afterEach hook to get status of the test.


IssueHunt Summary

bunysae bunysae has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@jamestalmage
Copy link
Contributor

jamestalmage commented May 17, 2016

If we do this, it has to be B. You are allowed to completely replace the context:

t.context = someThing()

Also, it would have to be for after.always hooks (we don't run after on failures without that modifier). Note that feature not shipped yet (but should soon).

this is required for UI tests running on saucelabs and that feature in mocha was born out of the same need: mochajs/mocha#797

Three stars for describing a real world need for the feature! ⭐ ⭐ ⭐

@novemberborn
Copy link
Member

@subash-canapathy could you provide an example (in Mocha) of how you use this information with your Saucelabs tests? The issue you linked to doesn't provide that kind of context.

@subash-canapathy
Copy link
Author

in mocha, when you run UI tests, you typically have the afterEach block handle the session closeouts with sauceLabs depending on the status of the test. For eg:

afterEach(function(){
  // propagate test status/result to sauceLabs
  this.driver.sauceJobStatus(this.currentTest.state)  
  if (this.currentTest.state == 'failed') {
    // do some additional testOpsCleanup if test failed.
  }
  this.driver.quit()
});

Currently we have a work around for this on AVA

test.beforeEach(async t => {
  t.context.testStatus = false;
}

// Flip the flag if test passes - thats only when afterEach gets called
test.afterEach(async t => {
  t.context.testStatus = true;
});

test.afterEach.always(async t => {
  await t.context.driver.quit()
    .finally(function() {
      if (isSauceSuite) {
        return t.context.driver.sauceJobStatus(t.context.testStatus);
      }
    });
});

I mean this works and gets the job done, but as a test runner, i'd rather let AVA tell me if the test has passed or failed, and be able to get that info on the after* hooks. This is essential for conditional teardowns and cleanups.

@novemberborn
Copy link
Member

@subash-canapathy great example, thank you. I agree would be good if AVA could provide the plumbing for this.

@cgcgbcbc
Copy link
Contributor

it would have to be for after.always hooks

Since hooks are tests in fact, is there an easy way to achieve this ..?

In addition, this feature means access test "history" in a test, seems that Sequence.run must be modified for passing previous result to hooks run ?

@rhysd
Copy link
Contributor

rhysd commented May 26, 2016

I want this feature.

Three stars for describing a real world need for the feature!

I'm also currently using ava for E2E testing.
When test failed, I want to take a screenshot of the page before closing browser window. I'm thinking afterEach.always is suitable but there is no way to know the test result.

@jamestalmage
Copy link
Contributor

@rhysd - That is a really cool idea!

@subash-canapathy
Copy link
Author

@cgcgbcbc im not sure about Sequence.run, looking at the runner code I thought we could utilize the report callback to additionally put the status/result of the test in the context so that it still works reliably on concurrent tests (sort of like a thread-local result context)

@ngavrish
Copy link

Any updates on this guys? Will it be done in some visible perspective?

@frantic1048
Copy link

frantic1048 commented Apr 21, 2017

Same question here, I need to log noisy debug information if a test is failing.

Because .afterEach() and afterEach.always() hooks behave differently on test failing, I found a workaround:

import test from 'ava'

test.afterEach('this only runs when test succeed', t => {
  // set a success status in t.context
  t.context = 'success'
})

test.afterEach.always('this always runs', t => {
  if (t.context === 'success') {
    console.log('this test is succeed')
  } else {
    console.log('this test is failed')
    // log my noisy debug info
  }
})

test('success', t => {
  t.is(true, true, '')
})

test('fail', t => {   
  t.is(true, false, '')
})

You can initiate t.context as an Object in test.beforeEach() hook to carry more information. Here I just want to determine whether the test is failing.

NOTE: This workaround is still not reliable if criterias mentioned #928 are met, try to avoid them:

  • There are test failures and --fail-fast is used.
  • There are uncaught exceptions thrown.

@fruch
Copy link

fruch commented Sep 28, 2017

I'm also looking for this option, basicly cause I'm trying to write a plugin to capture my app log, and only print it out when test is failing.

since ava is runing test in paraller, it's even more importent since logging to a file would be a bit more complex

@novemberborn
Copy link
Member

@fruch and others, PRs or implementation specs are very much welcome for this feature. I'm happy to give feedback as you familiarize yourself with the problem and the code.

@fruch
Copy link

fruch commented Oct 1, 2017

I have something working, based on @frantic1048 idea, and that was good enough for me.

it would be nicer to get the actual status in the test context, so have visibility similar to the test reporter, like test.duration and more.

I understand most of it is internal, and can easily break between version.

is afterEach.always being called also for skipped and todo ? (more ideas come in my head, but most of them are in the reporter category)

@novemberborn
Copy link
Member

is afterEach.always being called also for skipped and todo ? (more ideas come in my head, but most of them are in the reporter category)

No it's not.

@lotosotol
Copy link

lotosotol commented Mar 19, 2019

I have a similar issue retrieving the status of the test suite, I am talking about the full test suite's status which I need to pass to BrowserStack (which accepts passed or failed) - would be great to have the ability to retrieve this before closing browser's window either before calling the .quit() method.

Oh, and I am running my tests in Mocha, not AVA and could not find a solution for the issue there as well.

@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label May 15, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $40.00 to this issue.


@tryzniak
Copy link
Contributor

Maybe it's worth introducing a new hook? For instance afterFail:

test.afterFail('only runs if a test failed', t => {
  // log, do screenshots
})

novemberborn added a commit that referenced this issue Apr 4, 2020
In `afterEach` hooks, consult `t.passed` to determine whether the test passed.

Fixes #840.

Co-authored-by: Mark Wubben <mark@novemberborn.net>
@bunysae
Copy link
Contributor

bunysae commented Apr 6, 2020

@novemberborn
Could you transfer the bounty?

@novemberborn
Copy link
Member

Could you transfer the bounty?

@sindresorhus knows how to do that 😄

@issuehunt-oss
Copy link

issuehunt-oss bot commented Apr 6, 2020

@sindresorhus has rewarded $36.00 to @bunysae. See it on IssueHunt

  • 💰 Total deposit: $40.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $4.00

@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt This issue has been funded on Issuehunt labels Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new functionality 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted scope:test-interface
Projects
None yet
Development

Successfully merging a pull request may close this issue.