Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# 什么是TestContext.DataRow[“MyColumnName”的替代品_C#_Asp.net Core_Datasource_Mstest - Fatal编程技术网

C# 什么是TestContext.DataRow[“MyColumnName”的替代品

C# 什么是TestContext.DataRow[“MyColumnName”的替代品,c#,asp.net-core,datasource,mstest,C#,Asp.net Core,Datasource,Mstest,在.Net核心单元测试项目中使用MSTest。我试图使用csv数据源为测试方法提供数据 以前,我会在.Net Framework测试项目中使用如下内容: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod] p

在.Net核心单元测试项目中使用MSTest。我试图使用csv数据源为测试方法提供数据

以前,我会在.Net Framework测试项目中使用如下内容:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"data.csv", "data#csv", DataAccessMethod.Sequential),
     DeploymentItem("data.csv"),
     TestMethod]
    public void ValuesController_Post()
    {
        _controller.Post(TestContext.DataRow["body"]);
        _valuesRepository.Verify(_ => _.Post(It.IsAny<string>()), Times.Once);
    }
[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”@“data.CSV”、“data#CSV”、DataAccessMethod.Sequential),
DeploymentItem(“data.csv”),
测试方法]
public void values控制人职位()
{
_controller.Post(TestContext.DataRow[“body”]);
_valuesRepository.Verify(=>u.Post(It.IsAny()),Times.Once);
}
这里的键是在TestContext中找到的DataRow属性。这在TestContext的.Net核心版本中似乎不存在


如何在.Net Core中执行此操作?

自从迁移到aspnet Core后,我从未能够使用相同的[Datasource(…)]属性来迭代测试数据,我的数据驱动测试总是被跳过

您是否考虑过使用[DataTestMethod]和[DynamicATA]以及读取您的文件的自定义源切换到另一种方法

这里有一篇关于这方面的好文章:

也许另一种方法是在测试开始时读取整个文件,然后作为单个单元测试在数据集中迭代


希望这能有所帮助。

我花了一个下午的时间来摆弄东西,但我终于找到了解决办法。由于您没有指定您的测试或CSV文件,下面是一个我可以使用的快速示例

长话短说,我安装了,因为解析CSV非常容易,但事实并非如此。正如Carl Verret指出的,您需要使用测试方法上方的
[dynamicata(…)]
属性,然后使用CsvHelper解析CSV

CSV文件(例如.CSV)

重要提示:确保此CSV文件包含在测试项目中,并且在解决方案资源管理器中的CSV文件属性中将“复制到输出目录”设置为“始终”

CsvHelper使用的数据传输对象

public class AdditionData
{
    public int A { get; set; }
    public int B { get; set; }
    public bool IsLessThanZero { get; set; }
}
测试类

[TestClass]
public class ExampleTests
{
    // HINT: Look in {Your Test Project Folder}\bin\{Configuration}\netcore3.1\FolderYourCsvFileIsIn for the CSV file.
    //       Change this path to work with your test project folder structure.
    private static readonly string DataFilePath = Path.GetDirectoryName(typeof(ExampleTests).Assembly.Location) + @"\FolderYourCsvFileIsIn\Example.csv";

    [TestMethod]
    [DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
    public void AddingTwoNumbers(AdditionData data)
    {
        bool isLessThanZero = data.A + data.B < 0;

        Assert.AreEqual(data.IsLessThanZero, isLessThanZero);
    }

    private static IEnumerable<object[]> GetData()
    {
        using var stream = new StreamReader(DataFilePath);
        using var reader = new CsvReader(stream, new CsvConfiguration(CultureInfo.CurrentCulture));
        var rows = reader.GetRecords<AdditionData>();

        foreach (var row in rows)
        {
            yield return new object[] { row };
        }
    }
}
[TestClass]
公共类示例测试
{
//提示:在{Your Test Project Folder}\bin\{Configuration}\netcore3.1\FolderYourCsvFileIsIn中查找CSV文件。
//更改此路径以使用测试项目文件夹结构。
私有静态只读字符串DataFilePath=Path.GetDirectoryName(typeof(ExampleTests).Assembly.Location)+@“\FolderYourCsvFileIsIn\Example.csv”;
[测试方法]
[dynamicata(nameof(GetData),dynamicatasourcetype.Method)]
公共无效添加两个数字(添加数据)
{
bool isLessThanZero=data.A+data.B<0;
Assert.AreEqual(data.IsLessThanZero,IsLessThanZero);
}
私有静态IEnumerable GetData()
{
使用var stream=newstreamreader(DataFilePath);
使用var reader=new CsvReader(流,新CsvConfiguration(CultureInfo.CurrentCulture));

var rows=reader.GetRecords

我也有同样的问题。我问了一个关于有没有人找到解决方案的问题?@magol-github的答案对我不起作用。不幸的是,我没有时间找到解决方案
[TestClass]
public class ExampleTests
{
    // HINT: Look in {Your Test Project Folder}\bin\{Configuration}\netcore3.1\FolderYourCsvFileIsIn for the CSV file.
    //       Change this path to work with your test project folder structure.
    private static readonly string DataFilePath = Path.GetDirectoryName(typeof(ExampleTests).Assembly.Location) + @"\FolderYourCsvFileIsIn\Example.csv";

    [TestMethod]
    [DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
    public void AddingTwoNumbers(AdditionData data)
    {
        bool isLessThanZero = data.A + data.B < 0;

        Assert.AreEqual(data.IsLessThanZero, isLessThanZero);
    }

    private static IEnumerable<object[]> GetData()
    {
        using var stream = new StreamReader(DataFilePath);
        using var reader = new CsvReader(stream, new CsvConfiguration(CultureInfo.CurrentCulture));
        var rows = reader.GetRecords<AdditionData>();

        foreach (var row in rows)
        {
            yield return new object[] { row };
        }
    }
}