Voyager.UnitTestLogger 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Voyager.UnitTestLogger --version 1.0.0                
NuGet\Install-Package Voyager.UnitTestLogger -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Voyager.UnitTestLogger" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Voyager.UnitTestLogger --version 1.0.0                
#r "nuget: Voyager.UnitTestLogger, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Voyager.UnitTestLogger as a Cake Addin
#addin nuget:?package=Voyager.UnitTestLogger&version=1.0.0

// Install Voyager.UnitTestLogger as a Cake Tool
#tool nuget:?package=Voyager.UnitTestLogger&version=1.0.0                

Voyager.UnitTestLogger

Simple implementation of ILogger interface. Is fast to use in the unit tests because there is no need to configure DI and builders. It is possible to use it as an output to console or write the result for testing-spy purposes.

How to use the logger

The class logging into the console has the name: Voyager.UnitTestLogger.ConsoleLogger. This is an example of use in a unit test:

  internal class SimpleExample
  {
    private MyDataReader dataReader;

    [SetUp]
    public void Setup()
    {
      dataReader = new MyDataReader(new Voyager.UnitTestLogger.ConsoleLogger<MyDataReader>());
    }

    [Test]
    public void Example()
    {
      dataReader.Read();
      Assert.Pass();
    }
  }

How to use the test-spy functionality

The class Voyager.UnitTestLogger.SpyLog either writes logs to the console and also saves the history of log operation so it could be used for asserting purposes. By a text search condition, it is possible to find out about methods that are processed during the calling of the test.

This is an example how to check the content of the history of logs:

  public class ScopeTest
  {
    Microsoft.Extensions.Logging.ILogger logger;

    [SetUp]
    public void Setup()
    {
      logger = new Voyager.UnitTestLogger.SpyLog<ScopeTest>();
    }

    [Test]
    public void Test()
    {
      logger.LogInformation("B1");
      using (var scope = logger.BeginScope("S1"))
      {
        logger.LogInformation("L1");
        logger.LogInformation("L2");
        using (var scope2 = logger.BeginScope("S2"))
        {
          logger.LogInformation("m1");    
          logger.LogInformation("m2");
        }
        logger.LogInformation("L3");
      }
      logger.LogInformation("B2");
			
      Voyager.UnitTestLogger.SpyLog<ScopeTest> casted = (logger as Voyager.UnitTestLogger.SpyLog<ScopeTest>)!;
      Assert.That(casted.GetSpyContent(), Is.EqualTo(
"B1" + Environment.NewLine +
"S1" + Environment.NewLine +
"  L1" + Environment.NewLine +
"  L2" + Environment.NewLine +
"  S2" + Environment.NewLine +
"    m1" + Environment.NewLine +
"    m2" + Environment.NewLine +
"  L3" + Environment.NewLine +
"B2"
));

There is a method that counts the number of lines in the spy content:

  internal class SpyTest
  {
    private Voyager.UnitTestLogger.SpyLog<ScopeTest> logger;
    private MyDataReader dataReader;

    [SetUp]
    public void Setup()
    {
      logger = new Voyager.UnitTestLogger.SpyLog<ScopeTest>();
      dataReader = GetReader(logger);
    }

    [Test]
    public void CheckReadCount()
    {
      dataReader.Read();
      dataReader.Read();
      Assert.That(logger.GetLinesCount(), Is.EqualTo(GetExpectedValue()));
    }
...

✍️ Authors

  • @andrzejswistowski - Idea & work. Please let me know if you find out an error or suggestions.

contributors.

🎉 Acknowledgements

  • Przemysław Wróbel - for the icon.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 418 4/21/2023
1.0.2 171 4/17/2023
1.0.1 171 4/17/2023
1.0.0 225 3/17/2023