Skip to content
MorganPersson edited this page Oct 22, 2012 · 4 revisions

Hooks can be used to additional automation logic on specific events, like before the execution of a feature.

Hooks live in the namespace

NBehave.Narrator.Framework.Hooks

Hooks are global.

Types of hooks

Attribute Description
BeforeRun Runs before anything else in your test run
AfterRun The last thing that get to execute in the test run
BeforeFeature Runs before a feature
AfterFeature Runs after a feature
BeforeScenario Runs before a scenario
AfterScenario Runs after a scenario
BeforeStep Runs before a step
AfterStep Runs after a step

You can set a hook attributes on methods and delegates. public or private doesn't matter

using NBehave.Narrator.Framework;
using NBehave.Narrator.Framework.Hooks;

[Hooks]
public class MyHooks
{
    [Hooks.BeforeScenario, Hooks.BeforeStep]
    private void HookMeUp()
    {
        if (ScenarioContext.Current.Tags.Contains("foo"))
        {
            //do stuff
        }
    }

    [Hooks.BeforeRun]
    public Action PublicDelegateHook = () => { /* do stuff here */ };

}

Dont use in the attributes BeforeScenario, AfterScenario, BeforeStep and AfterStep in namespace NBehave.Narrator.Framework, they are deprecated.

Clone this wiki locally