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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:07:43+00:00 2026-06-16T01:07:43+00:00

Possible Duplicate: What would be an alternate to [SetUp] and [TearDown] in MSTest? I’m

  • 0

Possible Duplicate:
What would be an alternate to [SetUp] and [TearDown] in MSTest?

I’m learning how to use Unit Testing and automated testing in general and have a few questions I can’t figure out/find the answer to

I currently test by having multiple [TestMethod]s that call various methods and have asserts in them. Right now the TestMethod’s all have duplicate code to hit a DB and set themself up for the rest of the test. An example is:

public void TestDBReturnsFooInFormatXyz() {
    var foo = HitDBAndReturnStuff();
    Assert.IsTrue( // foo format is xyz );
}

public void TestDBFooContainsAbc() {
    var foo = HitDBAndReturnStuff();
    Assert.IsTrue( // foo contains abc );
}

So some questions:
Is it a best practice to make private fields in a test class and have the constructor set them?

Should I do it in each TestMethod because testing speed doesn’t matter that much?

What [Test???] do I put on top of the constructor to make sure it gets called when running tests?

I’ve looked at MSDN and the book “Programming Microsoft Visual C# 2008: The Language” and I can’t find any good information on unit testing. If there is a resource I should have read which answers these questions just let me know.

Thanks!

  • 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-16T01:07:44+00:00Added an answer on June 16, 2026 at 1:07 am

    Duplicated code is always a bad practice, so you should refactor your code to initialize the foo object in the TestInitialize Method, which executes once before each test run, and then perform specific operations in each test case:

    private FooType foo;
    
    [TestInitialize()]
    public void TestInitialize()
    {
        foo = CreateFooObject();
    }
    
    [TestMethod()]
    public void TestToAssertThatAbcStuffGetsDone()
    {
        foo.DoAbcStuff();
        Assert.IsTrue(foo.DidAbc());
    }
    
    [TestMethod()]
    public void TestToAssertThatXyzStuffGetsDone()
    {
        foo.DoXyzStuff();
        Assert.IsTrue(foo.DidXyz());
    }
    

    If your foo object needs to be disposed after each test method run, you should also implement a TestCleanup Method.

    However, using a database in your unit tests is not considered a best practice because you don’t want external dependencies affecting your tests results. Instead, you should use a database mock that acts like a database but isolates your unit tests from the real database dependency.

    Several database related problems could break your tests, such as connection failures, timeouts, data changes and occasional database upgrades. This SO question discusses strategies for handling the database dependency issue.

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

Sidebar

Related Questions

Possible Duplicate: When to Use Static Classes in C# Questions in the title..........i would
Possible Duplicate: Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
Possible Duplicate: How would you do a not in query with Linq? I have
Possible Duplicate: When or why would you use a right outer join instead of
Possible Duplicate: Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
Possible Duplicate: How would I stop this foreach loop after 3 iterations? I have
Possible Duplicate: Clearing all cookies with javascript I would like to have a checkbox
Possible Duplicate: Alternative for PHP_excel A friend would like to use a Php Excel
Possible Duplicate: Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
Possible Duplicate: C/C++: When would anyone use a union? Is it basically a remnant

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.