V
V
Victor P.2022-04-13 10:04:05
Software testing
Victor P., 2022-04-13 10:04:05

How to make integration tests elegant?

Hello everyone,
I added an integration test project for my api according to the instructions:
https://docs.microsoft.com/en-us/aspnet/core/test/...

In almost every method in Arrange, you need to do two things: enter the initial data for the test into the database and send an authorization request. It takes up half a page and looks disgusting. Can you please tell me how to improve this code? It may be possible to transfer the preparation to some builders, but I don’t understand how it can be beautifully written. Here is an example of a test method, one user with the necessary rights (superadmin) is created in the database earlier and information about him is stored in a static file

public class EpicTests : IClassFixture<ApiApplicationFactory<Program>>
{
    private readonly ApiApplicationFactory<Program> _factory;

    public Epic(ApiApplicationFactory<Program> factory)
    {
        _factory = factory;
    }

    [Fact]
    public async Task Details()
    {
        // Arrange
        var epic = new Atheneum.Entity.Epic
        {
            Id = 42,
            Assignee = ArrangeUsers.SuperAdmin.Id,
            Reporter = ArrangeUsers.SuperAdmin.Id,
            Priority = IssuePriorityEnum.high,
            Name = "Тестовый эпик",
            Description = "Ребята решили протестировать санни дей на эпик",
            DueDate = new DateTime(2007, 1, 1)
        };

        var client = _factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureServices(services =>
            {
                var sp = services.BuildServiceProvider();

                using var scope = sp.CreateScope();
                var scopedServices = scope.ServiceProvider;
                var db = scopedServices.GetRequiredService<ApplicationContext>();

                db.Epic.Add(epic);
                db.SaveChanges();
            });
        }).CreateClient();

        var usr = new LoginDto()
        {
            Login = ArrangeUsers.SuperAdmin.UserName,
            Password = ArrangeUsers.SuperAdminPassword
        };
        await client.PostAsJsonAsync("/api/Auth/Login", usr);

        // Act
        var response = await client.GetAsync("/api/Epic/Details?id=42");

        // Assert
        await response.ShouldBeSuccessful();
        var result = await response.Content.ReadFromJsonAsync<EpicDto>();
        Assert.True(result.Id == epic.Id, "Id");
        Assert.True(result.Assignee == epic.Assignee, "Assignee");
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2022-04-13
@vabka


In almost every method in preparation (Arrange), you need to do two things: enter the initial data for the test into the database and send an authorization request.

xunit uses a constructor for this.
And if these are asynchronous calls, then the IAsyncLifetime.
There is taken out something that does not differ in any way from test to test.
And for cleaning up after running tests - Idisposable.Dispose

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question