C#单元测试代码题

标签 c# unit-testing

我们开始使用 C# 内置的单元测试功能。我有 VisualStudio 2008 为我创建的单元测试代码。我对生成的代码几乎没有疑问。以下是我从生成的文件中复制的代码:

  #region Additional test attributes
  // 
  //You can use the following additional attributes as you write your tests:
  //
  //Use ClassInitialize to run code before running the first test in the class
  //[ClassInitialize()]
  //public static void MyClassInitialize(TestContext testContext)
  //{
  //}
  //
  //Use ClassCleanup to run code after all tests in a class have run
  //[ClassCleanup()]
  //public static void MyClassCleanup()
  //{
  //}
  //
  //Use TestInitialize to run code before running each test
  //[TestInitialize()]
  //public void MyTestInitialize()
  //{
  //}
  //
  //Use TestCleanup to run code after each test has run
  //[TestCleanup()]
  //public void MyTestCleanup()
  //{
  //}
  //
  #endregion

如果我需要 initialize 和 cleanup 方法,是否需要在启用它们时从方法名称中删除那些“My”?

  //Use ClassInitialize to run code before running the first test in the class
  //[ClassInitialize()]
  //public static void MyClassInitialize(TestContext testContext)
  //{
  //}

我是否需要在运行第一个测试之前在某处调用 "MyClassInitialize" 方法,或者它会在调用其他方法之前自动调用。

其他三种方法的类似问题,它们是否在正确的时间范围内自动调用?

最佳答案

这些方法由测试框架自动调用。如果你愿意,你可以重命名它们,我相信,因为它使用属性来标识在适当时间调用的适当方法。请记住取消对属性以及方法本身的注释,否则测试框架将无法找到并调用该方法。

关于C#单元测试代码题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714073/

相关文章:

c# - System.Array 到列表的转换

c# - OO 中的单元测试和访问修饰符

java - 如何对 QueryDSL 条件生成进行单元测试?

c# - InDesign COM 引用位于 obj 中但不在 bin 目录中

C# Mono MySql 连接器 :Unable to connect to any specified MySQL hosts

c# - 从 C# 中的枚举中获取 int 值

ios - 如何使用 Instrument 运行 Xcode UnitTest

c# - c#中强制转换的优先级

javascript - Angular - 提交时测试服务订阅

objective-c - OCMock - 尽管是预期的,但调用了意外的方法