在 NUnit 中延迟加载 TestCaseSource [英] Delay-loading TestCaseSource in NUnit

查看:42
本文介绍了在 NUnit 中延迟加载 TestCaseSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

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

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.

一些教条式的单元测试人员可能会反驳说我正在使用 NUnit 做一些它不应该做的事情.我不得不承认我必须满足一个要求,而 NUnit 是一个非常棒的工具,它的 GUI 满足了我的大部分要求,以至于我不在乎它是否适当的单元测试.

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.

附加信息(来自 NUnit 文档)

Additional info (from NUnit documentation)

对象构造注意事项

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

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.

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

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.

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

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?

注意:

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

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

有计划在 NUnit 的后续版本中引入此选项.

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

推荐答案

我不知道在 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.

这篇关于在 NUnit 中延迟加载 TestCaseSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆