如何针对不同版本的外部 dll api 对我的项目进行单元测试?
我正在开发一个严重依赖外部 dll 的应用程序,我的应用程序需要支持新版本的 dll 并向后兼容旧版本。
有没有什么好的方法可以让我的单元测试针对所有这些不同的 dll 版本,而无需在新版本的 api 发布后立即重写测试?如何最好地处理这个问题?
谢谢!
I am developing an app that relies heavily on an external dll, my app needs to support new versions of the dll as well as being backwards compatible with the old ones.
Are there any good ways to have my unit tests target all of these different dll versions without the need to rewrite the tests as soon as a new version of the api is released? How is this best handled?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写包装外部 DLL 的适配器或外观。使其实现一个接口 IExternalDLL (当然选择一个更好的名称),该接口记录/指定您对外部 DLL 的需求;它不必完全模仿实际实现的函数签名。
按照您期望接口工作的方式,针对接口编写一组“契约测试”。
现在,您可以为每个新版本编写不同的适配器 - 以防从 v1 到 v2 发生一些重大更改。您的客户端由于接口而被抽象。适配器/外观的工作是确保 dll 的相应版本满足合同测试。您编写一组测试并使用适配器/外观的所有实现来练习它。
下次推出新版本时 -
Write an Adapter or Facade that wraps the external DLL. Make it implement an interface IExternalDLL (of course choose a better name), which documents/specifies your needs from the external DLL ; it does not have to exactly mimic the function signatures of the actual implementation.
Write up a set of 'contract tests' against the interface, the way you expect the interface to work.
Now you can write different adapters per new version - in case of some breaking changes from v1 to v2. Your client is abstracted due to the interface.. Its the job of the adapter/facade to make sure the corresponding version of the dll meets the contract tests. You write one set of tests and exercise it with all implementations of the adapter/facade.
The next time a new version is rolled out - you can
如果您使用 nant、team build 或类似工具,您可以将 lib 二进制文件复制到测试项目在运行测试之前从中获取其二进制文件的文件夹。
伪示例:
5 ..
这非常简单,应该很容易尝试。注意:“编写适配器或外观”答案是使您的程序针对不同版本工作的首选方式,因此也这样做,我只是在谈论针对多个版本的实际测试。
If you are using nant, team build or similar you could xcopy the lib binary to the folder which your test project fetches its binaries from before running the tests.
Pseudo example:
5 ..
This is pretty simple and should be easy to try out. Note: "The write an adapter or facade" answer is the preferred way of making your program work against different version so do that too, I'm just talking about actually testing against multiple versions.