在VS Testing框架中使用构造函数与TestInitialize()属性之间有什么区别? [英] What's the difference between using the constructor in VS Testing framework vs. TestInitialize() attribute?

查看:177
本文介绍了在VS Testing框架中使用构造函数与TestInitialize()属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,我正在使用Visual Studio的测试框架进行单元测试.只是想知道使用构造函数进行初始化工作与使用具有[TestInitialize()]属性的方法有什么区别?

Quick question, I'm using the Visual Studio's testing framework for unit testing. Just wondering what's the difference between using the constructor to do initialization work vs. having a method with [TestInitialize()] attribute?

推荐答案

This post gives an overview of the different methods. As you can see, the ctor is called immediately before the ClassInitialize (only once, of course) and TestInitialize.

因此在TestInitialize方法中的ClassInitialize中放置需要代码的内容.在ClassInitialize之前应该设置的所有内容都放在ctor中.

So put stuff that requires code in ClassInitialize in your TestInitialize method. Everything that should be set up before ClassInitialize goes in the ctor.

很显然,TestInitialize内容将在每次测试之前执行一次.每次测试后关闭的相应方法为TestCleanup.对于类,请使用ClassCleanup.程序集(AssemblyInitialize/Cleanup)也存在同样的问题.

Obviously, TestInitialize content will be executed once before each test. The corresponding method to close after each test is TestCleanup. For classes, use ClassCleanup. The same thing exists for assemblies as well (AssemblyInitialize/Cleanup).

进一步阅读

这篇关于在VS Testing框架中使用构造函数与TestInitialize()属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆