A testing framework for Microsoft SQL-Server


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Este trabalho enquadra-se no grupo de trabalhos “Teste por camadas de aplicações empresariais”
  • A testing framework for Microsoft SQL-Server

    1. 1. A testing framework for Microsoft SQL-Server Jorge Almeida
    2. 2. Summary <ul><li>Database Testing in an RDBMS Unit test framework for databases </li></ul><ul><li>Database manual testing </li></ul><ul><li>Database automatized tests </li></ul><ul><li>Database testing tools </li></ul><ul><li>TSQLUnit framework </li></ul><ul><li>How to write T-SQL unit tests </li></ul><ul><li>Conclusions </li></ul><ul><li>References </li></ul>
    3. 3. Database Testing in an RDBMS
    4. 4. Database Testing <ul><li>Categories of database tests </li></ul><ul><ul><li>Interface tests (validate what is going into, out of, and mapped) </li></ul></ul><ul><ul><li>Internal database tests (validate the internal structure, behavior and contents) </li></ul></ul><ul><li>Internal database tests should validate: </li></ul><ul><ul><li>Database methods (e.g. stored procedures, functions, and triggers) </li></ul></ul><ul><ul><li>Existence of database schema elements (tables, procedures, ...) </li></ul></ul><ul><ul><li>View definitions </li></ul></ul><ul><ul><li>Referential integrity rules </li></ul></ul><ul><ul><li>Default values for a column </li></ul></ul><ul><ul><li>Data invariants for a single column </li></ul></ul><ul><ul><li>Data invariants involving several columns </li></ul></ul>
    5. 5. Database Manual Testing <ul><li>Combination of PRINT statements and ad-hoc tests with the SQL Query Analyzer </li></ul><ul><li>Trace/debug tools like SQL Profiler </li></ul><ul><li>Human judgment is required to analyze the results </li></ul><ul><li>The tests can not easily be repeated again </li></ul>
    6. 6. Database Automatized Tests <ul><li>Can be repeated over and over again </li></ul><ul><li>Improve the quality of the code (refactoring) </li></ul><ul><li>Serve the purpose of documentation </li></ul><ul><li>May improve the design </li></ul>
    7. 7. Database testing tools <ul><li>Category of database testing (interface and internal) </li></ul><ul><li>Support the language of developing </li></ul><ul><li>Help putting the database into a known state </li></ul>
    8. 8. Some database testing tools <ul><li>Data Factory </li></ul><ul><li>Datatect </li></ul><ul><li>DTM Data Generator </li></ul><ul><li>Turbo Data </li></ul>Developers need test data against which to validate their systems. Test data generators can be particularly useful when need large amounts of data, for stress and load testing Test data generator <ul><li>Empirix </li></ul><ul><li>Mercury Interactive </li></ul><ul><li>RadView </li></ul><ul><li>Rational Suite Test Studio </li></ul><ul><li>Web Performance </li></ul>Tools simulate high usage loads on database, enabling to determine whether the system’s architecture will stand up to true production needs Testing tools for load testing <ul><li>DBUnit </li></ul><ul><li>NDbUnit </li></ul><ul><li>OUnit for Oracle (being replaced soon by Qute ) </li></ul><ul><li>SQLUnit </li></ul><ul><li>TSQLUnit </li></ul><ul><li>Visual Studio Team Edition for Database Professionals </li></ul>Tools which enable regression test Unit testing tools Examples Description Category
    9. 9. TSQLUnit <ul><li>A testing framework for Microsoft SQL-Server </li></ul><ul><li>It follows the tradition of the &quot;xUnit&quot; framework </li></ul><ul><li>TSQLUnit is open source </li></ul><ul><li>Developed by Henrik Ekelund </li></ul><ul><li>Unit testing for stored procedures, functions, triggers and views </li></ul><ul><li>Available from http://sourceforge.net/projects/tsqlunit </li></ul><ul><li>Version 0.9 (December 15, 2002) </li></ul>
    10. 10. TSQLUnit Install <ul><li>Download file from http://tsqlunit.sourceforge.net/tsqlunit_download.htm </li></ul><ul><li>Unzip the file </li></ul><ul><li>Connect to database as dbo using the SQL Query Analyzer, execute the tsqlunit.sql file </li></ul>
    11. 11. Basic principles of database testing <ul><li>Database with good test data </li></ul><ul><li>Shouldn’t developing against a production database </li></ul><ul><li>Test one feature only per test procedure </li></ul><ul><li>Be careful of how T-SQL comparison operators work </li></ul><ul><li>When it is difficult to write tests change the design or refactor the system </li></ul>
    12. 12. How to write T-SQL unit tests <ul><li>Create a stored procedure with a name that starts with ut and underscore (e.g. ut_testSomething ) </li></ul><ul><li>Code a test, then call tsu_failure if test fails </li></ul><ul><li>Execute tsu_runTests </li></ul>
    13. 13. Test example (1) <ul><li>CREATE PROCEDURE ut_CapitalizeOneSentence AS </li></ul><ul><li>BEGIN </li></ul><ul><li>DECLARE @outStr VARCHAR (500) </li></ul><ul><li>EXECUTE capitalize ‘ a string ’ , @outStr OUTPUT </li></ul><ul><li>IF ASCII ( LEFT (@outStr,1)) <> ASCII ( ‘ A ’ ) OR @outStr IS NULL </li></ul><ul><li>EXECUTE tsu_failure ‘ Capitalize should make the first character uppercase ’ </li></ul><ul><li>END </li></ul>CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
    14. 14. Test example (2) <ul><li>CREATE PROCEDURE ut_CapitalizeNullSentence AS </li></ul><ul><li>BEGIN </li></ul><ul><li>DECLARE @outStr VARCHAR (500) </li></ul><ul><li>EXECUTE capitalize NULL , @outStr OUTPUT </li></ul><ul><li>IF @outStr IS NOT NULL </li></ul><ul><li>EXECUTE tsu_failure ‘ Capitalize should not return any value ’ </li></ul><ul><li>END </li></ul>CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
    15. 15. Test example (3) <ul><li>CREATE PROCEDURE ut_CapitalizeEmptySentence AS </li></ul><ul><li>BEGIN </li></ul><ul><li>DECLARE @outStr VARCHAR (500) </li></ul><ul><li>EXECUTE capitalize ‘’ , @outStr OUTPUT </li></ul><ul><li>IF @outStr<>’’ </li></ul><ul><li>EXECUTE tsu_failure ‘ Capitalize should return the same value ’ </li></ul><ul><li>END </li></ul>CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
    16. 16. Test Results
    17. 17. Improving tests <ul><li>Organize tests in classes ( suites ) (e.g. rename ut_CapitalizeEmptySentence as ut_Capitalize_EmptySentence then run the store procedure tsu_runTest ‘Capitalize’ ) </li></ul><ul><li>Create a unique procedure for test setup (e.g. ut_Capitalize_SetUp) </li></ul><ul><li>Create a unique procedure for test “teardown” (e.g. ut_Capitalize_TearDown) </li></ul>
    18. 18. Conclusions <ul><li>Good unit testing tool for Microsoft SQL-Server stored procedures </li></ul><ul><li>Regression test suite </li></ul><ul><li>Some improvements needed (e.g. execution time of a single test) </li></ul><ul><li>Be careful with error treatment in test store procedures </li></ul>
    19. 19. References <ul><li>http://sourceforge.net/projects/tsqlunit </li></ul><ul><li>http://www.tassq.org </li></ul><ul><li>http://www.agile.org/essays/databaseTesting.html </li></ul><ul><li>http://www.ambysoft.com/books/refactoringDatabases.html </li></ul><ul><li>http:// www.ambysoft.com/books/agileDatabaseTechniques.html </li></ul>
    20. 20. <ul><li>Questions ? </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.