BeforeScenario without tag vs BeforeScenario with tag

767 views
Skip to first unread message

Kevin

unread,
Nov 13, 2010, 11:07:36 PM11/13/10
to SpecFlow
Why does a [BeforeScenario] hook with no tag run AFTER a
[BeforeScenario] that does have a tag?

In the [BeforeScenario] with no tag I do some common initialization
for all scenarios, so I would expect it to run before any other
[BeforeScenario("some_tag")] scenario-specific tag.

Since the [BeforeScenario] WITH tag uses some component created in the
[BeforeScenario] without one, but runs BEFORE... boom.

Workaround?

Gáspár Nagy

unread,
Nov 14, 2010, 5:53:40 AM11/14/10
to SpecFlow
Hi,

Maybe you can solve this with the injection feature:

You can write a class that represents the "common initialization
logic". Write the logic into the constructor of the class.

public class CommonInit{
public CommonInit()
{
// do comon init logic
}
}

In the class(es) where you have the BeforeScenario WITH tags (make
sure that they are instance methods, not static), add an instance of
the CommonInit.

[Binding]
public class MyScenarioEvents
{
public MyScenarioEvents(CommonInit dummy){}

[BeforeScenario("sometag")]
public void MyEvent(){ ... }
}

When SpecFlow executes, it will create an instance of CommonInit (only
one, even if you inject it into multiple classes), so the constructor
will be executed before the scenario events with tags.

Br,
Gaspar

Jonas Bandi

unread,
Nov 14, 2010, 8:04:51 AM11/14/10
to spec...@googlegroups.com
I think right now we do not guarantee any ordering of execution of the
hooks of the same category.
So logic in the hooks should not rely on other hooks of the same category.

See gaspars answer for a possible solution without relying on ordered
hook execution.

The question is if it would be a desired feature to have ordered hook
execution and what would define the order (maybe another parameter in
the attribute)?

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

Kevin Krac

unread,
Nov 14, 2010, 3:54:14 PM11/14/10
to spec...@googlegroups.com
@Gaspar: yep, after writing the post a tried a solution very similar to the one you proposed. And it worked.
The difference of my solution is that instead of injecting an "initializer" to the constructor, I made my step class inherit from a "StepsBase" class that would do the initialization in its constructor.

@Jonas: not sure if specifying the order to the hooks is what I wanted. I just assumed a hook without tag was considered a "generic" one so it would be executed before any other hook with tag of the same category.
Reply all
Reply to author
Forward
0 new messages