带有许可证文件的单元测试产品
我已经开始开发使用许可证文件的产品。需要读取(并验证)这些文件才能使应用程序正常工作。这会在单元测试中导致一些问题,如果没有适当的许可证,它会引发异常。
我们正在使用 NUnit,我需要做的是:
- 在运行测试之前将许可证文件复制到影子复制目录中。
- 将工作目录设置为原始构建输出文件夹,以便文件名在临时测试文件夹中仍然有效。
我知道在单元测试中通常应该避免文件访问,但在重构开始之前,我们需要单元测试到位,所以我需要它才能工作。
Ive started working on a product that uses license files. These files need to be read (and verified) in order for the application to work. This causes some problems in the unit tests, without the proper license it throws exceptions.
We are using NUnit and what I need to do is either:
- Copy the license file into the shadow copied directory before the test is run.
- Set the working directory to the original build output folder so that file names are still valid in the temporary test folder.
I know that file access should generally be avoided in unit tests but before the refactoring can begin, we need the unit tests in place so I need this to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您的测试目标太远了,您可以为更下游的功能编写测试。虽然我认为,一次全面测试并不是一个好方法,但只是一个开始。尝试平衡尝试让此测试工作所花费的时间与如何花费这些时间来测试更小、更有针对性的工作单元。
I think you are aiming your test too far up the chain, you could write tests for the functions further down. One blanket test is not really a good way to do it, a start though I suppose. Try to balance the amount of time spent trying to get this test to work with how that time might be spent testing smaller more targetted units of work.
您需要编写一个模拟类来替换正在读取许可证文件的类。您可以使用最小起订量来实现这一点。
You need to write a mock class to replace the class that is reading the license file. You can use MOQ to achieve this.
禁用 NUnit 的卷影复制功能将使测试在构建的同一输出文件夹中运行。但这也会阻止新的构建(因为该文件正在使用中)。在我看来,这是一个糟糕的解决方案,但仍然是一个解决方案。
Disabling the Shadow Copy feature of NUnit will make the tests run in the same output folder as they were built in. But this will also block new builds (since the file is in use). A bad solution, imo, but a solution nontheless.
我建议你从阅读这本书开始:
它将让您深入了解如何打破打开此类问题。为了使此类代码可测试,将需要进行一定程度的更改,而这些更改必须在没有测试的情况下进行,但要尽可能少地进行,并且要非常小心地进行。
在您的情况下,由于注入伪造阅读许可证的类太过跳跃,因此您可以做的是更改验证许可证文件的类,以便实际的验证逻辑
在单个方法中是从单个方法启动,该方法告诉类的其余部分许可证没有问题,并使该方法成为虚拟的,然后使用重写该方法的子类进行测试,以假装它验证了文件。然后,一旦您对该类进行了一些测试,您就可以转储该方法和子类,以支持正确注入的类。
(编辑是为了回应验证很复杂的事实)。
I suggest you start with reading this book:
It will give you lots of insight into how to break open this type of problem. There will be some level of change to make such code testable that will have to happen without tests, but keep it as little as possible and do it very very carefully.
In your case, since injecting classes that fake reading licenses is too much of a jump, what you can do is change the class that validates the license file so that the actual validation logic is
in a single methodis launched from a single method that tells the rest of the class that the license is fine and make that method virtual, and then test with a subclass that overrides the method to pretend that it validated the file.Then, once you have some tests around this class you can dump the method and subclass in favor of a properly injected class.
(Edited to respond to the fact that the validation is complex).
不一定要回答您的问题,但通常当我们购买软件产品的许可证时,它通常会附带某种类型的支持。打电话看看供应商是否有他们推荐的解决方案可能不是一个坏主意。
Not necessary an answer to your question, but usually when we buy a license for a software product it usually comes with some type of support. It might not be a bad Idea to just call and see if the vendor has a solution they would recommend.