开发者

C# unit test code questions

We start use C# build-in unit test functionality. I have VisualStudio 2008 created unit test code for me. I have few question above th开发者_如何学Goe 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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜