在 NUnit 中动态创建测试
使用 Nunit,我希望能够编写一个测试装置,该装置将读取特定目录中的所有文件名并为每个文件创建一个测试。
我可以很容易地编写一种测试方法来扫描目录并执行所有测试,但是当我运行 NUnit 时,我希望能够单独查看每个测试。
这可能吗?
Using Nunit, I want to be able to write a test fixture that will read all the filenames in a particular directory and create a test for each file.
I could quite easily write one test method that scans through the directory and just does all the tests, but when I run NUnit I want to be able to see each of the tests individually.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了一种适合我的目的的方法
有一个测试用例,并用 TestCaseSource 属性标记它,如下所示
然后编写 GetTestCases 来读取目录中的所有文件名
然后当我启动 NUnit 时,我会得到一个测试列表运行(在 TestFile 下列出)。
I've found a way that fits my purposes
Have one test case, and mark it with the TestCaseSource attribute like so
Then write the GetTestCases to read all the file names in the directory
Then when I start NUnit I get a list of the tests to be run (listed under TestFile).
尝试数据驱动的测试用例 NUnit 扩展。
Try the data driven test cases NUnit extension.
如果您不随着时间的推移将文件添加到该目录,并且有一组文件名作为通用测试的输入传递,请尝试使用 RowTest NUnit 扩展(std distrib 帖子 v2.4.7 的一部分) - 您可以查看每个测试用例 - 在 GUI 中单独输入组合并分组到单个节点下。
如果您要将文件添加到该目录,我会编写一个 NUnit TestCase,它循环运行时获取的文件列表,并使用每个文件路径调用通用测试方法。 使用 收集参数 收集失败的测试文件名 - 最后断言。 您将无法单独查看每个测试用例,但您将拥有可读的简单测试代码。
If you are not going to be adding files to that directory over time and have a set of filenames to pass in as an input to a generic test, try using the RowTest NUnit extension (part of the std distrib post v2.4.7) - You'd be able to see each test case - input combination individually in the GUI grouped under a single node.
If you are going to be adding files to that directory, I'd write a single NUnit TestCase that loops over a list of files obtained at runtime and calls the generic test method with each filepath. Use a collecting parameter to collect failing test file names - at the end you assert. You wouldn't be able to see each test case individually but you'd have readable simple test code.
我知道这有点晦涩,但我过去使用过一个脚本来为我生成代码,然后我可以将其作为单独的测试用例执行。
I know this is a little obscure but I have used a script in the past to generate code for me which I can then execute as individual test cases.