locked
Testing initialize - do something before run tests RRS feed

  • Question

  • Hello

    I'm wondering if there is possibility to create a testing record in DB before running tests. I know that there is ClassInitialize() and TestInitialize() but those work fine in each class and test. I have many classes which requires a simple record of user in db. I don't want to create record int db each class is initializing.

    I have idea, to create a testmethod with highest priority. In this test put something in database, sleep test for about ~30seconds and clean-up database.

    Any ideas will be very helpful.

    I use vs 2010pro

    Tuesday, November 30, 2010 12:04 PM

All replies

  • Hi,

    of course you can do a seperate test taht is doing this initialisation. But that is something that I would not recommend, because each test should run fine without a dependency to other tests.

    So at least we make sure, that each test has a propper initialisation and cleanup (if required) so each test can be run independently. If you have just one initialisation, you should think of:

    - moving the code to a seperate class (So you do not have to repeat the code)
    - Maybe have a propper test, if the initialisation was done already (Depending on the initialisation, this could speed up things. But if you want to clean up afterwards again, it could be unwanted).

    This is just, how we do this stuff in our team. I hope it was of some help for you and maybe someone else has much better ideas.

    With kind regards,

    Konrad

    Tuesday, November 30, 2010 12:25 PM
  • Hi Piter,

    You should refactor and split your tests. Make sure you test for correct retrieval of the data from the db (this is an integration test). If you don't have one already, create a repository class that shields all database access. Then stub this repository class and setup the data as if it would have been called from the db.

    Most important thing to grasp here is that a) is a test smell to depend on database state b) is a test smell to access the db for any other logic than contained in your repository (and the repository should be SOLID).

    As a side effect, your tests will run much, much faster. 

     

    Kind regards,
    Tom de Koning


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Tom de Koning Tuesday, November 30, 2010 10:23 PM
    Tuesday, November 30, 2010 10:21 PM
  • how about  [AssemblyInitialize()] ? i use this method to add some test record to database. This approach can give me opportunity to split Data Access Layer and database. So I can test DAL classes without reaching to database itself. Is this a good way to go for?
    Monday, December 6, 2010 1:49 PM