Can't specify test setup and cleanup methods to only run on certain tests

666 views
Skip to first unread message

Pete

unread,
Nov 25, 2010, 12:21:22 AM11/25/10
to SpecFlow
Environment info:
I'm using visual studio 2010 with Nunit, testing using specflow,
running the tests using TestDriven.net

The problem:
I've got setup and cleanup methods that i want to run only for the
Feature test files they are in and not with the other feature test
files.
For example, I have the following file structure:
-One.feature (and One.feature.cs autocreated)
-OneStepDefinition.cs
-Two.feature (Two.feature.cs)
-TwoStepDefintion.cs

I define a setup metho in OneStepDefintion.cs like this:

[BeforeFeature("one")]
public static void TestSetup()
{
// setup test variables
}

and the One.feature file would look like this:

@one
Feature: One
In order to do something
As a customer
I want to do something

Scenario: Open a browser window and navigate to google
...

SO.........
I want this TestSetup to only run inside the One feature tests, which
is why i put the tag in, but with this in place, it doesn't get called
at all anymore. When i remove the tag, this setup method works, but
also gets called by the Two.feature class.
I only want it to run in One.feature.

I have tried a few variations of using BeforeFeature(), BeforeTestRun,
BeforeScenario but haven't been able to produce the desired response
(which I would have imagined should be the default situation that a
setup method would only run in the file it is located in unless
explicitly called by other files).

Hopefully that description makes sense, if more info is needed, let me
know

Pete

Steven Zhang

unread,
Nov 25, 2010, 3:26:50 AM11/25/10
to spec...@googlegroups.com
Hi Pete,

Base on your description, the "BeforeFeature" attribute should work.

I spent couple minutes on writing a PoC project, you may find it here https://gist.github.com/715076
--
Regards,
Steven Zhang

Pete

unread,
Nov 25, 2010, 5:22:05 PM11/25/10
to SpecFlow
Thanks for your reply Steven,

However, that method didn't work, it still just skips over the setup
method (and cleanup method) when i run the test.

Perhaps there is something about my environment that is making this
not work like normal? Is there any rules about the tags you use? Or
strict naming conventions about these tags, or anything else used with
Specflow?

My feature file has the top Feature definition, then a couple of
scenario definitions. The StepDefinitions file then has the methods to
match each of the Given/When/Then steps in the scenarios.

I run the tests by using TestDriven.Net, i left click on the test in
the code behind of the feature file (eg one.feature.cs) and click 'Run
Test(s)' which makes it run through Nunit I believe. Maybe i need to
run them a different way?

Maybe it's because i have windows 7 with visual studio 2010??

As you can see I'm grasping at straws here, i'll put a subset of my
actual code here, see if that sheds any light

using System;
using NUnit.Framework;
using TechTalk.SpecFlow;
using Tests.Common;
using Tests.TestData;

namespace Tests.API.FunctionalTests
{
[Binding]
public class ClientStepDefinition
{
private static ClientApi _clientApi;
private static APIMethodsV3 _apiMethods;
private static Customer _client;
private static string _clientId;

[BeforeFeature("clientTag")]
public static void Setup()
{
_clientApi = new ClientApi();
_apiMethods = new APIMethodsV3();
}

[AfterFeature("clientTag")]
public static void TearDown()
{
if (_clientId != null)
{
var result = _clientApi.DeleteClient(_clientId, Json);
_apiMethods.CheckSuccessResult(result, Json);
}
if (_client != null)
_client.CloseBrowserAndEndTest();
}

[Given(@"I have created a new client")]
public void GivenIHaveCreatedANewClient()
{
var client = new ClientDetail


and the feature file:

@clientTag
Feature: Client
In order to know that I can set up new clients using the API
As a designer
I want to create a client and check their access

Scenario: Create a new client with level 1 access
Given I have created a new client
When I give them level 1 access
Then I should be able to access some stuff

Scenario: Create a new client with level 2 access
Given I have created a new client
When I give them level 2 access
Then I should be able to access more stuff

Pete

unread,
Nov 25, 2010, 6:18:23 PM11/25/10
to SpecFlow
OK, I've figured it out... First I observed that my code behind file
for the feature file didn't seem to be updating properly when i made a
change to the feature file since nothing changed in the code behind
when i made a change in the feature file... Hence, the feature tag
wasn't being recognised.

After consulting a team member, I discovered that the project given to
me to work with using specflow required me to download and install
specflow on my computer as well (from http://specflow.org/downloads/installer.aspx).
Sounds obvious right?? Well not so much when you see that Specflow
will almost work perfectly on your machine without being installed.
The tests will run, it will interpret the step definitions ok.. BUT,
it won't auto-update your feature code-behind files..

Bottom line, my code now works as expected using the
BeforeFeature("tagName") method. Anyone seeing similar issues, make
sure you have specflow installed and make sure that your feature code
behind file is being updated with your changes properly :)

cheers, Pete

Steven Zhang

unread,
Nov 25, 2010, 9:19:50 PM11/25/10
to spec...@googlegroups.com
True. The specflow uses CustomTool generator to code-gen for .feature file.

When you get specflow for visual studio installed, you could see the "CustomTool" property of ".feature" file was specified to "SpecFlowSingleFileGenerator".

Or you may see "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\.feature" was created in registry.

If you could not see them, that means you have not installed specflow properly.
--
Regards,
Steven Zhang

Gáspár Nagy

unread,
Nov 26, 2010, 2:58:36 PM11/26/10
to SpecFlow
Hi,

One additional option to consider:

You can configure the project to re-generate (if necessary) the code
files from the feature file before each compilation (see details in
the user's guide draft). This way you can use specflow even on
machines where the Visual Studio integration is not installed.

(With the VS integration you get syntax coloring and other freatures
as well.)

Br,
Gaspar
Reply all
Reply to author
Forward
0 new messages