Preparing test data for Automated Testing



  • So i've ran into a bit of a problem, I have a set of automated tests created for a site, were looking into really moving towards a CI based system for testing (Running the test suite after pushing features/etc...)

    However in this site and a lot of my automated tests involve creating records in the database (through the site). Which is fine and everything, I mean these are features I WANT to exercise.

    However....I can't just keep running the tests on the same database, the test data would just keep filling up! I could Delete the records from the database using the Websites ability to delete things, but that's insanely brittle and relies me going to an exact "Delete" button on a list to do that....seems like a poor idea. Plus if that automated test doesn't work, we get extra data...not good.

    So how do I get around this?

    Most of our sites are rails sites, i've looked into using Factories, but that doesn't really seem like it replicates doing a true integration test. So I could just recreate a database and reseed with test data before every automation suite run, or is this bad practice?

    How do I get a good system running for test data in this scenario + moving forward?



  • Think about the three steps for every test

    • setup
    • execute
    • teardown

    Ideally this is done for every test and the database strategy used between tests is truncation.

    Frequently this is deemed not possible and seed data is used which would slow down the tests too much if deleted each time. In those cases each test should use the database strategy of rolling back the transactions it create.

    For rails look at this section in spec/rails_helper.rb
    The following is an example from our environment.

    config.prepend_before(:each) do |example|
      if truncate?(example)
        DatabaseCleaner[:active_record].strategy = :truncation, {pre_count: true, reset_ids: false}
      else
        DatabaseCleaner[:active_record].strategy = :transaction
      end 
      DatabaseCleaner.start
    end 
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2