Git Product home page Git Product logo

jaredcnance / dotnettestabstractions Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 45 KB

A set of abstractions for improving the performance and DX when writing integration tests

Home Page: https://nance.io/a-better-story-for-asp-net-core-integration-testing/

License: MIT License

C# 92.02% PowerShell 7.12% Shell 0.86%
aspnetcore asp-net-core entity-framework-core xunit autofac testing dotnet-core dotnetcore

dotnettestabstractions's Introduction

DotnetTestAbstractions

NuGet Pre Release Build status

Provides a set of abstractions for improving the performance and DX when writing integration tests.

Read the blog post here.

Usage

See the Examples for usage details.

Write Easy to Read Tests Without Worrying About Leaky State

public class ArticlesController_Tests : JsonServerFixture<TestStartup, AppContext>
{
    [Fact]
    public async Task Can_Get_All_Articles()
    {
        // arrange
        var article = new Article();
        await DbContext.Articles.AddAsync(article);
        await DbContext.SaveChangesAsync();

        // act
        var articles = await GetAllAsync<Article>("articles");

        // assert
        Assert.Single(articles);
    }
}

Features

  • Management of TestServer instances
  • Database calls get wrapped in a transaction that is shared between the test and server
  • Easy to use APIs for executing RESTful JSON HTTP requests
  • Reset the database before any tests run by setting the environment variable DROP_DATABASE_ONSTART=true
  • Enable logging by setting the environment variable DTA_LOG_LEVEL=Information to the approproate log level (see Microsoft.Extensions.Logging.LogLevel for valid values.

Development

Pre-Requisites

Need a running SQL Server on localhost. You can do this by pulling the SQL Server docker container:

docker run \
    -e 'ACCEPT_EULA=Y' \
    -e 'SA_PASSWORD=P@ssword1' \
    -p 1433:1433 \
    --name DotnetTestAbstractions \
    -d microsoft/mssql-server-linux:2017-latest

dotnettestabstractions's People

Contributors

jaredcnance avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

n2retail

dotnettestabstractions's Issues

Allow opt-out of transaction

This is useful when the application needs to create its own transaction but will fail since EF does not support cross-platform nested transactions.

Document Usage

  • Create test Startup class

public override IServiceProvider ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
return services.UseAmbientDbConnection<AppContext>();
}

  • How to write custom fixtures that inherit from the base fixtures
  • Avoid inheriting Startup. Underlying registrations may depend on the current assembly type. By inheriting, the return value of GetType().Assembly will become the test assembly rather than the application.
  • Beware injections of scoped DbContext instances into singleton instances. You can enable debug logging to see which transaction ids are being used. If the same transaction is used for multiple tests, you probably have a registration scope issue. In this case, walk the registration tree upwards from the DbContext implementation, making sure all components are registered as scoped services.

xUnit Enhancements

  • Assert.Equals with string message e.g.:
// act
var response = await _fixture.Client.GetAsync("/api/v1/articles");

// assert
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"Received {response.StatusCode} status code with payload: {body}");

Any chance of .NET Core 3.0 support?

It seems this no longer works with .NET Core 3.0, possibly because they changed the way ConfigureServices() work. I have no clue how to get it working. Any chance of an update or some pointers?

Support vanilla fixtures that do not use transactions

Should provide the same abstractions as JsonServerFixture but use any startup desired

  • No container dependency, services would have to be resolved from the application level service provider
  • Maybe we can resolve the default scope provider and it will just work?
  • Alternative for #3 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.