具有外部主机进程依赖性的单元测试

发布于 2024-08-19 18:25:33 字数 226 浏览 4 评论 0原文

我想针对 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赴月观长安 2024-08-26 18:25:33

我可能会将类分为两个类 - 一个从外部进程中的类型派生的“外部”类,以及一个完成所有工作但不从您无法控制的任何内容继承的“内部”类。

然后,测试内部类,并让外部类尽可能没有逻辑。

当您可以以某种方式将代码与依赖项隔离时,单元测试几乎总是更容易。

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.

划一舟意中人 2024-08-26 18:25:33

如果您想测试您的源代码并在您的过程中调用那些外部类方法,也许模拟整个外部过程可以解决您的问题。

您可以将模拟设计得像您的主机类一样。

包括:

  • 模拟加载运行时库(来自您感兴趣的范围)
  • 模拟特定方法
  • 模拟异常的

所有可能输出因为我不是 .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:

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文