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 7641579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:57:36+00:00 2026-05-31T08:57:36+00:00

I wonder what the best practice is for initializing instance variables in a test

  • 0

I wonder what the best practice is for initializing instance variables in a test class under MSTest. Let’s say I have a test class where there is a lot of overhead to mocking and setting up supporting objects. I want to just do this once, instead of repeating the same code in each test. My thought was to use the MyClassInitialize method to initialize some global instance variables that all tests had access to. That way, I initialize the global instance variables once and they are just used by each test as they run.

Unfortunately, the MyClassInitialize method is static, so cannot initialize global instance variables. I thought about making the global instance variables static, but doesn’t seem to be the right solution. I next thought about just putting the initialization code in a constructor of the test class itself, but something inside me keeps saying that MyClassInitialize is what I am supposed to be using. Another thought would be to use MyTestInitialize since that method is not static, but that would be creating the object over and over with each test. Is that appropriate?

Are there best practices for how to use variables across tests where those variables need only be initialized once before the tests run? Below is a contrived example of what I am talking about.

[TestClass()]
public class ProgramTest
{
    // this object requires extensive setup so would like to just do it once
    private SomeObjectThatIsUsedByAllTestsAndNeedsInitialization myObject;
    private TestContext testContextInstance;

    [ClassInitialize()]
    public static void MyClassInitialize(TestContext testContext)
    {
        // initializing SomeObjectThatIsUsedByAllTestsAndNeedsInitialization clearly will
        // not work here because this method is static.
    }

    [TestMethod()]
    public void Test1()
    {
        // use SomeObjectThatIsUsedByAllTestsAndNeedsInitialization here
    }

    [TestMethod()]
    public void Test2()
    {
        // use SomeObjectThatIsUsedByAllTestsAndNeedsInitialization here
    }

    [TestMethod()]
    public void Test3()
    {
        // use SomeObjectThatIsUsedByAllTestsAndNeedsInitialization here
    }
}
  • 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-05-31T08:57:37+00:00Added an answer on May 31, 2026 at 8:57 am

    Use [TestInitialize] and [TestCleanup] whenever possible. A unit test should be fast and isolated, so the cleanest way is to initialise and cleanup for each test. This ensures the results of a test are not influenced by another test. When the initialization of a test takes long, you probably didn’t wite a unit test but an integration test.

    The exception is integration tests that go to the database or another resource, it is possible that you want to execute an operation once, and then verify the result with multiple assertions (TestMethods). I used to have a specific generic class with a context type that is only initialized once per class. But now I think this is overkill, and just put the dependencies and results in private static variables.

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

Sidebar

Related Questions

I have a dilema and wonder what is business best practice. I'm using Uploadify
I wonder if there's a best practice when sharing Xcode4 project between multi-developer team.
I wonder, what is the best practice for object validation. Is there any extra
I wonder if there are any common or best practice techniques out there if
I just wonder what the best approach is to have multiple users work on
Whenever I design a database, I always wonder if there is a best way
I'm writing a library and wonder what's the best practice for datatypes used in
I wonder what's the best practice to make a db update thread safe. I
From what I have read best practice is to have classes based on an
I wonder what's the best practice in C# regarding mutable / immutable interfaces. I

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.