Skip to content

Commit

Permalink
(refactor) Update testCase to include pickle object
Browse files Browse the repository at this point in the history
  • Loading branch information
gd46 committed Oct 8, 2017
1 parent 6c8bad2 commit e9e021a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
40 changes: 40 additions & 0 deletions docs/support_files/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,46 @@ defineSupportCode(function({After, Before}) {
});
```

Before and After hooks return a testCase which contains a result(pass or fail), sourceLocation(test file uri and line number), and pickle(info on the current scenario)

```javascript
var {defineSupportCode, Status} = require('cucumber');

defineSupportCode(function({Before}) {
// Synchronous
Before(function (testCase) {

/*
* Check if scenarios has failed
* testCase.result.status === Status.FAILED
*
* testCase.pickle
* contains tags, name, locations, language, steps of the current scenarios
*
* testCase.pickle.name
* contains current scenario name
*
* testCase.pickle.tags
* contains an object array with the current scenarios tags
*
* testCase.sourceLocation
* contains uri and line number, uri is path to feature file, line number will give you current scenario
*
*/

/*
* How to check current scenario tags
*/

let tags = testCase.pickle.tags
_.forEach(tags, (tag.name) => {
// Provides you with the name of each tag
});

});
});
```

## Tagged hooks

Hooks can be conditionally selected for execution based on the tags of the scenario.
Expand Down
10 changes: 10 additions & 0 deletions features/hook_parameter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ Feature: Hook Parameters
defineSupportCode(({Before}) => {
Before(function(testCase) {
console.log(testCase.sourceLocation.uri + ":" + testCase.sourceLocation.line)
console.log('tags: ', testCase.pickle.tags);
console.log('name: ', testCase.pickle.name);
})
})
"""
When I run cucumber.js
Then the output contains the text:
"""
features/my_feature.feature:2
tags: []
name: a scenario
"""

@spawn
Expand Down Expand Up @@ -65,6 +69,8 @@ Feature: Hook Parameters
message += "did not fail"
}
console.log(message)
console.log('tags: ', testCase.pickle.tags);
console.log('name: ', testCase.pickle.name);
})
})
"""
Expand All @@ -73,8 +79,12 @@ Feature: Hook Parameters
And the output contains the text:
"""
features/my_feature.feature:2 did not fail
tags: []
name: a scenario
"""
And the output contains the text:
"""
features/my_feature.feature:5 failed
tags: []
name: another scenario
"""
4 changes: 3 additions & 1 deletion src/runtime/test_case_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ export default class TestCaseRunner {
this.emitPrepared()
this.emit('test-case-started', {})
await this.runHooks(this.beforeHookDefinitions, {
sourceLocation: this.testCaseSourceLocation
sourceLocation: this.testCaseSourceLocation,
pickle: this.testCase.pickle
})
await this.runSteps()
await this.runHooks(this.afterHookDefinitions, {
sourceLocation: this.testCaseSourceLocation,
pickle: this.testCase.pickle,
result: this.result
})
this.emit('test-case-finished', { result: this.result })
Expand Down

0 comments on commit e9e021a

Please sign in to comment.