将外部数据源与 NUnit 的 TestCaseAttribute 一起使用

发布于 2024-09-04 09:32:18 字数 513 浏览 3 评论 0原文

是否可以从外部数据源(例如 Excel 电子表格、CSV 文件或数据库)获取 TestCaseAttribute 的值?即,每个测试用例有一个包含 1 行数据的 .csv 文件,并将该数据一次传递给 NUnit。

这是我想使用它的具体情况。我目前正在将一个系统的一些功能合并到另一个系统中。这几乎只是从旧系统复制并粘贴到新系统的过程。不幸的是,被移动的代码不仅没有任何测试,而且不是以可测试的方式编写的(即与数据库和其他代码紧密耦合)。花时间使代码可测试实际上是不可能的,因为它是一团糟,我的日程安排很紧,整个功能计划在接下来的 6-9 个月内从头开始重写。但是,由于我不喜欢不对代码进行任何测试的想法,因此我将使用 WebDriver 创建一些简单的 Selenium 测试,以通过 UI 测试页面。虽然这并不理想,但总比没有好。

有问题的页面有大约 10 个输入值和大约 20 个我需要在计算完成后断言的值,以及大约 30 个我想要测试的有效值组合。我已经将数据保存在电子表格中,因此如果能够直接将其取出,而不必在 Visual Studio 中重新输入所有内容,那就太好了。

Is it possible to get the values for a TestCaseAttribute from an external data source such as an Excel Spreadsheet, CSV file or Database? i.e. Have a .csv file with 1 row of data per test case and pass that data to NUnit one at a time.

Here's the specific situation that I'd like to use this for. I'm currently merging some features from one system into another. This is pretty much just a copy and paste process from the old system into the new one. Unfortunately, the code being moved not only does not have any tests, but is not written in a testable manner (i.e. tightly coupled with the database and other code.) Taking the time to make the code testable isn't really possible since its a big mess, i'm on a tight schedule and the entire feature is scheduled to be re-written from the ground up in the next 6-9 months. However, since I don't like the idea of not having any tests around the code, I'm going to create some simple Selenium tests using WebDriver to test the page through the UI. While this is not ideal, it's better than nothing.

The page in question has about 10 input values and about 20 values that I need to assert against after the calculations are completed, with about 30 valid combinations of values that I'd like to test. I already have the data in a spreadsheet so it'd be nice to simply be able to pull that out rather than having to re-type it all in Visual Studio.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-09-11 09:32:18

我终于能够使用 NUnit 的 TestCaseSource 属性。代码看起来有点难看,但它可以工作。

以下是从 .csv 文件中提取数据并将其传递给测试方法的示例。该测试针对一个简单计算器的 Add 方法,该方法接受两个整数,将它们相加并返回总和。

用于从文件加载测试数据的类。

public class TestData
{
    public int number1 { get; set; }
    public int number2 { get; set; }
    public int sum { get; set; }

    public static IEnumerable TestCases
    {
        get
        {
            string inputLine;
            using(FileStream inputStream = 
                new FileStream("C:\\Code\\TestData\\TestData.csv", 
                    FileMode.Open, 
                    FileAccess.Read))
            {
                StreamReader streamReader = new StreamReader(inputStream);

                while((inputLine = streamReader.ReadLine()) != null)
                {
                    var data = inputLine.Split(',');
                    yield return new TestData { 
                        number1 = Convert.ToInt32(data[0])
                        ,number2 = Convert.ToInt32(data[1])
                        ,sum = Convert.ToInt32(data[2]) 
                    };
                }

                streamReader.Close();
                inputStream.Close();
            }
        }
    }
}

包含实际测试的类:

[TestFixture]
public class CalculatorTests
{
    [Test]
    [TestCaseSource(typeof(TestData), "TestCases")]
    public void AddTwoNumbers(TestData data)
    {
        int sum = Calculator.Add(data.number1, data.number2);
        sum.ShouldEqual(data.sum);
    }
}

TestData.csv 的内容

4,4,8
15,20,35
8,8,16
5,5,10
42,13,55

修改 TestData 类中的 get 属性以从您想要的任何数据源(即数据库、Web 服务、Excel...)中提取数据应该相当简单。

I was finally able to accomplish what I wanted using NUnit's TestCaseSource attribute. The code looks a little ugly but it works.

Here is an example of pullind the data from a .csv file and passing it to the test method. The test is for the Add method of a simple calculator that takes two ints, adds them together and returns the sum.

Class to load the test data from the file.

public class TestData
{
    public int number1 { get; set; }
    public int number2 { get; set; }
    public int sum { get; set; }

    public static IEnumerable TestCases
    {
        get
        {
            string inputLine;
            using(FileStream inputStream = 
                new FileStream("C:\\Code\\TestData\\TestData.csv", 
                    FileMode.Open, 
                    FileAccess.Read))
            {
                StreamReader streamReader = new StreamReader(inputStream);

                while((inputLine = streamReader.ReadLine()) != null)
                {
                    var data = inputLine.Split(',');
                    yield return new TestData { 
                        number1 = Convert.ToInt32(data[0])
                        ,number2 = Convert.ToInt32(data[1])
                        ,sum = Convert.ToInt32(data[2]) 
                    };
                }

                streamReader.Close();
                inputStream.Close();
            }
        }
    }
}

Class with the actual tests:

[TestFixture]
public class CalculatorTests
{
    [Test]
    [TestCaseSource(typeof(TestData), "TestCases")]
    public void AddTwoNumbers(TestData data)
    {
        int sum = Calculator.Add(data.number1, data.number2);
        sum.ShouldEqual(data.sum);
    }
}

Contents of TestData.csv

4,4,8
15,20,35
8,8,16
5,5,10
42,13,55

It should be fairly simple to modify the get property in the TestData class to pull data from any datasource you want (i.e. Database, Web Service, Excel...)

懵少女 2024-09-11 09:32:18

您始终可以在 Excel 或任何电子表格工具中打开 csv 文件,然后添加一个新列,将输入/输出值连接到测试用例语法中。

类似于: =CONCATENATE("[TestCase(", A1, ",", B1, ",", C1, ",", D1, ",", E1, ")]")

然后将列复制/粘贴到代码。

You could always open your csv file in excel or any spreadsheet tool and then add a new column that concatenates the input/output values into the test case syntax.

Something like: =CONCATENATE("[TestCase(", A1, ",", B1, ",", C1, ",", D1, ",", E1, ")]")

Then copy/paste the column into the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文