未调用基于单元测试的类中的 ClassInitialize 属性
我在 TestBase 类中添加了这些方法:
[ClassInitialize]
public static void InitializBeforeAllTests()
{
}
但是当我在调试中运行单元测试 Test1()
时:
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
}
TestBase.InitializBeforeAllTests()
方法永远不会被调用。 为什么?
I added these method in a TestBase class :
[ClassInitialize]
public static void InitializBeforeAllTests()
{
}
But when I run in Debug an unit test Test1()
:
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
}
The TestBase.InitializBeforeAllTests()
method is never called.
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在方法上声明 ClassInitialize 属性时,该方法必须是静态、公共、void(或任务,如果异步)并且应采用 TestContext 类型的单个参数。
如果您在同一单元测试中还有其他具有 AssemblyInitialize 属性的方法,则测试将运行,但会跳过所有测试方法,并直接进入 AssemblyCleanup 或直接退出。
尝试 MSDN 中的 ClassInitialize 属性。
When declaring ClassInitialize attribute on a method, the method has to be static, public, void (or Task, if async) and should take a single parameter of type TestContext.
If you're having also other method with the AssemblyInitialize attribute on the same unit test, the test will run but will skip on all test methods and will go directly to AssemblyCleanup or just quit.
Try the example on ClassInitialize attribute in MSDN.
我知道这是一个非常古老的问题,但在寻找类似问题时,它是第一个在谷歌搜索中弹出的问题,无论如何,这里是答案的更新:
注意:您需要 MSTest.TestFramework-Version 2.0.0 包或更高版本为了这个工作。
i know this is a very old question, but its the first to popup in google search when looking for a similar problem, anyhow, here is an update for the answer:
Note: you need MSTest.TestFramework-Version 2.0.0 package or newer for this to work.
您可以在基类中设置程序集初始化方法。与 ClassInitialize 不太一样,但它是一个可行的选择。来源:提到的解决方法在这里。
您还可以添加一个 Cleanup 方法:
You can setup an assembly initialize method in your base class. Not quite the same as ClassInitialize, but it's a viable option. Source:The Workaround mentioned here.
You can also add a Cleanup method:
无论出于何种原因,单元测试框架的 UnitTestExecuter 只允许为每个测试类定义一个 ClassInitialize 和一个 ClassCleanup 方法...与 TestInitialize 和 TestCleanup 方法不同,它们在派生测试类和基测试类中都被调用...
for whatever reason, the unit test framework's UnitTestExecuter only allows one ClassInitialize and one ClassCleanup method to be defined per test class... unlike TestInitialize and TestCleanup methods, which get called in both the derived and base test class...
MS 链接不再工作。
无论如何,解决此问题的一种方法是将初始化代码移至基类的构造函数中。这将确保每当实例化任何后代类时都会调用它。
The MS link is not working anymore.
Anyway, one way to work around this issue is to simply move your initialization code into the constructor of the base class. This will ensure that it gets called from any descendant classes whenever they are instantiated.
就我而言,我应用了
[Ignore]
属性(测试是手动运行的)这导致
[AssemblyInitialize]
永远不会被调用如果我删除了
[Ignore]
属性,[AssemblyInitialize]
按预期被调用。奇怪的是,无论是否将
[Ignore]
应用于我的测试,仍然会调用[AssemblyCleanup]
In my case, I had the
[Ignore]
attribute applied (test is ran manually)This caused
[AssemblyInitialize]
to never be calledIf I removed the
[Ignore]
attribute[AssemblyInitialize]
was called as expected.Oddly,
[AssemblyCleanup]
is still called with or without the[Ignore]
applied to my test就我而言,由于我没有将
TestContext
作为参数传递,因此从未调用过 [ClassInitialize] 方法。即使它是可选的,我想你每次都必须传递它,即使你不在方法中使用它。所以方法签名
对我有用。
In my case
[ClassInitialize]
method was never called due to the fact that I haven't passedTestContext
as a parameter. Even if it's optional I guess you have to pass it every time even though you don't use it in the method.So method signature
worked for me.