【问题标题】:How to run XUnit test using data from a CSV file如何使用 CSV 文件中的数据运行 XUnit 测试
【发布时间】:2017-03-10 20:43:49
【问题描述】:

有没有办法使用CSV 文件作为数据源来运行数据驱动的XUnit 测试?我试过Cavity.Data.XUnit,但它不再与最新版本的XUnit 兼容。到目前为止,我只能使用 Excel 文件来实现这一点,但我需要将它们更改为 CSV

非常感谢任何帮助。

一个例子:

[Theory]
[ExcelData(@"Settings\TestFileParam.xls", "Select url, username, password, from TestData")]
//^Replace with a CSV file instead
public void Tester_Method(string url, string username, string password)
{
    //Code reading the data from CSV
    Assert.True(something);
}

【问题讨论】:

  • 我只是好奇。您要测试什么以及文件中的这些数据是什么?
  • -What you trying to test:我正在为一个网站运行 UI 测试。我在我用作框架的单独解决方案中使用 Selenium。 - what are these data from file 数据来自 2003-07 格式的 excel 文件

标签: c# csv selenium qa xunit


【解决方案1】:

您需要创建一个自定义 xUnit.Sdk.DataAttribute。这是一个将以这种形式读取数据的示例。

MyCsv.csv

sep=,
csvRowOne,csvRowTwo,csvRowThree
15,"Just A Test","Apples are Red"

那么你可以这样称呼它:

  [Theory]
  [CsvData(@"C:\MyCsv.csv")]
  public void TestWithCSVData(int csvRowOne, string csvRowTwo, string csvRowThree)

它只是解析字符串和整数,但您可以扩展 ConvertParameter 方法来做更多事情。

CsvDataAttribute.cs

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class CsvDataAttribute : DataAttribute
{
    private readonly string _fileName;
    public CsvDataAttribute(string fileName)
    {
        _fileName = fileName;
    }

    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        var pars = testMethod.GetParameters();
        var parameterTypes = pars.Select(par => par.ParameterType).ToArray();
        using (var csvFile = new StreamReader(_fileName))
        {
            csvFile.ReadLine();// Delimiter Row: "sep=,". Comment out if not used
            csvFile.ReadLine(); // Headings Row. Comment out if not used
            string line;
            while ((line = csvFile.ReadLine()) != null)
            {
                var row = line.Split(',');
                yield return ConvertParameters((object[])row, parameterTypes);
            }
        }
    }

    private static object[] ConvertParameters(IReadOnlyList<object> values, IReadOnlyList<Type> parameterTypes)
    {
        var result = new object[parameterTypes.Count];
        for (var idx = 0; idx < parameterTypes.Count; idx++)
        {
            result[idx] = ConvertParameter(values[idx], parameterTypes[idx]);
        }

        return result;
    }

    private static object ConvertParameter(object parameter, Type parameterType)
    {
        return parameterType == typeof(int) ? Convert.ToInt32(parameter) : parameter;
    }
}

【讨论】:

    【解决方案2】:

    xUnit 没有内置这种确切的功能,但是它确实具有针对用例类型的扩展点。大部分 xUnit 都可以替换和定制,新的测试用例数据源是最常见的数据源之一。

    Xunit.Sdk.DataAttributeExcelDataAttribute 使用的扩展点。

    典型的实现如下所示

    public class CsvDataAttribute : DataAttribute
    {
        readonly string fileName;
    
        public CsvDataAttribute(string fileName)
        {
            this.fileName = fileName;
        }
    
        public override IEnumerable<object[]> GetData(MethodInfo testMethod)
        {
            //Parse CSV and return an object[] for each test case
        }
    }
    

    ExcelDataAttribute 的实现可以在这里找到。 https://github.com/xunit/samples.xunit/blob/885edfc2e84ec4934cd137a985c3b06dda043ab5/ExcelDataExample/ExcelDataAttribute.cs

    【讨论】:

    • ExcelDataAttribute 实现不再存在于 github 存储库中。提供的链接已不存在。
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多