Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ Implemented pieces include:
| Comments (`#`) | Supported |
| Tags (`@tag`) | Supported |
| `Before`, `After` hooks | Supported |
| `BeforeAll`, `AfterAll` hooks | Supported |
| `Rule` | Unsupported |
| `Scenario Outline` | Unsupported |
| `Examples` | Unsupported |
| Data tables | Unsupported |
| `BeforeAll`, `AfterAll` hooks | Unsupported |

Tags can be selected with `--tag` / `-t` and skipped with `--exclude-tag` / `-x`.
`@Before` and `@After` hooks are declared in step definition files and may be
limited to a tag.
limited to a tag. `@BeforeAll` and `@AfterAll` hooks are untagged and wrap the
selected scenario run.

## Usage

Expand Down Expand Up @@ -264,10 +265,12 @@ file.

## Step Definition Hooks

Step definition files can also declare scenario hooks with `@Before` and
`@After`.
Step definition files can also declare hooks.

```bash
@BeforeAll
./suite-setup

@Before
mkdir -p tmp

Expand All @@ -279,13 +282,22 @@ Step definition files can also declare scenario hooks with `@Before` and

@After @needs-server
./server stop

@AfterAll
./suite-teardown
```

Hooks without a tag run for every scenario. Tagged hooks run only for scenarios
with that tag, including tags inherited from the feature. `@Before` hooks run
before background and scenario steps. `@After` hooks run after the scenario
steps, even when a step or `@Before` hook fails.

`@BeforeAll` runs once before the first selected scenario executes. `@AfterAll`
runs once after the last selected scenario executes. If no scenario is selected,
neither all-hook runs. All-hooks do not accept tags. If `@BeforeAll` fails, the
run aborts immediately and `@AfterAll` does not run. If `@AfterAll` fails, the
run fails.

Hooks can call helper functions from `support.sh`:

```bash
Expand Down
2 changes: 0 additions & 2 deletions doc/shellkin-feature.5
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ The following common Gherkin constructs are not currently supported:
\f[B]Examples\f[R]
.IP \(bu 2
data tables
.IP \(bu 2
\f[B]BeforeAll\f[R] and \f[B]AfterAll\f[R] hooks
.SH EXAMPLE
.IP
.EX
Expand Down
1 change: 0 additions & 1 deletion doc/shellkin-feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ The following common Gherkin constructs are not currently supported:
- **Scenario Outline**
- **Examples**
- data tables
- **BeforeAll** and **AfterAll** hooks

EXAMPLE
==================================================
Expand Down
19 changes: 17 additions & 2 deletions doc/shellkin-stepdefs.5
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ Then the text should include \(aqJim \(dqJimbo\(dq Jackson\(aq
The opening and closing quote in the feature step must match.
Quoted token patterns do not match unquoted values.
.SH HOOKS
Step definition files can also declare scenario hooks with
\f[B]\(atBefore\f[R] and \f[B]\(atAfter\f[R].
Step definition files can also declare hooks.
.IP
.EX
\(atBeforeAll
./suite\-setup

\(atBefore
mkdir \-p tmp

Expand All @@ -90,6 +92,9 @@ Step definition files can also declare scenario hooks with

\(atAfter \(atneeds\-server
./server stop

\(atAfterAll
./suite\-teardown
.EE
.PP
Hooks without a tag run for every scenario.
Expand All @@ -104,6 +109,16 @@ remaining steps are skipped.
\f[B]\(atBefore\f[R] hook fails.
If an \f[B]\(atAfter\f[R] hook fails, the scenario fails.
.PP
\f[B]\(atBeforeAll\f[R] runs once before the first selected scenario
executes.
\f[B]\(atAfterAll\f[R] runs once after the last selected scenario
executes.
If no scenario is selected, neither all\-hook runs.
All\-hooks do not accept tags.
If \f[B]\(atBeforeAll\f[R] fails, the run aborts immediately and
\f[B]\(atAfterAll\f[R] does not run.
If \f[B]\(atAfterAll\f[R] fails, the run fails.
.PP
Passing hooks are quiet.
Failing hooks are shown in the error report.
.PP
Expand Down
15 changes: 13 additions & 2 deletions doc/shellkin-stepdefs.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ patterns do not match unquoted values.
HOOKS
==================================================

Step definition files can also declare scenario hooks with **@Before** and
**@After**.
Step definition files can also declare hooks.

```bash
@BeforeAll
./suite-setup

@Before
mkdir -p tmp

Expand All @@ -106,6 +108,9 @@ Step definition files can also declare scenario hooks with **@Before** and

@After @needs-server
./server stop

@AfterAll
./suite-teardown
```

Hooks without a tag run for every scenario. Tagged hooks run only for scenarios
Expand All @@ -117,6 +122,12 @@ hook fails, the scenario fails and the remaining steps are skipped.
**@After** hooks run after scenario steps, even when a step or **@Before** hook
fails. If an **@After** hook fails, the scenario fails.

**@BeforeAll** runs once before the first selected scenario executes.
**@AfterAll** runs once after the last selected scenario executes. If no
scenario is selected, neither all-hook runs. All-hooks do not accept tags.
If **@BeforeAll** fails, the run aborts immediately and **@AfterAll** does not
run. If **@AfterAll** fails, the run fails.

Passing hooks are quiet. Failing hooks are shown in the error report.

Hooks can call helper functions from **support.sh**:
Expand Down
4 changes: 3 additions & 1 deletion features/fixtures/hooks/hooks.feature
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Feature: hooks
Before and After hooks
Step definition hooks

@needs-server
Scenario: tagged hooks run before matching scenarios
Then the hook log should include 'before-all'
Then the hook log should include 'before'
And the hook log should include 'server-start'

Scenario: after hooks run after scenarios
Then the hook log should include 'before-all'
Then the hook log should include 'after'
And the hook log should include 'server-stop'
13 changes: 13 additions & 0 deletions features/fixtures/hooks/step_definitions/core.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@BeforeAll
printf 'before-all\n' >>"$HOOK_LOG"

@Before
printf 'before\n' >>"$HOOK_LOG"

Expand All @@ -10,5 +13,15 @@
@After @needs-server
printf 'server-stop\n' >>"$HOOK_LOG"

@AfterAll
printf 'after-all\n' >>"$HOOK_LOG"
printf 'HOOK_COUNTS before-all=%s before=%s after=%s after-all=%s server-start=%s server-stop=%s\n' \
"$(grep -c '^before-all$' "$HOOK_LOG")" \
"$(grep -c '^before$' "$HOOK_LOG")" \
"$(grep -c '^after$' "$HOOK_LOG")" \
"$(grep -c '^after-all$' "$HOOK_LOG")" \
"$(grep -c '^server-start$' "$HOOK_LOG")" \
"$(grep -c '^server-stop$' "$HOOK_LOG")"

@Then the hook log should include '{text}'
[[ "$(cat "$HOOK_LOG")" == *"$text"* ]]
3 changes: 3 additions & 0 deletions features/step_definitions/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
@Then the output should include '{text}'
[[ "$(printf '%s' "$LAST_STDOUT" | strip_ansi)" == *"$text"* ]]

@Then the output should not include '{text}'
[[ "$(printf '%s' "$LAST_STDOUT" | strip_ansi)" != *"$text"* ]]

@Then the error output should include '{text}'
[[ "$(printf '%s' "$LAST_STDERR" | strip_ansi)" == *"$text"* ]]

Expand Down
1 change: 1 addition & 0 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Scenario: Filtering scenarios by tag
Scenario: Running hooks from step definition files
When I run 'shellkin features/fixtures/hooks'
Then the output should include 'Feature: hooks'
And the output should include 'HOOK_COUNTS before-all=1 before=2 after=2 after-all=1 server-start=1 server-stop=1'
And the output should include '2 scenarios, 0 failing'
And the exit code should mean success

Expand Down
Loading
Loading