TestInitialize VS ClassInitialize [英] TestInitialize vs ClassInitialize
问题描述
之间有什么 TestInitialize
VS ClassInitialize
在MSTest的区别?各有什么优点缺点?
What is the difference between TestInitialize
vs ClassInitialize
in MSTest? What are the pros cons of each?
我的印象是, TestInitialze
应与每个测试运行,每次?那是对的吗?和 ClassInitialize
将运行每次任何类的新实例?
I'm under the impression that TestInitialze
should run with EACH test, every time? Is that correct? And that ClassInitialize
will run every time a new instance of any class?
推荐答案
这两个属性是只为它们所属的类(因此测试)提供。
Both attributes are available only for the classes (and hence tests) where they belong.
TestInitialize
每个测试即宣告对在同一类在属性之前运行的声明。
TestInitialize
runs before every test that is declared on the the same class where the attribute is declared.
ClassInitialize
运行的仅的类,其中的属性初始化声明。换句话说,它不会为每个类运行。只是包含 ClassInitialize
方法的类。
ClassInitialize
runs only on the initialization of the class where the attribute is declared. In other words it won't run for every class. Just for the class that contains the ClassInitialize
method.
如果你想将所有的测试之前运行一次的方法或者类初始化使用 AssemblyInitialize
。
If you want a method that will run once before all tests or classes' initialization use the AssemblyInitialize
.
这篇关于TestInitialize VS ClassInitialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!