具有外部主机进程依赖性的单元测试
我想针对 .Net 类库程序集中的类运行一些 NUnit 单元测试,该程序集设计为由外部进程(在我的控制范围之外)托管并在运行时加载。
我想要测试的类派生自主机 exe 中定义的类,该类需要在主机进程中实例化。任何在主机进程之外实例化基类派生类的尝试都会失败并出现异常。
因此,我无法通过简单地加载程序集并实例化该类来测试 NUnit gui 或控制台测试主机中的类。有人对我如何执行这些测试有任何建议吗?
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 instantiating the class. Does anyone have any suggestions as to how I can execute these tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会将类分为两个类 - 一个从外部进程中的类型派生的“外部”类,以及一个完成所有工作但不从您无法控制的任何内容继承的“内部”类。
然后,测试内部类,并让外部类尽可能没有逻辑。
当您可以以某种方式将代码与依赖项隔离时,单元测试几乎总是更容易。
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.
如果您想测试您的源代码并在您的过程中调用那些外部类方法,也许模拟整个外部过程可以解决您的问题。
您可以将模拟设计得像您的主机类一样。
包括:
所有可能输出因为我不是 .NET 开发人员,所以我不熟悉细节,但我知道 .NET 中的模拟引擎是相当强大。
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:
Since I'm not a .NET developer I'm not familiar with details, but I know that mocking engine in .NET is quite powerful.