BIO PRESENTATION T14 November 18, 2004 1:30 PM A UTOMATED D ATABASE T ESTING WITH NU NIT Alan Corwin Process Builder, Inc. International Conference On Software Testing Analysis & Review November 15-19, 2004 Anaheim, CA USA
Alan Corwin Alan Corwin has been a software developer, trainer, and course designer for nearly thirty years. He is president and founder of Process Builder, CIO of OptionBots, and teaches High Performance Data Integration in the University of Washington’s Advanced Web Development program.
Automated Database Testing with NUnit Planned Test-Driven Development 1
Planned? � Isn’t test-driven development planned? Yes. Although planning is often decried as a waste of time by the XP community, simply writing your tests before you write your code is a form of planning. It’s the next level of planning that few XPers are willing to take – planning to do a series of standard tests for an entire application. 2
Test Goals � Test the Generic Database Requirements � Provide a Framework for More Complex Tests � Support Rapid Development (Daily Deliverables!) � Support Test-Driven Development � Allow Tests to be run both by developers and testers. � Test engineers and developers should be able to quickly and easily add test cases as needed. � Eliminate unnecessary communication cycles. 3
Test Design Features � Tests are fully automated. � Tests are black box (ignorant of the underlying data structure) � Tests are infinitely expandable. � Tests remain valid even when the implementation changes. 4
Tools Required � VisualStudio.NET (Enterprise Architect Edition) � NUnit � XML Spy (Optional) 5
The Test Process Developer Builds the Test DataSet Classes � Developer Creates the Test Data File � The Test Data Files are placed in a common directory. � Developer constructs and executes the basic test classes. � Test Engineers develop test cases and add them to the test file. � Tests are continually added. � Each time the product is changed or a test case is added, all of the � tests are rerun. Developers run all tests when the code changes. � Test engineers run all tests when new test cases are added. � 6
Test Data � Create the Test Data Dataset � Create the Test Data Data File 7
What is the Purpose of the test DataSet? � To hold the values necessary to robustly test the Create, Read, Update, and Delete functions. � To provide a strongly typed DataSet to increase reliability and reduce coding. � To simply the generation and initial population of the test data file. 8
Why do you need a DataSet? � First, you don’t “need” a DataSet. They are just a good idea. � DataSets reduce the amount of code required to construct the tests. � DataSets increase the readability of code. � DataSets eliminate many kinds of errors in the test code itself. 9
Create the Test DataSet � Each DataSet is based on an underlying table, view, or stored procedure. � Start the DataSet Wizard by adding a new DataSet to your test project. � Drag the underlying object to the designer. � Switch to the XML view. � Modify the primary key field. � Duplicate the data fields. � Add “Update” to the end of each new field name. � Save. Demo Here! 10
What Just Happened? � The format for the test data file was defined. � An XSD (XML schema definition file) was just created. � A Strongly Typed DataSet class was generated (3000 or more lines of highly reliable code) – requires that Generate DataSet be turned on (it is by default). 11
Generate the Data File � Here’s where XML Spy comes in handy. � Open the Schema file you just created in XML Spy � Choose the ‘Generate Sample XML File’ command from the DTD/Schema menu. � When the dialog appears, select your options including the number of rows to generate, and click OK. � Save the file in your public TestData directory as an XML file. � Edit the data rows as needed. Demo Here! 12
The Test Data File � XML Format Means � Editable with a wide variety of tools (including Notepad) � Built to a known standard supported by every software vendor � Easy to transfer to any other data format � Easy to read and understand. � .NET-ready � For proper testing, you need both a green bar data file and a red bar data file. 13
NUnit Basics � What is NUnit? � NUnit Limitations � Obtaining and Installing the Product � Constructing A Simple Test � Running Tests 14
What Is NUnit? � A Public Domain Test Harness for .NET � A Set of Classes to Facilitate Test Development and Execution � A Command Line Interface � A GUI to Run the Tests from Windows � A Tool For Developer-Centered Testing � Developed as JUnit, Ported to .NET, then Rewritten in C# 15
NUnit Limitations � You can’t test exception propagation easily. � Hard Code Tests Unlike user interfaces, database and business objects should throw � Red Bar Tests rather than handle exceptions. � Can’t debug when you run from the UI (This was a much longer list until version 2.1 which corrected most of the problems.) 16
Obtaining and Installing NUnit � http://www.nunit.org � Go to the Downloads Page � Run the Executable (That’s all there is to installing the program. Note, however that each project only knows about the test harness when it contains a reference to the NUnit libraries.) 17
Constructing a Simple Test � Adding the Harness Reference � Creating a Test Class � Adding Test Fixture Decorations � Write the Test Logic 18
Adding the Harness Reference � Right-click the References icon for the test project in the Solution Explorer and select Add Reference. � When the Open dialogue appears, navigate to the NUnit Installation directory. � Select all of the DLLs and click Open. (I’m not sure they are all needed, but the documentation does not specify which are needed.) 19
Creating a Test Class � Add a new class to the project. � Import the NUnit Framework � Imports NUnit.Framework (VB.NET) � using NUnit.Framework; (C#) � Add a TestFixture Decoration to the class declaration. � <TestFixture()> (VB -- must be on the same line as the class declaration) � [TestFixture] (C# -- Must be on the declaration line or the line before) 20
Write the Test Logic The test logic is often in a � Get the Test Data different class than the test execution. � For Each Test Case That is the approach that � Create the Record we use because it makes debugging both the test � Verify the Record and the application easier. � Update the Record � Verify the Record � Delete the Record � Verify the Deletion 21
Running Tests � From the GUI � Green Bar Tests � Red Bar Tests � From the Code 22
Process Change Results � As a result of adding NUnit and the basic database test process demonstrated here, we were able to shrink delivery times significantly while testing our database applications hundreds of times more thoroughly than we could before. � To our credit and embarrassment, our prototypes are now more robust and bug-free than our finished applications used to be. (And our finished applications were always more robust than the majority of what you see.) 23
Current Deliverables for One Programmer for One Four Hour Work Period � One DataSet Class � One Test Class for Each Business Object Class � One Command Helper Class � One Test Data File containing at least 3 test � Five Stored Procedures cases � One Business Object Class � A Data Entry form for the for Individual Objects individual object class. � One Business Object Class � A List form for the DataSet for the DataSet Class 24
Summary � NUnit is free, flexible, and easy to use. � XML test data files are easy to create and use. � NUnit supports a simple, standard approach to database testing that addresses the needs of both the test team and the development team. � The result of this approach is an automated test suite that creates significant gains in quality and productivity. 25
Technical Appendix � Test Logic � Set Tests � Additional Tests � Standardizing Test Classes � Resources � Automation of Test Automation 26
Test Logic: Get the Data � Three Details Make getting the Data Easy � Our test data is in an XML file. � We have a strongly typed DataSet class that matches that XML file. � Microsoft provides the ReadXML method. � VB Dim myTestData As New MyTestDataSetName() myTestData.ReadXML( fullPathOfTestFile) � C# MyTestDataSetName myTestData = New MyTestDataSetName(); myTestData.ReadXML( fullPathOfTestFile); 27
Test Logic: Get Each Test Case � A Strongly Typed DataSet consists of Strongly Typed DataRows. � VB For Each rowInstance _ As BackSpreadsParameterSetsTestData.SampleBusinessObjectsRow _ in _testSet.Tables[0].Rows _testRow = rowInstance ‘Assign to class variable for common use. Next rowInstance � C# These Row classes were generated when the Test Data DataSet was created. foreach (BackSpreadsParameterSetsTestData.SampleBusinessObjectsRow rowInstance in _testSet.Tables[0].Rows) {_testRow = rowInstance;} // assign to a class variable for common use. 28
Recommend
More recommend