Skip to content
suremaker edited this page Jul 2, 2023 · 34 revisions

Page version: 3.x / 2.x / 1.x

The acceptance tests written with LightBDD are represented by partial feature class, split between two files:

  • acceptance tests definition part containing easy to read scenarios,
  • implementation part containing scenario test implementation.

The first part contains:

  • optional feature description and label,
  • list of scenario methods with optional label and body with Runner executing scenario steps.
[TestFixture]
[FeatureDescription(
@"In order to access personal data
As an user
I want to login into system")] //feature description
[Label("Story-1")]
public partial class Login_feature //feature name
{
	[Scenario]
	[Label("Ticket-1")]
	[ScenarioCategory(Categories.Security)]
	public void Successful_login() //scenario name
	{
		Runner.RunScenario(

			Given_the_user_is_about_to_login, //scenario steps
			Given_the_user_entered_valid_login,
			Given_the_user_entered_valid_password,
			When_the_user_clicks_login_button,
			Then_the_login_operation_should_be_successful,
			Then_a_welcome_message_containing_user_name_should_be_returned);
	}
}

The second part contains:

  • implementation of all step methods
  • set up / tear down methods
  • inheritance of FeatureFixture base class (it is strongly recommended, however, it is possible also to not have it - please see Feature Fixture section).
public partial class Login_feature : FeatureFixture
{
	/* scenario data */
	/* set up / tear down methods */

	private void Given_the_user_is_about_to_login()
	{
		_loginService = new LoginService();
		_loginService.AddUser(_validUserName, _validPassword);
		_loginRequest = new LoginRequest();
	}
	
	private void When_the_user_clicks_login_button()
	{
		_loginResult = _loginService.Login(_loginRequest);
	}

	private void Then_the_login_operation_should_be_successful()
	{
		Assert.That(_loginResult.IsSuccessful, Is.True);
	}

	/* other step definitions */
}

Note: The feature class derives from FeatureFixture in order to get access to Runner property, that allows running scenarios!

Please note that the project contains a set of templates that can be used with Visual Studio to create Feature tests. To use them, please install LightBDD for Visual Studio extension from Visual Studio Marketplace.

Please also note that LightBDD supports various step method signatures, which are described in Scenario Steps Definition section.

Choosing integration

LightBDD bases on underlying test framework (NUnit / MsTest.TestFramework / xUnit / Fixie) to run the tests.

In order to integrate with chosen test framework, one of the following nuget package has to be added to the test project:

  • LightBDD.NUnit3
  • LightBDD.XUnit2
  • LightBDD.MsTest3
  • LightBDD.Fixie3

After chosen package is added, please add following code to the test assembly to enable the integration:

  • For NUnit 3 or xUnit 2 integration: [assembly: LightBddScope]
  • For MsTest 3 integration:
    [TestClass]
    public class LightBddIntegration
    {
        [AssemblyInitialize]
        public static void Setup(TestContext testContext) { LightBddScope.Initialize(); }
        [AssemblyCleanup]
        public static void Cleanup() { LightBddScope.Cleanup(); }
    }
  • For Fixie 3 integration:
    internal class ConfiguredLightBddScope : LightBddScope
    {
    }

If tests are run without integration in place, their execution will fail with message like following one (NUnit3 example):

LightBddScopeAttribute is not defined in the project. Please ensure that following attribute, 
or attribute extending it is defined at assembly level: [assembly:LightBddScopeAttribute]

More information about supported test frameworks, differences and integration details can be found on Test Framework Integrations page.

Running tests

After the underlying test framework is chosen, the LightBDD tests can be run in exactly the same way as regular tests in that framework.

During a run, the test progress will be printed on the console, like:

SCENARIO: [Ticket-1] Successful login
  STEP 1/6: GIVEN the user is about to login...
  STEP 1/6: GIVEN the user is about to login (Passed after 9ms)
  STEP 2/6: AND the user entered valid login...
  STEP 2/6: AND the user entered valid login (Passed after <1ms)
  STEP 3/6: AND the user entered valid password...
  STEP 3/6: AND the user entered valid password (Passed after <1ms)
  STEP 4/6: WHEN the user clicks login button...
  STEP 4/6: WHEN the user clicks login button (Passed after 182ms)
  STEP 5/6: THEN the login operation should be successful...
  STEP 5/6: THEN the login operation should be successful (Passed after 5ms)
  STEP 6/6: AND a welcome message containing user name should be returned...
  STEP 6/6: AND a welcome message containing user name should be returned (Passed after 14ms)
  SCENARIO RESULT: Passed after 233ms

After all the tests are finished, Reports\FeaturesReport.html will be created in the project output folder with scenario execution details.

To see the full HTML report, please take a look (here).

Please note that while creating FeaturesReport.html is the default behavior, more report types are supported and they can be customized in LightBDDScope setup - please see Generating Reports section for details.

Continue reading: Tests structure and conventions

Clone this wiki locally