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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:04:04+00:00 2026-05-10T23:04:04+00:00

I was wondering whether the object to test should be a field and thus

  • 0

I was wondering whether the object to test should be a field and thus set up during a SetUp method (ie. JUnit, nUnit, MS Test, …).

Consider the following examples (this is C♯ with MsTest, but the idea should be similar for any other language and testing framework):

public class SomeStuff {     public string Value { get; private set; }      public SomeStuff(string value)     {         this.Value = value;     } }   [TestClass] public class SomeStuffTestWithSetUp {     private string value;     private SomeStuff someStuff;      [TestInitialize]     public void MyTestInitialize()     {         this.value = Guid.NewGuid().ToString();         this.someStuff = new SomeStuff(this.value);     }      [TestCleanup]     public void MyTestCleanup()     {         this.someStuff = null;         this.value = string.Empty;     }      [TestMethod]     public void TestGetValue()     {         Assert.AreEqual(this.value, this.someStuff.Value);     } }  [TestClass] public class SomeStuffTestWithoutSetup {     [TestMethod]     public void TestGetValue()     {         string value = Guid.NewGuid().ToString();         SomeStuff someStuff = new SomeStuff(value);         Assert.AreEqual(value, someStuff.Value);     } } 

Of course, with just one test method, the first example is much too long, but with more test methods, this could be safe quite some redundant code.

What are the pros and cons of each approach? Are there any “Best Practices”?

  • 1 1 Answer
  • 3 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. 2026-05-10T23:04:04+00:00Added an answer on May 10, 2026 at 11:04 pm

    It’s a slippery slope once you start initializing fields & generally setting up the context of your test within the test method itself. This leads to large test methods and really really unmanageable fixtures that don’t explain themselves very well.

    Instead, you should look at the BDD style naming & test organization. Make one fixture per context, rather than one fixture per system-under-test. Then your [setup] truly does setup the context, and your tests can be simple one-liner asserts.

    It’s much easier to read when you see a test output that does this:

    OrderFulfillmentServiceTests.cs

    • with_an_order_from_a_new_customer

      • it should check their credit from the credit service
      • it should give no discount
    • with valid credit check

      • it should decrement inventory
      • it should ship the goods
    • with a customer in texas or california

      • it should add appropriate sales tax
    • with an order from a gold customer

      • it should NOT check credit
      • it should get expedited shipping added for free

    Our tests are now really good documentation for our system. Each ‘with_an…’ is a test fixture, and the items below it are tests. Within those, you setup the context (the state of the world as the class name describes) and then the test does the simple assert that verifies what the method name says it does.

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

Sidebar

Related Questions

I'm wondering whether I should create extension methods that apply on the object level
Just wondering whether an object can self-destruct. Consider this situation. An object that extends
Just wondering whether an object can self-destruct. Consider this situation. An object that extends
I'm updating an old project, and I'm wondering whether I should remove some objects
I'm wondering whether or not there are any restrictions during HBase minor/major compactions .
I'm wondering whether an object asking a question to another object that indirectly holds
I've been wondering whether using prototypes in JavaScript should be more memory efficient than
I'm wondering whether there's an existing framework or technology concept for the following project.
I am wondering whether there is any feature like method inheritance rather than whole
I've managed to boil this down to the following test case but I'm wondering

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.