Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:36:40+00:00 2026-06-08T03:36:40+00:00

ClassInitialize seems to be getting called for every test. I guess it is because

  • 0

ClassInitialize seems to be getting called for every test. I guess it is because a new class is actually created for every test.

Why? Every other framework doesn’t do this!

Anyways… I need to execute one method (not static) on the class. I also need to execute all the test methods within the same instance of the class.

Am I up a creek without a paddle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:36:41+00:00Added an answer on June 8, 2026 at 3:36 am

    ClassInitialize is called once by MSTest before any of the TestMethods are invoked, see remarks here. TestInitialize is called once before each test method. MSTest creates a new instance of the test class for each TestMethod call. This is why ClassInitialize is a static method.

    I need to execute one method (not static) on the class. I also need to execute all the test methods within the same instance of the class.

    Do you mean you need to execute one method on the TestClass or the class under test (the class you are actually testing)?

    In either case, you can have a static member in the TestClass and initialize it once in ClassInitialize. It will be created only once and exist for the lifetime of your tests. You can invoke a method on it only once. You can then use this single instance in each of your test methods.

    One thing to be aware of is that MSTest may interleave tests from different classes. So if you have any global mutable state which is accessed from more than one ClassInitialize (or test for that matter), unpredictable things might happen. For that reason, statics are best avoided.

    The requirement that all methods must be executed on the same instance is quite unusual. Perhaps there is a way to refactor your code to eliminate this constraint?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found out this morning that proc.new works in a class initialize method, but
We start use C# build-in unit test functionality. I have VisualStudio 2008 created unit
I'm trying to create a test class which organizes its test methods using inner
var SortingTable = new Class({ initialize: function( table, options ) { this.table=$(table); this.tbody =
I'm using the unit testing framework in .NET in C++/CLI to test unmanaged C++
I have this little script: var moolang = new Class({ initialize: function(element) { this.el
I'm getting this exception thrown when I run my tests: Test method OuvertureClasseur threw
Sorry for my english, I'm french :) I created a Mootools class named Slider.
If arrays are returned by reference, why doesn't the following work: 'Class1 class module
I have created a new solution with a minimal amount of code that represents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.