简体   繁体   English

C#单元测试代码问题

[英]C# unit test code questions

We start use C# build-in unit test functionality. 我们开始使用C#内置单元测试功能。 I have VisualStudio 2008 created unit test code for me. 我有VisualStudio 2008为我创建的单元测试代码。 I have few question above the generated code. 我在生成的代码上面几乎没有问题。 Following are code I copied from the generated file: 以下是我从生成的文件中复制的代码:

  #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

If I need the initialize and cleanup methods, do I need to remove those "My" from the method name when I enable them? 如果我需要初始化和清理方法,我启用它们时是否需要从方法名称中删除那些“我的”?

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

Do I need to call the "MyClassInitialize" method somewhere before running the first test or it will be called automatically before other methods are called. 在运行第一个测试之前,我是否需要在某处调用"MyClassInitialize"方法,否则在调用其他方法之前会自动调用它。

Similar questions for other three methods, are they called automatically at right time frame? 其他三种方法的类似问题是否会在正确的时间框架内自动调用?

The methods are called automatically by the test framework. 这些方法由测试框架自动调用。 You can rename them if you want, I believe, as it uses the attributes to identify the proper methods to invoke at the proper time. 我相信你可以重命名它们,因为它使用属性来识别在适当的时候调用的正确方法。 Remember to uncomment the attributes as well as the method itself or the testing framework won't be able to find the method and invoke it. 请记住取消注释属性以及方法本身,否则测试框架将无法找到方法并调用它。

No, the name of the methods are irrelevant. 不,方法的名称是无关紧要的。 Name them whatever you want. 将它们命名为您想要的任 It's the attributes above the methods that tell the testing runtime what the methods are for. 它是方法之上的属性,告诉测试运行时方法的用途。

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

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