Testavior: A Happy Solution To Test Your ASP.NET Core Applications

I talked several times about my vision of testing ASP.NET Core applications on this blog. The story has began here: A Different Approach To Test Your ASP.NET Core Application.
At the same time, I was compiling a set of libraries to industrialize this testing approach...

And here we are :) after several months of testing, I am hugely excited to present you: Testavior :D !

What is Testavior?

Testavior is a lightweight solution to help you develop Behavior Tests for ASP.NET Core.

What is a Behavior Test?

Behavior Tests are a way of testing your application features applying different types of behaviors to cover a functional scenario.

In other words:

This is all about testing the maximum of business with the minimum of work

Overview

Testavior helps you configure your application to easily integrate behavior tests for your scenarios. It provides a featured and highly configurable test environment for your behavior tests.

Here is a global features review:

  • Configured Test WebHost
  • Configured authentication context
    • Test authentication middleware
    • Configurable test identity
    • Identity claims helper
  • Configured Entity Framework Core context using SQLite provider with its In Memory mode
  • Serialization helper to handle URL encoded content
  • Set of HTTP tools to handle CSRF protection (very useful to test edition scenarios)
  • Assertion helper

Indeed, Testavior provides a pre-configured test environment to help you write your tests with minimum effort. If you use EF Core for your data access layer, you can use it as is:

[TestMethod]
public void ScenarioShouldBeOk()  
{
    var testEnvironment =
      new TestEnvironment<Startup, TestStartupConfigurationService<[EF_DB_CONTEXT]>>();

    var response = testEnvironment.Client.GetAsync("/api/data").Result;
    response.EnsureSuccessStatusCode();

    // Test result content
    var result =
      JsonConvert.DeserializeObject<Data[]>(response.Content.ReadAsStringAsync().Result);

    Assert.AreEqual("data", result.Data);
}

The pre-configured environment is represented by the TestStartupConfigurationService class. Thus, Testavior provides you a mocked in memory data access layer using the SQLite provider of EF Core. It provides also a mocked identity that you can easily override to test your authorizations matrix.

This pre-configured environment has been designed to suit most cases but it can be partially or totally overridden to fulfill your needs.

There is also plenty of other cool stuff for testing ASP.NET Core applications. If you want a more practical overview, go check the samples.

Here is the Testavior Github landing page, go and send me feedbacks, I can't wait to listen what you guys think about that :D

To help you write your behavior test. Don't forget to take a look to my others posts about Behavior Testing with ASP.NET Core ;):