将外部数据源与 NUnit 的 TestCaseAttribute 一起使用
是否可以从外部数据源(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于能够使用 NUnit 的 TestCaseSource 属性。代码看起来有点难看,但它可以工作。
以下是从 .csv 文件中提取数据并将其传递给测试方法的示例。该测试针对一个简单计算器的 Add 方法,该方法接受两个整数,将它们相加并返回总和。
用于从文件加载测试数据的类。
包含实际测试的类:
TestData.csv 的内容
修改 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.
Class with the actual tests:
Contents of TestData.csv
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...)
您始终可以在 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.