Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

Similar questions for other three methods, are they called automatically at right time frame?

like image 497
5YrsLaterDBA Avatar asked Sep 15 '25 04:09

5YrsLaterDBA


1 Answers

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.

like image 74
tvanfosson Avatar answered Sep 17 '25 18:09

tvanfosson