Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xUnit-显示理论成员数据的测试名称(TestCase)_C#_Asp.net_.net_Asp.net Core_Xunit - Fatal编程技术网

C# xUnit-显示理论成员数据的测试名称(TestCase)

C# xUnit-显示理论成员数据的测试名称(TestCase),c#,asp.net,.net,asp.net-core,xunit,C#,Asp.net,.net,Asp.net Core,Xunit,我一直在使用NUnit进行测试,我非常喜欢测试用例。在NUnit中,您可以使用TestCaseData类中的SetName函数轻松设置测试用例中的每个测试名称 xUnit对此有类似的功能吗 目前,即使在测试用例中有6个测试,我也只能在测试资源管理器中看到一个测试 xUnit测试 public class LogHandler : TestBase { private ILogger _logger; public LogHandler() { //Arr

我一直在使用NUnit进行测试,我非常喜欢测试用例。在NUnit中,您可以使用TestCaseData类中的SetName函数轻松设置测试用例中的每个测试名称

xUnit对此有类似的功能吗

目前,即使在测试用例中有6个测试,我也只能在测试资源管理器中看到一个测试

xUnit测试

public class LogHandler : TestBase
{
    private ILogger _logger;

    public LogHandler()
    {
        //Arrange
        LogAppSettings logAppSettings = GetAppSettings<LogAppSettings>("Log");

        IOptions<LogAppSettings> options = Options.Create(logAppSettings);

        LogService logService = new LogService(new Mock<IIdentityService>().Object, options);

        LogProvider logProvider = new LogProvider(logService);

        _logger = logProvider.CreateLogger(null);
    }

    public static IEnumerable<object[]> TestCases => new[]
    {
        new object[] { LogLevel.Critical,
            new EventId(),
            new Exception(),
            1 },
        new object[] { LogLevel.Error,
            new EventId(),
            new Exception(),
            1 },
        new object[] { LogLevel.Warning,
            new EventId(),
            new Exception(),
            0 },
        new object[] { LogLevel.Information,
            new EventId(),
            new Exception(),
            0 },
        new object[] { LogLevel.Debug,
            new EventId(),
            new Exception(),
            0 },
        new object[] { LogLevel.Trace,
            new EventId(),
            new Exception(),
            0 },
        new object[] { LogLevel.None,
            new EventId(),
            new Exception(),
            0 }
    };

    [Theory, MemberData(nameof(TestCases))]
    public void Test(LogLevel logLevel, EventId eventId, Exception exception, int count)
    {
        //Act
        _logger.Log<object>(logLevel, eventId, null, exception, null);

        //Assert
        int exceptionCount = Database.Exception.Count();

        Assert.Equal(exceptionCount, count);
    }
}
NUnit测试窗口

这就是我在xUnit想要的

如何在xUnit中为测试用例中的每个测试设置名称?

目前,即使在测试用例中有6个测试,我也只能在测试资源管理器中看到一个测试

这是因为xUnit.net认为测试数据不可序列化。见本期:

主要细节如下:

简短的回答 如果你的一些理论数据不能被xUnit.net“序列化”,那么 它不能封装到测试用例的序列化中 我们需要为VisualStudio测试运行程序执行此操作

长话短说 在VisualStudio测试运行程序中,在一个 进程,并在另一个进程中执行。因此,测试用例必须能够 要转换为非限定字符串表示(aka, “序列化”),以便运行。我们还可以在测试时序列化 方法级别,因为这只需要知道类型和方法 名称(两个字符串)。当您开始将数据放入混合时,我们需要 确保我们知道如何序列化该数据;如果我们不能序列化 一个理论的所有数据,然后我们只能回到单一方法(我们知道我们可以序列化)

完整答案包含更多细节

在NUnit中,您可以使用TestCaseData类中的SetName函数轻松设置测试用例中的每个测试名称

xUnit对此有类似的功能吗


目前还没有。

事实上,有一个可行的解决方案需要一些管道代码,这些代码应该可以在未更改的测试中使用。它需要实现自定义TheoryAttribute、自定义TheoryDiscoveryr和自定义TestCase类。整个解决方案在本回购协议的MIT许可下可用

实现所需的文件包括: ,

用法很简单:将上述文件直接编译到测试程序集中或作为单独的程序集,并在代码中使用它们,如下所示:

[djvu理论]
[ClassData(typeof(DjvuJsonDataSource))]
公共无效DirmChunk_理论(DjvuJsonDocument文档,int索引)
{
int pageCount=0;
使用(DjvuDocument document=DjvuNet.Tests.Util.GetTestDocument(index,out pageCount))
{
DjvuNet.Tests.Util.VerifyDjvuDocument(pageCount,document);
DjvuNet.Tests.Util.VerifyDjvuDocumentCtor(页面计数,文档);
//DirmChunk仅出现在多页文档中
//其中根形式为DjvmChunk类型
if(document.RootForm.ChunkType==ChunkType.Djvm)
{
DirmChunk dirm=((DjvmChunk)document.RootForm).dirm;
Assert.NotNull(dirm);
Assert.True(dirm.IsBundled?doc.Data.dirm.DocumentType==“bundled”:doc.Data.dirm.DocumentType==“间接”);
var组件=直接组件;
Assert.Equal(components.Count、doc.Data.Dirm.FileCount);
}
}
}
其中一个理论函数参数在xUnit中不可序列化,但尽管理论测试将单独显示并编号。若理论函数的第一个参数是字符串类型,那个么除了作为函数调用的参数外,它还将用作测试的名称

这个想法归功于其他开发人员——我必须找到他的代码链接——但它是为DjvuNet项目从头开始重新实现的。

这对我来说很有效:

public class TestScenario
{
...
    public override string ToString()
    {
        return $"Role: {Role}...";
    }
}

我以前也有同样的问题。类将需要从IXunitSerializable继承。实施起来相当麻烦。。不确定是否有更好的方法。有一个现成的实现,它绕过了更改测试代码和测试代码的需要。只需包括新的自定义xUnit TheoryAttribute、XUnitDiscoveryer类和两个测试类,详细信息请参见第二个答案和麻省理工学院许可的工作代码。。就目前情况而言,我将继续讨论NUnit。希望他们能解决这个问题。@Reft Common这不是一个耻辱,而是一个很小的限制,可以很容易地用一小段代码来弥补-我的答案见下文。不管怎么说,xUnit是一个测试框架,可供许多在网络世界最重要的回购协议使用。@Jacekblasszczynski您能谈谈这些原因吗?
    [DjvuTheory]
    [ClassData(typeof(DjvuJsonDataSource))]
    public void DirmChunk_Theory(DjvuJsonDocument doc, int index)
    {
        int pageCount = 0;
        using (DjvuDocument document = DjvuNet.Tests.Util.GetTestDocument(index, out pageCount))
        {
            DjvuNet.Tests.Util.VerifyDjvuDocument(pageCount, document);
            DjvuNet.Tests.Util.VerifyDjvuDocumentCtor(pageCount, document);

            // DirmChunk is present only in multi page documents
            // in which root form is of DjvmChunk type
            if (document.RootForm.ChunkType == ChunkType.Djvm)
            {
                DirmChunk dirm = ((DjvmChunk)document.RootForm).Dirm;

                Assert.NotNull(dirm);

                Assert.True(dirm.IsBundled ? doc.Data.Dirm.DocumentType == "bundled" : doc.Data.Dirm.DocumentType == "indirect");

                var components = dirm.Components;
                Assert.Equal<int>(components.Count, doc.Data.Dirm.FileCount);
            }
        }
    }
public class TestScenario
{
...
    public override string ToString()
    {
        return $"Role: {Role}...";
    }
}
    [Theory]
    [ClassData(typeof(MyProvider))]
    public void TestScenarios(TestScenario scenaro)
    {
        TestScenarioInternal(scenaro);

    }