使用基类进行单元测试设置和拆卸

发布于 2024-07-11 21:51:11 字数 217 浏览 3 评论 0原文

假设使用 Visual Studio.NET 2008 和 MsTest 来运行单元测试。 当系统基于服务容器和依赖注入单元测试时,自然需要设置服务。

这些服务是否应该设置在所有单元测试派生自的基类中或在每个单元测试类中设置。 如果它们应该位于基类中,是否有一种方法可以执行 TestInitialize 和 ClassInitialize 方法,而无需从派生类(例如基类)调用它们。初始化?

Assuming Visual Studio.NET 2008 and MsTest are used to run unit tests. When a system is based on a service container and dependency injection unit testing would naturally require the setting up of services.

Should these services rather be setup in a base class that all unit tests derive from or in each unit test class. If they should be in a base class, is there a way to have the TestInitialize and ClassInitialize methods be executed without requiring them to be called from the derived class, e.g base.Initialise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

淡淡绿茶香 2024-07-18 21:51:11

MSTest 框架将在整个对象(所有基类)中搜索标记为 Test* 的方法。 就像在单元测试类上声明它们一样,您不必显式调用它们。

The MSTest framework will search the entire object (all base classes) for the methods marked Test*. Like when you declare them on the unit test class, you do not have to call them explicitly.

深居我梦 2024-07-18 21:51:11

在 2008 中,您应该能够在基类上使用 [TestInitialize],并且只要您不在层次结构中的某个位置添加另一个 [TestInitialize],就应该调用它。 您还可以使用虚拟方法执行操作。

With 2008, you should be able to have [TestInitialize] on a base class, and as long as you don't add another [TestInitialize] somewhere down the hierarchy, it should be called. You could also do things with virtual methods.

自在安然 2024-07-18 21:51:11

我更喜欢将 Test*Class* 标记的方法放在实际的单元测试类上。 如果您在基类上定义它们,则无法向它们添加测试特定活动。 相反,请在基类上使用静态构造函数和实例构造函数以及终结器。

I prefer the Test* and Class* marked methods to be on the actual unit test class. If you define them on a base class, you cannot add test specific activities to them. Instead, use the static and instance constructors and finalizer on your base class.

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