自定义 NUnit GUI 以进行数据驱动测试

发布于 2024-10-16 16:47:16 字数 1474 浏览 0 评论 0原文

我的测试项目由一组输入数据文件组成,这些文件被输入到一个旧的第三方软件中。由于该软件的输入数据文件很难构建(不是故意可以完成的),因此我不会添加新的输入数据文件。

每个输入数据文件都将受到一组“测试函数”的影响。一些测试功能可以独立调用。其他测试函数代表顺序操作的阶段 - 如果较早的阶段失败,则不需要执行后续阶段。

我已经尝试了 NUnit 参数化测试用例(TestCaseAttribute 和 TestCaseSourceAttribute),将数据文件列表作为测试用例传递。我总体上对选择输入数据进行测试的能力感到满意。

但是,我想看看是否可以自定义其 GUI 的树结构,以便“测试函数”成为“输入数据”的子级。例如:

  • 文件#1
    • 检查文件类型测试
    • 获取FileTopLevelStructureTest
    • 完整流程测试
      • StageOne测试
      • 第二阶段测试
      • 第三阶段测试
  • 文件 #2
    • 检查文件类型测试
    • 获取FileTopLevelStructureTest
    • 完整流程测试
      • StageOne测试
      • 第二阶段测试
      • 第三阶段测试

这对于识别在处理特定输入文件期间失败的阶段非常有用。

是否有任何提示和技巧可以启用新的树布局?我是否需要自定义 NUnit 才能获得此布局?

已编辑:请参阅问题将测试逻辑与测试结果数据分离的单元测试术语,介绍分离测试数据和测试代码的概念。就我而言,分离是出于实际考虑,而不是意识形态原因。我的测试数据文件的来源是“野外”。

数据驱动测试的其他轶事使用:

My test project consists of a set of input data files which is fed into a piece of legacy third-party software. Since the input data files for this software are difficult to construct (not something that can be done intentionally), I am not going to add new input data files.

Each input data file will be subject to a set of "test functions". Some of the test functions can be invoked independently. Other test functions represent the stages of a sequential operation - if an earlier stage fails, the subsequent stages do not need to be executed.

I have experimented with the NUnit parametrized test case (TestCaseAttribute and TestCaseSourceAttribute), passing in the list of data files as test cases. I am generally satisfied with the the ability to select the input data for testing.

However, I would like to see if it is possible to customize its GUI's tree structure, so that the "test functions" become the children of the "input data". For example:

  • File #1
    • CheckFileTypeTest
    • GetFileTopLevelStructureTest
    • CompleteProcessTest
      • StageOneTest
      • StageTwoTest
      • StageThreeTest
  • File #2
    • CheckFileTypeTest
    • GetFileTopLevelStructureTest
    • CompleteProcessTest
      • StageOneTest
      • StageTwoTest
      • StageThreeTest

This will be useful for identifying the stage that failed during the processing of a particular input file.

Is there any tips and tricks that will enable the new tree layout? Do I need to customize NUnit to get this layout?

Edited: See the question Term for unit testing that separates test logic from test result data for an introduction to the concept of separating test data and test code. In my situation, the separation is driven by practical concerns, not by ideological reasons. The sources of my test data files are "in the wild".

Other anecdotal use of data-driven tests:

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

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

发布评论

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

评论(1

情绪少女 2024-10-23 16:47:16

您可能无法完全像这样对结构进行建模。测试只能存在于 TestFixtures(类)中,唯一允许您在 GUI 中嵌套任意级别的就是命名空间。

您可以做的是:

* File001 (namespace)
  * Tests_File001 (class, text fixture)
    * CheckFileTypeTest (method, test)
    * GetFileTopLevelStructureTest (method, test)
  * CompleteProcessTest (namespace)
    * TestsCompleteProcessTest (class, text fixture)
      * StageOneTest (method, test)
      * StageTwoTest (method, test)
      * StageThreeTest (method, test)

我假设您当前有一个设置,其中

[Test]
[TestCaseSource("TestFiles")]
public void StageOneTest(String file)
{
}

一个测试方法检查特定的事物,然后为每个文件运行该测试。在这种情况下,您只需删除属性并从新测试中调用此方法即可。

我还感觉到您希望测试按特定顺序执行,并且不同的测试运行程序以不同的顺序执行测试。我不知道有什么方法可以做到这一点,一般来说你不应该这样做。任何测试都不应该依赖于另一个测试的成功。

假设您的测试有点耗时,并且如果“较早”测试已经失败,您不想运行所有“较晚”测试,您可以考虑将它们放入适当的类别,例如:“初步”、“第一阶段”等。然后您可以依次执行一个类别并查看第一个类别失败的位置。

我希望这对你有帮助。

You are probably not going to be able to model the structure exactly like this. Tests can only exist in TestFixtures (classes) and the only thing that allows you to nest arbitrary levels in the GUI are namespaces.

What you could do is this:

* File001 (namespace)
  * Tests_File001 (class, text fixture)
    * CheckFileTypeTest (method, test)
    * GetFileTopLevelStructureTest (method, test)
  * CompleteProcessTest (namespace)
    * TestsCompleteProcessTest (class, text fixture)
      * StageOneTest (method, test)
      * StageTwoTest (method, test)
      * StageThreeTest (method, test)

I assume you currently have a setup more along the lines of

[Test]
[TestCaseSource("TestFiles")]
public void StageOneTest(String file)
{
}

where one test method checks a specific thing and you run that test for every file. In that case you'd just have to remove the attributes and call this method from your new tests.

I also get the feeling that you want your tests to execute in a specific order and the different test runners execute your test in different orders. I am not aware of any way to do that and generally speaking you shouldn't. No test should depend on another test having succeeded.

Assuming your tests are somewhat time intensive and you don't want to run all of the "later" tests if an "earlier" test already failed you can consider putting them in appropriate categories like: "Preliminary", "Stage1", etc. Then you can execute one category after the other and see where the first fails.

I hope this helps you.

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