It sounds as if you want to have a common background in several different function files. There are at least two different ways to do this:
Composite steps
One way to do this is what AlSki suggested, creating a complex step that calls for many other steps. I will not review this here because he did an excellent job with the answer. You can call this composite step (a step that calls many steps) from any script (or background) in any function that you like.
Hook tag filtering
Another approach is to use the Hooks defined in SpecFlow, for example [BeforeScenario] to run code before each script. You can use tag filtering (see Link for interceptors) to specify which launch to launch by adding the appropriate tag to the script or function. Let me show you an example:
Say I have scripts that run using Selenium to control a web browser, but not all of my scripts use Selenium. If I wanted to configure Selenium only for scripts that need it, I can create a BeforeScenario binding that only runs if the script has an @web tag.
Here is my LoggingIn.feature file:
Feature: Logging In @web Scenario: Log In Given I am on the login page When I supply valid credentials Then I should be taken to the homepage
Here is my StepDefinitions.cs step definition file:
[Binding] public class StepDefinitions { [BeforeScenario("web")] public static void BeforeWebScenario() {
For any script that has an @web tag, both BeforeAllScenarios() and BeforeWebScenario() will be executed before the script starts. For a script that does not have an @web tag, only the BeforeAllScenarios() method will be executed.
That way, you can simply run the code set by applying a specific attribute to the script.
FYI: Starting with SpecFlow 1.9, you cannot specify the order in which these hooks are executed if more than one is specified.
When to use this or that?
It depends on whether you fix technical problems or business problems.
Use tags to configure technical issues
If you want to customize some technical aspect of your test that a business user should not know about, I would use a tag approach. A good example is the one I introduced ... the creation of Selenium. A business user can take care of what Selenium is, so it makes no sense to create a step in the script that sets it up.
Use complex steps to set up business tasks
If you need to indicate the state of the system that a business user is interested in (for example, a registered user or existing product data), this should be another step in the script (or in the background). This is because the steps should include everything you need to read and understand behavior from a business perspective.