Integrating with NUnit

When using Alba within NUnit testing projects, you probably want to reuse the IAlbaHost across tests and test fixtures because AlbaHost is relatively expensive to create (it's bootstrapping your whole application more than Alba itself is slow). To do that with NUnit, you could track a single AlbaHost on a static class like this one:

[SetUpFixture]
public class Application
{
    // Make this lazy so you don't build it out
    // when you don't need it.
    private static readonly Lazy<IAlbaHost> _host;

    static Application()
    {
        _host = new Lazy<IAlbaHost>(() => Program
            .CreateHostBuilder(Array.Empty<string>())
            .StartAlba());
    }

    public static IAlbaHost AlbaHost => _host.Value;

    // Make sure that NUnit will shut down the AlbaHost when
    // all the projects are finished
    [OneTimeTearDown]
    public void Teardown()
    {
        if (_host.IsValueCreated)
        {
            _host.Value.Dispose();
        }
    }
}

snippet source | anchor

Then reference the AlbaHost in tests like this sample:

public class sample_integration_fixture
{
    [Test]
    public Task happy_path()
    {
        return Application.AlbaHost.Scenario(_ =>
        {
            _.Get.Url("/fake/okay");
            _.StatusCodeShouldBeOk();
        });
    }
}

snippet source | anchor