Transcript

In the previous chapter, we learned how we can work with table arguments for our steps effectively using data table utility methods provided by Reqnroll. Before we start looking at properly implementing our step definition methods using some of the most popular C# test automation libraries, there are two more features of Reqnroll that deserve a closer inspection. These are hooks and tags. Let's start with Reqnroll hooks.

When you're writing automated acceptance tests, there will always be code that needs to be run before and after each test, each test suite, or each test run. In other words, your tests will often require setup and teardown code. For example, you might need to spin up a specific service, fill or empty a database, clear logs, and so on.

Including that setup and teardown code in each of your tests will result in a lot of duplicated and messy code. Additionally, when you're using Reqnroll and Gherkin to document the desired behaviour of your system, you often do not want to explicitly mention these setup and teardown steps in your scenarios, as that would only distract from the actual behaviour that is described.

Imagine that, in the case of our loan application feature, we need to make sure that the loan processor service is up and running before every scenario, and that it is properly stored and log files are backed up afterwards. We probably do not want to pollute the scenarios we wrote in previous chapter with additional steps to trigger these actions, not even in a Background. Starting and stopping the service and taking care of the log files is a technical requirement in order to ensure smooth execution of our acceptance tests, but it is not behaviour that we want to document in our scenarios.

This is where hooks are useful. Hooks are essentially event bindings that you can use to make sure that specific code is, for example, run before or after each scenario, or even before or after each step within a scenario. To be precise, Reqnroll offers five different levels of hooks:

BeforeTestRun and AfterTestRun - These are used to run code just before or just after the entire test run BeforeFeature and AfterFeature - These are used to run code just before or just after every feature that is executed as part of a test run BeforeScenario and AfterScenario - These are used to run code just before or just after each scenario that is run as part of a test run BeforeScenarioBlock and AfterScenarioBlock - These are used to run code just before or just after each section of a scenario, where a section means 'all the Given steps in a scenario', for example BeforeStep and AfterStep - These are run just before or just after each individual step in a scenario

Here's an example of what that will look like in your code. I've added a method with a BeforeScenario attribute, as well as one with an AfterScenario attribute. The BeforeScenario method will be invoked by Reqnroll just before the first step in every scenario that is executed in a specific test run, and the AfterScenario is run just after the last step in every scenario in the test run.

For all of the After hooks, it is good to remember that code tagged with these hooks is guaranteed to run, no matter what happens during execution of the preceding code. In our example, no matter if the scenario passes, if it fails because of a failed assertion or or if it fails because of an uncaught exception, the AfterMethod will always be executed. It is therefore a great place to do some tidying up before the next scenario is run.

Of these five, I personally use BeforeScenario and AfterScenario most often in practice, although there are use cases to be found for every type of hook in the list. A very common use case for BeforeScenario and AfterScenario, for example, are starting and stopping a browser in a user interface-driven acceptance test. We'll see a working example of this in the next chapter.

The other concept I would like to briefly cover in this video is that of tags.

In Reqnroll, you can decorate your features and scenarios with tags to group and filter them. Tags are optional, and you can add as many tags as you want to entire features, or to specific scenarios within a feature file.

As an example, I've added the loanapplication tag to our feature file here, to indicate that this feature describes part of the behaviour around loan applications for our online banking platform. It's good to know that decorating a feature file with a tag automatically applies that tag to all scenarios in the feature file.

When you run acceptance tests described in Reqnroll feature files and scenarios, you can tell the test runner to only run those features and scenarios decorated with a specific tag, or, alternatively, to exclude features and scenarios with a specific tag from your test run.

Some common use cases of tags are:

  • Grouping feature files and scenarios together so they can be run as a whole
  • Marking specific features and scenarios as work in progress or to be ignored

Another useful feature of Reqnroll is the fact that you can scope hooks to specific tags. In other words, you can tell Reqnroll to only execute specific Before and After methods for features and scenarios that are decorated with a specific tag.

In this example, I've scoped the methods with the BeforeScenario and AfterScenario attributes to feature files and scenarios containing the loanapplication tag. Reqnroll will execute these methods just before and just after every scenario that either is decorated with the loanapplication tag itself, or is contained in a feature file decorated with the loanapplication tag.

Also note that using named arguments as I did here is entirely optional, but in this case, I think it makes the code easier to read.

Besides hooks, you can also scope individual step definition methods and even entire step definitions classes to specific tags. For example, if I add the Scope attribute with the Tag argument set to loanapplication to our step definitions class, all step definitions inside this class will be mapped only to features and scenarios with that tag. If I deactivate the tag, for example, you'll see that Reqnroll can no longer find the corresponding step definitions, because the ones we have only apply to features and scenarios with the loanapplication tag.

This enables you to implement the same steps in different ways for different types of tests, for example. We'll see a working example of this in the next chapter.

In that chapter, we will finally turn our Reqnroll scenarios and step definitions into proper automated acceptance tests on our online banking application by means of implementing the actions described in our step definitions using some of the most popular UI and API automation libraries available in the C# ecosystem.

The current state of this project is available on GitHub, in the 'chapter8' branch of the repository you see linked in the video right now.



Resources



© 2026 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR