Question

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);
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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

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

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.

this one is working with me

[TestMethod]
    [TestCategory("Category 4")]
    [DataSource("MyExcelDataSource")]
    [DeploymentItem("UnitTestProject\\DBConnections.xlsx")]
    public void testwithexceldata()
    {
        //http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testcontext.datarow.aspx

        int x = TestContext.DataRow.Table.Rows.Count;
        int Balance = Convert.ToInt32(TestContext.DataRow["Balance"]);
        int Amount = Convert.ToInt32(TestContext.DataRow["Amount"]);
        Assert.AreEqual(Balance, Amount);

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top