开发者

Unit testing with an external host process dependency

I'd like to run some NUnit unit tests against a class in a .Net class library assembly which is designed to be hosted by an external process (outside of my control) and loaded at runtime.

The class I want to test derives from a class defined within the host exe that requires it to be instantiated within the host process. Any attempt to instantiate a derivative of the base class outside of the host process fails with an exception.

I am therefore not able to test the class in the NUnit gui or console test hosts by simply loading the assembly and i开发者_如何学Gonstantiating the class. Does anyone have any suggestions as to how I can execute these tests?


I'd probably split the class into two classes - an 'outer' class that derives from the type in the external process, and an 'inner' class that does all the work, but does not inherit from anything that you do not control.

Then, test the inner class, and leave the outer class as devoid of logic as possible.

Unit testing is almost always easier when you can isolate your code from your dependencies in some fashion.


In case when You want to test Your source and inside Your procedures You've those external class methods invokes, maybe mocking whole external process would solve You're problem.

You could design the mock to behave like Your host class.

Including:

  • mock loading runtime libraries (those from your scope of interest)
  • mock all possible output from particular method
  • mock exceptions

Since I'm not a .NET developer I'm not familiar with details, but I know that mocking engine in .NET is quite powerful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜