Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data driven testing in MSTest - problem with TestContext.DataRow

I'm having essentially the same problem as the question linked below, but I cannot seem to get it to work. I'm getting "cannot apply indexing [] to an expression of type System.Data.DataRow". As far as I can tell, I have implemented the solution correctly.

Problems with data driven testing in MSTest

[TestClass]
public class UnitTest1
{
    private TestContext testContextInstance;

    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }        

    private ServiceReference1.ProductCatalogClient client = new ServiceReference1.ProductCatalogClient("BasicHttpBinding_IProductCatalog");

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\CountList.csv", "CountList#csv", DataAccessMethod.Sequential), DeploymentItem("..\\ServiceTest\\CountList.csv"), TestMethod]
    public void AreCountsCorrect()
    {
        int id = TestContext.DataRow["Id"] as int;
        int count = client.GetProductCount(id);
        Assert.IsTrue(count == TestContext.DataRow["Count"] as int);
    }
}
like image 468
bq1990 Avatar asked Jan 24 '11 21:01

bq1990


4 Answers

Add a reference to System.Data to the test project. No idea why that would not be automatically included since DataRow is used for data driven tests.

like image 115
bq1990 Avatar answered Nov 14 '22 01:11

bq1990


You should add Reference to your test project to fix this issue. I don't know why VS2013 doesn't add it automatically.

right click your test project, Add->Reference...->Assemblies->Framework->System.Data, check it.

Done!

enter image description here

like image 34
karl li Avatar answered Nov 14 '22 01:11

karl li


I had the same problem as you with the this.TestContext.DataRow["PathFile_Original"], what I was doing wrong was so simple, I didn't add the reference to System.Data, that was all.

It's quite easy to fix, be sure that you already have library.

Cheers

like image 1
L. Vicente Mangas Avatar answered Nov 14 '22 01:11

L. Vicente Mangas


To fix your issue, as others have said, you should add a reference to System.Data in your project.

If you are still having an issue after that reference has been added, you may need to restart Visual Studio, and remove any redundant references or using headers.

like image 1
Craig Brown Avatar answered Nov 14 '22 02:11

Craig Brown