MSDN Magazine Launch Issue - February 15, 2008 - (Page 67) unit test by selecting New test from the Test menu, and then select the Database unit test template. Once the wizard has completed, you will see a dialog prompting you to configure the project—cancel this for now (I’ll come back to it later). In the solution explorer a new file called DatabaseUnitTest1 will be created and the associated designer will be opened so you can create the first unit test. In Figure 2 there are three interesting areas. First, the top-left dropdown allows you to select between common scripts and tests defined in the class. When a new test class is created, a default test called DatabaseTest1 is added to the class; you can rename this default test using the Rename button and add additional tests using the button with the green plus symbol. The dropdown to the left of that button allows you to switch between scripts. If Common scripts has been selected in the dropdown on the left, the test initialization/ Figure 2 New Unit Test cleanup scripts that are run before and afis used to execute the unit test. It should represent the configura- ter every test are available to be edited. On the other hand, if a Test tion of the application that uses the database. Privileged context is selected, the pre/test/post scripts are available to be edited. is used to verify unit tests as well as perform all other modificaThird, the Test Conditions panel provides you access to the test tion operations (including deployment and data generation). The conditions that operate on the result of the T-SQL script execution to definitions of these two contexts are stored in the accompanying verify that the results are what you expected. The dropdown is enabled sample test project’s app.config file, in a special section defined for once you click on the hyperlink “Click here to create.” This dropdown database unit testing. provides access to the test conditions registered on your machine. As you can see from the UI, there is Managing T-SQL Scripts a consistent pattern for all scripts. EvFrom a code perspective, database erywhere you can define a script, you unit testing generates C# or Visual can also define one or more test conBasic .NET code that targets the unitditions that can be used to verify that testing framework in Visual Studio. In the script executed as you expected. the unit-testing framework, a class is Verification of tests is crucial when used as a container to hold tests (each writing unit-test code. It is not enough test is defined as a method). There are a to just execute a piece of code; you also number of opportunities to add scripts need to verify that the execution did in the unit-testing designer to support what you expected. the scripting features I’ve described. All At this point, you are probably scripts, other than the test script, are exwondering how the test knows which ecuted using the privileged context bedatabase to connect to and what the cause they are meant to set up or verify privileged context I mentioned is all the test environment. A script will be about. Well, to answer these questions executed one or more times dependyou need to go back to that dialog I had ing on where it is added, so let’s look in asked you to cancel earlier. To get to the more detail at the scripts and how they dialog, select the Test Project and go are executed (see Figure 1). to Test | Database Test Configuration Now let’s see how to create, access, (see Figure 3). and modify scripts in the database unitIn the first dropdown option, you testing designer. First, create a database Figure 3 Configuring the Unit Test select the connection string that will Database Unit Testing launch2008 67
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.