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?


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.


  • Coded ui - Read/Write values from .csv files
  • MS Test: Getting executing testlocation?
  • ClassInitialize attribute in unit test based class not called
  • Render a SceneGraph with PyOpenGL
  • @Autowired bean is null in Test Listener class
  • C# unit test code questions
  • Effort Unit test: Argument 'xmlReader' is not valid
  • Data Driven Test on Visual Studio using a complex serialized object
  • Most efficient way to move table rows from one table to another
  • Center align outputs in ipython notebook
  • How does document.ready work with angular element directives?
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Asynchronous UI Testing in Xcode With Swift
  • Lost migrations and Azure database is now out of sync
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • angularjs unit test when to use $rootScope.$new()
  • Perl system calls when running as another user using sudo
  • ActionScript 2 vs ActionScript 3 performance
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Cannot Parse HTML Data Using Android / JSOUP
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app