Data-driven Unit Test

I'm using SQL 2005, Visual Studio Team Suite with CTP7 and would like to make a Data-driven Database Unit Test which tests a stored procedure.

Anyone got any sample code

I've managed to link the Database Unit Test to a Data Source and Data Table, so it now runs my SQL 100 times (there are 100 rows in the Data Table), but don't know how to link the data from the table to the SQL script.

Or am I getting the wrong end of the stick here, ie can you only make normal Unit Tests data-driven, so they have to be written in C# / VB.net

I know a bit of C# if that's required..

Thanks


wBob



Answer this question

Data-driven Unit Test

  • Giantsonic

    Hi Jeff,

    thanks for your reply! First of all, I am working with a Database Unit Test (DUT), so my script is in SQL. As you know, DataDude writes C# (in this instance) in the background which actually runs the test. The DUT allows you to configure a datasource and connection, as you can for a normal Unit Test, so I did this. What I was looking at was connecting the datasource to the SQL. I have now had some success in this:

    It can be done using a SQL script eg

    DUT003.sql

    DECLARE @result INT

    EXEC @result = dbo.uspTest '@Filename'

    SELECT @result

    GO

    Note, the parameter is there as a string. Now within DUT003.cs, the DataSource attribute has appeared:


    [DataSource("System.Data.SqlClient", "Data Source=etc;Initial Catalog=LoadTest;Integrated Security=True", "DUT003", DataAccessMethod.Sequential), TestMethod()]

    ...

    // Get the parameters from the data table
    String fileName = TestContext.DataRow["FilePath"].ToString();

    // Replace the parameters
    testActions.TestAction.SqlScript = testActions.TestAction.SqlScript.Replace("@Filename", fileName);

    // Execute the test
    ExecutionResult[] testResults = TestService.Execute(this.ExecutionContext, this.PrivilegedContext, testActions.TestAction);

    We now have data-driven Database Unit Tests, which I am very excited about! I'm currently working on using the sqlParameter argument of the TestService.Execute method with SqlParameters. I've had some success in this too, but still have some work to do.

    wBob


  • thelonesoldier

    Did you define the connection and the data table through the Unit Test properties or did you do all of your work in T-SQL (if so, could you provide the code that you are using)

    We can certainly provide you with a sample of a data-driven test through C#/VB, but if are gathering the data for the test and iterating through the data all in the T-SQL test code, then you would likely need to set test conditions for each of the result sets that would be returned from the test, which, I presume, would be 1 per iteration. It's probably cleaner for you to do the data-driven test in the C#/VB view of the test as you'll then be able to work with a single result set.

    Would you like a sample of a data-driven test through C# or VB

    Thanks



  • boulder_bum

    Actually you do not need to replace the variable this way - it will be error prone depending on what the data type of the variable is actually supposed to be. A better way is to define a DbParameter and pass this in to the Execute method. Something like this:

    // Execute the test script

    //

    DbParameter fileNameParam = this.ExecutionContext.Provider.CreateParameter();

    fileNameParam.DbType = DbType.String;

    fileNameParam.ParameterName = "@Filename";

    fileNameParam.Value = TestContext.DataRow["FilePath"].ToString();

    System.Diagnostics.Trace.WriteLineIf(

    (testActions.TestAction != null), "Executing test script...");

    ExecutionResult[] testResults = TestService.Execute(

    this.ExecutionContext, this.PrivilegedContext, testActions.TestAction,

    fileNameParam);



  • Data-driven Unit Test