Chris Riesbeck
EECS 394 Software Project Management
Database Testing
Thursday, May 12, 2011
EECS 394 Software Project Management Chris Riesbeck Database - - PowerPoint PPT Presentation
EECS 394 Software Project Management Chris Riesbeck Database Testing Thursday, May 12, 2011 Database testing problems Real database changes constantly so tests looking for specific results may fail Real database should not have
Chris Riesbeck
Thursday, May 12, 2011
Real database changes constantly
so tests looking for specific results may fail
Real database should not have dummy test data
confuses users and internal queries
Real database should not be changed by tests for
Test databases have to be cloned for each test
2
Thursday, May 12, 2011
Mock objects
see BDD and Mock Objects slides great for unit testing doesn't test db code actually works!
Database fixtures
frameworks for loading static test data into a new test
some support loading data from CSV, XML and other
use for integration testing
3
Thursday, May 12, 2011
4
<?php // app/tests/fixtures/post_fixture.php class PostFixture extends CakeTestFixture { var $name = 'Post'; var $fields = array( 'id' => array('type' => 'integer', 'key' => 'primary'), 'title' => array('type' => 'string', 'length' => 50, 'null' => false), 'body' => 'text', 'created' => 'datetime' ); var $records = array( array ('id' => 1, 'title' => 'First Post', 'body' => 'First Post Body', 'created' => '2007-03-18 10:39:23'), array ('id' => 2, 'title' => 'Second Post', 'body' => 'Second Post Body', 'created' => '2007-03-18 10:41:23'), array ('id' => 3, 'title' => 'Third Post', 'body' => 'Third Post Body', 'created' => '2007-03-18 10:43:23') ); } ?> the db schema the test data
Thursday, May 12, 2011
5
<?php // app/tests/cases/models/post.test.php class PostTestCase extends CakeTestCase { var $fixtures = array( 'app.post' ); function testGetRecentShouldReturnLastTwoPosts() { $this->Post =& ClassRegistry::init('Post'); $expected = array( array('Post' => array ('id' => 3, 'title' => 'Third Post', 'body' => 'Third Post Body', 'created' => '2007-03-18 10:43:23')), array('Post' => array ('id' => 2, 'title' => 'Second Post', 'body' => 'Second Post Body', 'created' => '2007-03-18 10:41:23')) ); $this->assertEqual($this->Post->getRecent(2), $expected); } } ?> tell CakePHP to use the test Post fixture magic CakePHP to use test suite db
Thursday, May 12, 2011
6
<?php // app/models/post.php class Post extends AppModel { var $name = 'Post'; function getRecent($count) { $params = array( 'limit' => $count, 'order'=>array('Post.created DESC') ); return $this->find('all', $params); } } ?> normal db code
Thursday, May 12, 2011
7
Ruby
http://ar.rubyonrails.org/classes/Fixtures.html
PHP
http://book.cakephp.org/view/1201/Preparing-test-
Django
http://docs.djangoproject.com/en/1.3/howto/initial-
Java
http://onjava.com/onjava/2004/01/21/dbunit.html
Thursday, May 12, 2011