Search code examples
c#unit-testing.net-corenunitrider

NUnit TestFixture only on BaseTest class dotnet cli run


There are my test classes:

BaseTest.cs:

using AutoMapper;
using Microsoft.Extensions.Configuration;
using Moq;
using NUnit.Framework;

[TestFixture]
public abstract class BaseTest
{
    protected Mock<IPatientRepository> PatientRepositoryMock = new();

    protected IConfiguration Configuration = null!;
    protected PasswordService PasswordService = null!;
    protected Mock<ICurrentUserService> CurrentUserMock = new();
    protected Mock<IUserRepository> UserRepositoryMock = new();
    protected Mock<IMapper> MapperMock = new();
    protected Mock<IPackageRepository> PackageRepositoryMock = new();



    [SetUp]
    public void Setup()
    {
        CurrentUserMock = new Mock<ICurrentUserService>();
        PackageRepositoryMock = new Mock<IPackageRepository>();
        MapperMock = new Mock<IMapper>();
    }

    [OneTimeSetUp]
    public void OneTimeSetup()
    {
        PasswordService = new PasswordService();
        Configuration = ConfigurationsBuilder.Build();
    }
}

CreatePackageTests.cs

using Moq;
using NUnit.Framework;

public class CreatePackageTests : PackageTestsBase
{
    [Test]
    public void CreatePackage_DuplicateName_BadRequestException()
    {
        // Arrange
        var createPackageDto = new CreatePackageRequest
        {
            Name = "Basic Package"
        };

        PackageRepositoryMock.Setup(r => r.SaveChangesWithNameValidationAsync()).Throws<BadRequestException>(() =>
            throw new BadRequestException("Package with the same name already exists"));
        // Act & Assert
        var exception = Assert.ThrowsAsync<BadRequestException>(() => PackageService.CreateAsync(createPackageDto));
        Assert.That(exception?.Message, Is.EqualTo("Package with the same name already exists"));
    }
}

Here is the dotnet test result:

No test is available in {mydirectory}Application.UnitTests.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.

I am using the same dotnet in Rider and in terminal:

rider: rider: dotnet path

terminal: terminal: dotnet path

I am running test using Rider and rider running all test. But when I'm running from dotnet cli, dotnet cli does not found my CreatePackageTests I think because I haven't put the TestFixture attribute to CreatePackageTests.

Question: Is there any way to run all tests using dotnet cli?


Solution

  • The problem was with the NUnit3TestAdapter. I have updated the package. It works!