NUnit TestFixture 仅在 BaseTest 类 dotnet cli 上运行

问题描述 投票:0回答:1

这是我的测试课程:

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();
    }
}

创建PackageTests.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"));
    }
}

这是

dotnet test
结果:

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.

我正在使用骑手运行测试,骑手运行所有测试。但是当我从 dotnet cli 运行时,dotnet cli 找不到我的

CreatePackageTests
我想是因为我还没有放置
TestFixture' attribute to 
CreatePackageTests`。

问题:有没有办法使用 dotnet cli 运行all测试?

c# unit-testing .net-core nunit rider
1个回答
0
投票

您能否确保在 Rider 和终端中使用相同的 .NET Cli 和 MBBuild 版本?

© www.soinside.com 2019 - 2024. All rights reserved.