Skip to content

Set 🔥 to your old tests! A better way to write tests in C# 🧪

License

Notifications You must be signed in to change notification settings

bmazzarol/Bunsen-Burner

Repository files navigation

Bunsen Burner

Bunsen Burner

🏃 Getting Started | 📚 Documentation

Nuget Coverage Quality Gate Status CD Build Check Markdown

Set 🔥 to your old tests! A better way to write tests 🧪 in C#.

Features

  • Test framework-agnostic
  • Zero dependencies
  • Easy to use DSL
  • More maintainable
  • Easier to refactor

Convert semantically equivalent tests from this,

[Fact(DisplayName = "SerializeAsync can work with anonymous objects")]
public async Task ExampleTest()
{
    // Arrange
    var widget = new { Name = "Widget1", Cost = 12.50 };
    var ms = new MemoryStream();

    // Act
    await JsonSerializer.SerializeAsync(ms, widget);

    // Assert
    Assert.Equal(
        expected: "{\"Name\":\"Widget1\",\"Cost\":12.5}",
        actual: Encoding.UTF8.GetString(ms.ToArray())
    );
}

to this,

[Fact(DisplayName = "SerializeAsync can work with anonymous objects")]
public Task ExampleTest() =>
     Arrange(() => new { Name = "Widget1", Cost = 12.50 })
    .Act(async widget =>
    {
        var ms = new MemoryStream();
        await JsonSerializer.SerializeAsync(ms, widget);
        return Encoding.UTF8.GetString(ms.ToArray());
    })
    .Assert(result => result == "{\"Name\":\"Widget1\",\"Cost\":12.5}");

Getting Started

To use this library, simply include BunsenBurner.dll in your project or grab it from NuGet, and add this to the top of each test .cs file that needs it:

// for AAA style tests
using static BunsenBurner.ArrangeActAssert;

or

// for BDD style tests
using static BunsenBurner.GivenWhenThen;

Note

Tests must always return a Task to be compatible with the DSL.

Then use the DSL to refactor your tests.

For more information, see the documentation.

Why?

Most tests in C# are written in an arrange, act, assert style. This is a good pattern, but it is only a convention.

This library aims to formalize this structure in the following ways,

  • Enforces that all tests must be arranged before acting and acted upon before assertions can occur. Making it a compile time error if you don't follow this pattern
  • Scaffolding tests using a fluent API making them easier to read, write and refactor
  • Encourages automatic refactoring of tests sections into helper methods, which is only possible if the test is structured using delegates
  • Works with the developers IDE to provide a better experience when writing tests

For more information have a look the documentation, or check out the test project or create an issue.

Attributions

Fire icons created by juicy_fish