Common Setup and TearDown actions in SpecFlow

7,275 views
Skip to first unread message

Eugene Strizhok

unread,
Jun 7, 2010, 6:20:01 AM6/7/10
to SpecFlow
How do I execute custom code on feature setup and teardown?
For example, a database related stuff such as transaction, which I do
not want to replicate in every test.
I though about specifying what class should test features generated by
specflow inherit from, but seems there is no such option.
How can I accomplish this?

Thank
Eugene

Jonas Bandi

unread,
Jun 7, 2010, 6:44:31 AM6/7/10
to spec...@googlegroups.com
There are hook methods:

[BeforeFeature()]
public static void Hook()
{
// perform setup
}

Similar there is [BeforeTestRun()], [BeforeScenario()] and the
according [After-] attributes.

Hooks can also be tagged:

@needs_extensive_setup
Feature: My feature
In order to run a feature requiring a complex setup
As a stakeholder
I want to signal the required setup with a tag

@needs_special_setup
Scenario: Relying of a common feature setup
Given bla bla
When bli bli bli
Then blu blu blu


Then in the step-definitions you can implement hooks that execute the
required setup only if the tag is present:

[BeforeFeature("needs_extensive_setup")]
public static void Hook()
{
int test = 42;
}

[BeforeScenario("needs_special_setup")]
public void Hook2()
{
int test = 42;
}

--
mail: jonas...@gmail.com
web: www.jonasbandi.net
blog: blog.jonasbandi.net
twitter: twitter.com/jbandi

Reply all
Reply to author
Forward
0 new messages