在 NUnit 中延迟加载 TestCaseSource

发布于 2024-11-03 17:07:30 字数 1198 浏览 5 评论 0原文

我有一些使用 TestCaseSource 函数的 NUnit 测试。不幸的是,我需要的 TestCaseSource 函数需要很长时间才能初始化,因为它递归地扫描文件夹树以查找将传递到测试函数的所有测试图像。 (或者,它可以在每次运行时从文件列表 XML 加载,但仍然需要自动发现新图像文件。)

是否可以与 TestCaseSource 一起指定 NUnit 属性,以便 NUnit 不枚举测试用例(不调用 TestCaseSource 函数)直到用户单击节点,或者直到测试套件正在运行?

项目要求需要将所有测试图像存储在文件夹中,因为无权访问测试项目的其他人将需要向该文件夹添加新的测试图像,而无需修改测试项目的源代码。然后他们就可以查看测试结果。

一些教条式的单元测试人员可能会反驳说我正在使用 NUnit 来做一些它不应该做的事情。我必须承认我必须满足一个要求,NUnit 是一个很棒的工具,它有一个很棒的 GUI,可以满足我的大部分要求,所以我不关心它是否是正确的单元测试。

其他信息(来自 NUnit 文档)

对象构造注意事项

NUnit 将测试用例定位在 加载测试时,创建 每个类的实例 非静态源并构建列表 要执行的测试。每个来源 对象仅在此创建一次 时间并在所有测试后被销毁 已加载。

如果数据源在测试中 固定装置本身,该对象被创建 使用适当的构造函数 上提供的夹具参数 TestFixtureAttribute 或默认值 构造函数(如果没有参数) 指定的。由于这个对象是 在测试运行之前销毁,不 之间可以进行通信 这两个阶段 - 或之间 不同的运行 - 除了通过 参数本身。

预先加载测试用例的目的似乎是避免 TestCaseSource 和测试执行之间的通信(或副作用)。这是真的吗?这是要求预先加载测试用例的唯一原因吗?

注意:

需要对 NUnit 进行修改,如 http://blog.sponholtz.com/2012/02/late-binded-parameterized-tests-in.html

计划将此选项引入 NUnit 的更高版本。

I have some NUnit tests which uses a TestCaseSource function. Unfortunately, the TestCaseSource function that I need takes a long time to initialize, because it scans a folder tree recursively to find all of the test images that would be passed into the test function. (Alternatively it could load from a file list XML every time it's run, but automatic discovery of new image files is still a requirement.)

Is it possible to specify an NUnit attribute together with TestCaseSource such that NUnit does not enumerate the test cases (does not call the TestCaseSource function) until either the user clicks on the node, or until the test suite is being run?

The need to get all test images stored in a folder is a project requirement because other people who do not have access to the test project will need to add new test images to the folder, without having to modify the test project's source code. They would then be able to view the test result.

Some dogmatic unit-testers may counter that I am using NUnit to do something it's not supposed to do. I have to admit that I have to meet a requirement, and NUnit is such a great tool with a great GUI that satisfies most of my requirements, such that I do not care about whether it is proper unit testing or not.

Additional info (from NUnit documentation)

Note on Object Construction

NUnit locates the test cases at the
time the tests are loaded, creates
instances of each class with
non-static sources and builds a list
of tests to be executed. Each source
object is only created once at this
time and is destroyed after all tests
are loaded.

If the data source is in the test
fixture itself, the object is created
using the appropriate constructor for
the fixture parameters provided on the
TestFixtureAttribute or the default
constructor if no parameters were
specified. Since this object is
destroyed before the tests are run, no
communication is possible between
these two phases - or between
different runs - except through the
parameters themselves.

It seems the purpose of loading the test cases up front is to avoid having communications (or side-effects) between TestCaseSource and the execution of the tests. Is this true? Is this the only reason to require test cases to be loaded up front?

Note:

A modification of NUnit was needed, as documented in http://blog.sponholtz.com/2012/02/late-binded-parameterized-tests-in.html

There are plans to introduce this option to later versions of NUnit.

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

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

发布评论

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

评论(1

如梦初醒的夏天 2024-11-10 17:07:30

我不知道在 GUI 中延迟加载测试名称的方法。我的建议是将这些测试转移到单独的程序集中。这样,您可以快速运行所有其他测试,并仅在需要时加载较慢的详尽测试。

I don't know of a way to delay-load test names in the GUI. My recommendation would be to move those tests to a separate assembly. That way, you can quickly run all of your other tests, and load the slower exhaustive tests only when needed.

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