Intro
To debug testing codes, I want to add loggers into my xUnit project.
- 【ASP.NET Core】【xUnit】【Moq】Add unit tests 1
- 【ASP.NET Core】【xUnit】Testing Entity Framework Core applications with in-memory SQLite
Environments
- .NET ver.6.0.101
- Microsoft.NET.Test.Sdk ver.16.11.0
- xUnit ver.2.4.1
- xunit.runner.visualstudio ver.2.4.3
- coverlet.collector ver.3.1.0
- Microsoft.EntityFrameworkCore.Sqlite ver.6.0.1
- Moq ver.4.16.1
Add loggers
In xUnit projects, I can't use "Console.WriteLine()" to log.
According to the document, to run tests in parallel, I should use "ITestOutputHelper".
BookTest.cs
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Moq; using Xunit; using BookshelfSample.Books; using BookshelfSampleTest.Models; using BookshelfSample.Models; using BookshelfSample.Models.SeedData; using Xunit.Abstractions; namespace BookshelfSampleTest.Books; public class BooksTest: IDisposable { ... private readonly ITestOutputHelper output; public BooksTest(ITestOutputHelper output) { this.output = output; this.output.WriteLine("Hello"); ... } public void Dispose() { this.databaseFixture.Dispose(); } ...
To output logs, I have to add "logger" option into "dotnet test" command.
dotnet test BookshelfSampleTest --logger "console;verbosity=detailed"
Result
How I shall run tests?
As same as this post, I run tests by ".NET Core Test Explorer" on VSCode.
But the logs aren't written in "OUTPUT" tab.
Test run for /home/example/Documents/workspace/BookshelfSample/BookshelfSampleTest/bin/Debug/net6.0/BookshelfSampleTest.dll (.NETCoreApp,Version=v6.0) Microsoft (R) Test Execution Command Line Tool Version 17.0.0 Copyright (c) Microsoft Corporation. All rights reserved. Starting test execution, please wait... A total of 1 test files matched the specified pattern. Results File: /tmp/test-explorer-kxye5v/0.trx Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: < 1 ms - /home/example/Documents/workspace/BookshelfSample/BookshelfSampleTest/bin/Debug/net6.0/BookshelfSampleTest.dll (net6.0)
Because I use NLog in the ASP.NET Core project, I tried to use NLog in the xUnit project.
But I couldn't resolve the problem.
And I also tried "xunit.NLog".
However, it didn't seem to be compatible with .NET 6.
So when I want to log some data to write test codes, I run tests by command.
After finishing creating them, I run them by ".NET Core Test Explorer".
For Entity Framework Core projects
To log Entity Framework Core projects, I can add "ITestOutputHelper" into DbContext options.
SharedDatabaseFixture.cs
using BookshelfSample.Models; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Xunit.Abstractions; namespace BookshelfSampleTest.Models; public class SharedDatabaseFixture: IDisposable { private readonly ITestOutputHelper output; private readonly SqliteConnection connection; public SharedDatabaseFixture(ITestOutputHelper output) { this.output = output; this.connection = new SqliteConnection("DataSource=:memory:"); this.connection.Open(); } public void Dispose() => this.connection.Dispose(); public BookshelfContext CreateContext() { var result = new BookshelfContext(new DbContextOptionsBuilder<BookshelfContext>() .UseSqlite(this.connection) .EnableSensitiveDataLogging() .LogTo(this.output.WriteLine) .Options); result.Database.EnsureDeleted(); result.Database.EnsureCreated(); return result; } }
Top comments (1)
Great article on integrating EF Core logging into xUnit tests! For developers working on macOS who need to run .NET Framework 4 applications without setting up a virtual machine, ServBay offers a seamless solution. It's been a game-changer for my development workflow.