简体   繁体   English

为什么要使用ClassCleanup?

[英]Why should one use ClassCleanup?

Currently I'm testing out the Unit Test features of .NET Framework and therefore came across [TestInitialize] [TestCleanup] [ClassInitialize] and [ClassCleanup] . 目前,我正在测试.NET Framework的单元测试功能,因此遇到了[TestInitialize] [TestCleanup] [ClassInitialize][ClassCleanup] Why should I use [ClassCleanup] because I cannot come up with any scenarios where I could use [ClassCleanup] in a useful way. 为什么我应该使用[ClassCleanup]因为我无法提出可以以有用的方式使用[ClassCleanup]任何方案。

I think it might be similar to [TestCleanup] which cleans up after every [TestMethod] but [ClassCleanup] is called after running through all tests and not each one (as explained to me). 我认为它可能类似于[TestCleanup] ,它在每个[TestMethod]之后都进行清理,但在运行所有测试而不是每个测试之后都会调用[ClassCleanup] (如我所解释的)。 This is the reason why I don't know how to use it or whether to use it at all (right now I'm not using it at all). 这就是为什么我不知道如何使用它或根本不使用它的原因(现在我根本不使用它)。

Fell free to explain this to me and thanks in advance! 随意向我解释一下,在此先感谢! :) :)

You might want to use it to cleanup the resources that you created in ClassInitialize . 您可能要使用它来清理您在ClassInitialize创建的资源。 Imagine you want to spin a database for your test class that all test methods should be able to use. 假设您要为所有测试方法都应能够使用的测试类旋转数据库。 You create it in ClassInitialize and destroy in ClassCleanup . 您可以在ClassInitialize创建它,并在ClassCleanup销毁ClassCleanup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM