Geeks With Blogs


View √Čtienne Tremblay's profile on LinkedIn
ET's VS and TFS World Fascinating tidbits about VS and TFS and .NET (well I hope...)

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
            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,


Posted on Thursday, September 22, 2005 8:26 PM Visual Studio Team System | Back to top

Comments on this post: Multiple input for Unit Test Case

# re: Multiple input for Unit Test Case
Requesting Gravatar...
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?
Left by Eric Hammersley on Sep 23, 2005 9:22 AM

# re: Multiple input for Unit Test Case
Requesting Gravatar...
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?


Left by ET on Sep 23, 2005 10:54 AM

# re: Multiple input for Unit Test Case
Requesting Gravatar...
How do I configure this DataTable programmatically as I'm reading from a file.
Left by vinayhabib on Nov 23, 2006 4:36 AM

# QA Qustion
Requesting Gravatar...
I have a web test that I am running on an automated loop, with 50 pass and 50 fail.

I'm trying to find out a way to report it line by line to a results report
Left by JJ on Aug 12, 2009 8:59 PM

# re: Multiple input for Unit Test Case
Requesting Gravatar...
When I've given multiple testcases and I'm executing this actual.asset() method individually for all the testcases individually. I'm getting the Result only for the last testcase. When the test fails in the middle It is not validating the remaining test cases.
I just want to execute multiple test cases for a method and see which case has failed
Left by Sunil on Aug 03, 2010 4:05 AM

Your comment:
 (will show your gravatar)

Copyright © Etienne Tremblay | Powered by: