How to unit test my project against different versions of external dll api?
I am developing an app that relies heavily on an external dll, my a开发者_开发百科pp 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!
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
- use the last adapter/facade if it meets your need
- roll out a new one to fix any breaking changes ; ensure that you run it against the contract tests so that your client doesn't break with this adapter.
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:
- Copy "old version" to binary folder
- Run tests (test-task in CI-system)
- Copy "new version" to binary folder
- Run tests 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.
精彩评论