Etienne's VSTS World

Fascinating tidbits about VSTS and .NET (well I hope...)

  Home  |   Contact  |   Syndication    |   Login
  154 Posts | 0 Stories | 94 Comments | 122 Trackbacks

News



View Étienne Tremblay's profile on LinkedIn

Tag Cloud


Archives

Post Categories

Image Galleries

My Friends

My Links

I was just reading Marc Lehmann post on Code Coverage and I agree with him that a single pass through your code will give you 100% coverage but it doesn't mean you extensively tested the boundaries of your code.

So with this in mind, I'll show you how to setup a link to a database where you can use a table to feed your unit test with all the test cases you need to fully test you code.  The unit testing framework of Team System lets you connect to a table and run the same test once for each row in the table.

First you need to connect you unit test to a database table, from the unit test view click on the test you want to connect and look in the property window.

Go to the Data Connection String property and click the … this will show the database connect box

Connect to your test data database this will add the following lines of code to your test (in VB.Net)

    <DataSource("System.Data.SqlClient", "Data Source=.;Initial Catalog=TestData;Integrated Security=True", "AddTestData", DataAccessMethod.Sequential)> <TestMethod() >  _
    Public Sub AddTest()

You now have a connection to the datatable.

Next you need to wire the data coming from that table to your unit test case here's how to do that (I'm showing a unit test method that tests the Add method in my class, obviously the Add method adds two integer and return the value).

        Dim target As MyClassLib.MyClassLib = New MyClassLib.MyClassLib

        Dim a As Integer = CType(TestContext.DataRow("a"), Integer)

        Dim b As Integer = CType(TestContext.DataRow("b"), Integer)

        Dim expected As Integer = CType(TestContext.DataRow("res"), Integer)

        Dim actual As Integer

        actual = target.Add(a, b)

        Assert.AreEqual(expected, actual, "MyClassLib.MyClassLib.Add did not return the expected value.")
    End Sub

So the most import piece of information here is the TestContext piece. The test harness create a property at the top of the test class here is a copy of it.

    Private testContextInstance As TestContext

    '''


    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

This TestContext hold a lot of properties but the one that interests us is the DataRow one, when we are connected to a datatable the DataRow property holds the current row.  The test harness will run the test for as many rows as there is in the table.  There are two type of data access method we can use, Sequential (all the row in the table in sequential order), and Random (All the row but picked randomly until they are all covered).

When you run the test, the test will pass of fail based on *all* the rows in the table so if one fails the test fails if they all pass then the test pass. Here is a result view of my little test with the following data in "TestData" table

a b res
3 3 6
4 5 9
3 5 8

We should have success and the result is here

So there you have it.   Run multiple input through your test so the code coverage will have more meaning.

Happy multipe input testing,

ET

posted on Thursday, September 22, 2005 8:26 PM

Feedback

# re: Multiple input for Unit Test Case 9/23/2005 9:22 AM Eric Hammersley
What I find interesting about this is that you lose your ability to print an error message from your assert. If any row fails the test will fail, fine, but it skips the error message from the assert. If it didn't do that you could at least have it pass out an ID field from your table so you would know the data that caused the error. Idea or workaround?

# re: Multiple input for Unit Test Case 9/23/2005 10:54 AM ET
Well I ran the test with a row of bad data and this is what came back

Passed 00:00:00.6479681 0
Failed 00:00:00.1589743 1 Assert.AreEqual failed. Expected:<8>, Actual:<9>. MyClassLib.MyClassLib.Add did not return the expected value.
Passed 00:00:00.0002324 2

So the test failed and i got the assert like I usualy get with hard coded test data. Does that help?

Cheers,

ET

# re: Multiple input for Unit Test Case 11/23/2006 4:36 AM vinayhabib
How do I configure this DataTable programmatically as I'm reading from a file.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 7 and type the answer here: