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

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer So since these higher level languages manage the memory for… May 11, 2026 at 10:01 am
  • added an answer I've never actually bothered to figure out how to extract… May 11, 2026 at 10:01 am
  • added an answer This is not related to exceptions but rather to undefined… May 11, 2026 at 10:01 am

Related Questions

I was wondering whether the object to test should be a field and thus
I was wondering whether SVN actually compresses the binary content on the server during
I'm fairly new to the STL, so I was wondering whether there are any
I liked the discussion at Differences in Generics , and was wondering whether there
I was wondering whether overriding conversion operators is only applicable to numeric types? If
I was just wondering whether there is some way to do this: I have
I was wondering how to check whether a variable is a class (not an
I was wondering, whether knockd http://www.zeroflux.org/cgi-bin/cvstrac.cgi/knock/wiki would be a good was to be able
I was wondering if there are any alternatives to Microsoft's SQL Server Management Studio?
I was wondering if InputManagers are still part of Leopard and if there is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.