locked
How does MSTest initialize tests? RRS feed

  • Question

  • I'm angry.

     

    How does MSTest initialize individual tests??? Here is what I want to do:

     

    I have a test class with multiple tests defined. Each test requires a clean copy of a database. I have added methods for [TestInitialize] and [TestCleanup]. These methods create and drop the required database, respectively.

    No matter what I try (ClassInitialize/ClassCleanup, AssemblyInitialize/AssemblyCleanup), I keep getting the following error:

    System.Data.SqlClient.SqlException:  System.Data.SqlClient.SqlException: A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.).

    This is driving me absolutely nuts. It seems that tests may all be initialized within one single step? I think there is a major thread sync problem here. One test create the database and starts to execute a test. Another test initializes and cant create the database because it already exists.

    Also, one test create the database while another test is running. The second test drops the database. The first test tries to query it. The database doesnt exist so boom.

    All i want to do is create a database for each test, run the test, drop the database, run the next test. What am i doing wrong??

    • Moved by Min ZhuModerator Tuesday, June 28, 2011 2:38 AM testing question (From:.NET Base Class Library)
    Saturday, June 25, 2011 4:01 AM

Answers

  • Hi,

    Error explains you have a problem with network. I guess tests will be executed one by one....so if starting of every test method have "Cleaning database code" you will have expected result.

    Does it has any InnerExcepttion?

    Just a suggestion - Since this is test environment why dont you think having in-memory/file based database like SQLLite or SQLCompactEdition.


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Saturday, June 25, 2011 4:27 PM

All replies

  • Hi,

    Error explains you have a problem with network. I guess tests will be executed one by one....so if starting of every test method have "Cleaning database code" you will have expected result.

    Does it has any InnerExcepttion?

    Just a suggestion - Since this is test environment why dont you think having in-memory/file based database like SQLLite or SQLCompactEdition.


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Saturday, June 25, 2011 4:27 PM
  • This is long after you marked another answer as accepted, but I'd like to point out that the approach you are using is against best-practices for unit testing.  Each test should be able to run in isolation of other tests.  They should also be able to run in parallel with  other tests from the same suite (which is the problem you are encountering when one test steps on another test's environment). 

    One approach to solving your issue is that each test uses a unique database name.  That way each test could run in parallel because they would all be using their own, private database (and then they should also clean their own database up).

    Depending on tests being run in a serial manner, or assuming that they run in a particular order will always set you up for failure.  

    Tuesday, November 6, 2012 6:47 PM