Skip to content

GherXUnit emerges as a viable alternative for those seeking an approach that combines the expressiveness of BDD with the well-established structure of xUnit.

License

Notifications You must be signed in to change notification settings

emergingcode/gherxunit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

62 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ GherXUnit: An Alternative for BDD with xUnit

πŸ‡§πŸ‡· VersΓ£o em PortuguΓͺs | πŸ‡¬πŸ‡§ English Version
NuGet

The adoption of Behavior-Driven Development (BDD) has become increasingly common in software development, promoting better communication between technical and non-technical teams. However, its integration with traditional testing frameworks is not always straightforward.

Important

According to the study Behavior Driven Development: A Systematic Literature Review (Farooq et al., 2023, IEEE Access), some recurring difficulties in using BDD include:

  • Complex automation: Integration with external tools can increase configuration and test execution complexity.
  • Difficult maintenance: As the test base grows, Gherkin scenarios can become hard to manage.
  • Learning curve: The need to master new tools can hinder BDD adoption, especially in teams already familiar with traditional frameworks.

GherXunit emerges as a viable alternative for teams looking to explore the benefits of BDD within the xUnit framework, without requiring external tools such as Cucumber or SpecFlow. It acts as a superset of xUnit, allowing tests to be written in Gherkin.

βœ… Where Can GherXunit Help?

GherXunit aims to offer an alternative for teams already using xUnit and looking to incorporate the BDD structure without completely changing their tools. Among its benefits are:

  • βœ” Using Gherkin syntax directly in xUnit, reducing external dependencies.
  • βœ” More modular and organized code, using partial classes to separate scenarios and steps.
  • βœ” Better integration with unit tests, allowing a smoother transition between different levels of testing.

πŸ“¦ Getting Started

This package is available through Nuget Packages.

Version Downloads Status
NuGet Nuget .NET

πŸ’‘ How Does It Work?

The core idea of GherXunit is to allow test scenarios to be written in a structure familiar to those already using xUnit. For that, it provides a set of attributes and methods that allow the definition of test scenarios using Gherkin syntax. The following sections provide examples of how to define test scenarios and implement step methods using GherXunit.

πŸ“Œ Example of Scenario Definition:

The following code snippet shows a test scenario defined using Gherkin syntax in a class named SubscriptionTest:

using GherXunit.Annotations;
...

[Feature("Subscribers see different articles based on their subscription level")]
public partial class SubscriptionTest
{
    [Scenario("Free subscribers see only the free articles")]
    async Task WhenFriedaLogs() => await this.ExecuteAscync(
        refer: WhenFriedaLogsSteps,
        steps: """
               Given Free Frieda has a free subscription
               When Free Frieda logs in with her valid credentials
               Then she sees a Free article
               """);

    [Scenario("Subscriber with a paid subscription can access both free and paid articles")]
    void WhenPattyLogs() => this.Execute(
        refer: WhenPattyLogsSteps,
        steps: """
               Given Paid Patty has a basic-level paid subscription
               When Paid Patty logs in with her valid credentials
               Then she sees a Free article and a Paid article
               """);
}

πŸ“Œ Example of Step Implementation:

The following code snippet shows the implementation of the step methods for the test scenario defined in the SubscriptionTest class:

public partial class SubscriptionTest(ITestOutputHelper output): IGherXunit
{
    public ITestOutputHelper Output { get; } = output;
    private void WhenPattyLogsSteps() => Assert.True(true);
    private async Task WhenFriedaLogsSteps() => await Task.CompletedTask;
}

Tip

In this example, the SubscriptionTest class is split into two files. The first file defines the test scenarios, while the second file defines the step methods. Using partial allows both files to contribute to the definition of the same SubscriptionTest class.

πŸ“Œ Example of output highlighting the test results:

The result of running the test scenarios defined in the SubscriptionTest class would be similar to the following output:

TEST RESULT: 🟒 SUCCESS
β€· FEATURE Subscribers see different articles based on their subscription level
  β€· SCENARIO Free subscribers see only the free articles
    | GIVEN β†˜ Free Frieda has a free subscription
    |  WHEN β†˜ Free Frieda logs in with her valid credentials
    |  THEN β†˜ she sees a Free article

TEST RESULT: 🟒 SUCCESS
β€· FEATURE Subscribers see different articles based on their subscription level
  β€· SCENARIO Subscriber with a paid subscription can access both free and paid articles
    | GIVEN β†˜ Paid Patty has a basic-level paid subscription
    |  WHEN β†˜ Paid Patty logs in with her valid credentials
    |  THEN β†˜ she sees a Free article and a Paid article

✏️ Customizing the lexical elements of Gherkin

The GherXunit allows you to customize the lexical elements of Gherkin, such as Given, When, Then, And, Background, Scenario, and Feature. You can define your custom emojis or symbols to represent these elements. The following code snippet shows an example of a custom lexer for emojis:

// Custom lexer for emojis
public record EmojiGherXunitLexer : IGherXunitLexer
{
    public (string Key, string Value)[] Given => [("Given", "\ud83d\ude10")];
    public (string Key, string Value)[] When => [("When", "\ud83c\udfac")];
    public (string Key, string Value)[] Then => [("Then", "\ud83d\ude4f")];
    public (string Key, string Value)[] And => [("And", "\ud83d\ude02")];
    public string Background => "\ud83d\udca4";
    public string Scenario => "\ud83e\udd52\ud83d\udcd5";
    public string Feature => "\ud83d\udcda";
}

The Gherkin provides two built-in lexers: Lexers.PtBr for Portuguese (πŸ‡΅πŸ‡ΉπŸ‡§πŸ‡·) and Lexers.EnUs for English (πŸ‡ΊπŸ‡Έ). You can also create your custom lexer by implementing the IGherXunitLexer interface. To use the custom lexer, you need to pass it as a parameter when defining the test scenario.

[Feature("Subscribers see different articles based on their subscription level")]
public partial class LocalizationTest
{
    // Using Portuguese (πŸ‡΅πŸ‡ΉπŸ‡§πŸ‡·) lexer
    [Scenario("Inscrever-se para ver artigos gratuitos")]
    async Task WhenFriedaLogs() => await this.ExecuteAscync(
        refer: WhenFriedaLogsSteps,
        lexer: Lexers.PtBr,
        steps: """
               Dado Free Frieda possui uma assinatura gratuita
               Quando Free Frieda faz login com suas credenciais vΓ‘lidas
               EntΓ£o ela vΓͺ um artigo gratuito
               """);

    // Using custom emoji lexer
    [Scenario("Subscriber with a paid subscription can access both free and paid articles")]
    void WhenPattyLogs() => this.Execute(
        refer: WhenPattyLogsSteps,
        lexer: new EmojiGherXunitLexer(),
        steps: """
               Given Paid Patty has a basic-level paid subscription
               When Paid Patty logs in with her valid credentials
               Then she sees a Free article and a Paid article
               """);
}

The result of running the test scenarios defined in the LocalizationTest class using the custom lexer would be similar to the following output:

TEST RESULT: 🟒 SUCCESS
β€· FUNCIONALIDADE Subscribers see different articles based on their subscription level
  β€· CENARIO Inscrever-se para ver artigos gratuitos
    |   DADO β†˜ Free Frieda possui uma assinatura gratuita
    | QUANDO β†˜ Free Frieda faz login com suas credenciais vΓ‘lidas
    |  ENTΓƒO β†˜ ela vΓͺ um artigo gratuito
    
TEST RESULT: 🟒 SUCCESS
β€· πŸ“š Subscribers see different articles based on their subscription level
  β€· πŸ₯’πŸ“• Subscriber with a paid subscription can access both free and paid articles
    | 😐 β†˜ Paid Patty has a basic-level paid subscription
    | 🎬 β†˜ Paid Patty logs in with her valid credentials
    | πŸ™ β†˜ she sees a Free article and a Paid article    

πŸ”Ž Is GherXunit for You?

If your team already uses xUnit and wants to experiment with a BDD approach without drastically changing its workflow, GherXunit may be an option to consider. It does not eliminate all BDD challenges but seeks to facilitate its adoption in environments where xUnit is already widely used. See more usage examples and implementation details for Background, Rule, Features, and other elements in the sample code available in the GherXunit repository.

πŸ“š References

About

GherXUnit emerges as a viable alternative for those seeking an approach that combines the expressiveness of BDD with the well-established structure of xUnit.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages