'TestContext' does not contain a definition for 'DataRow'

Viewed 4212

I have UWP project. Using VS Community 2017 I added a unit test to my solution (Add project-> Windows Universal -> Unit Test App (Windows Universal)),

I added reference to my project, I added .csv file with test data, I added DataSource Attribute

using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
 [TestClass]
 public class UnitTest1
 {
  private TestContext testContextInstance;
  public TestContext TestContext
  {
   get { return testContextInstance; }
   set { testContextInstance = value; }
  }

  [TestMethod()]
  [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", 
  @".\TestData.csv", "TestData#csv", DataAccessMethod.Sequential)]

  public void ParameterizedTest()
  {  
   int value = Convert.ToInt32(TestContext.DataRow["Row1"]); 

But still I can't use TestContext.DataRow

Error CS1061 'TestContext' does not contain a definition for 'DataRow' and no accessible extension method 'DataRow' accepting a first argument of type 'TestContext' could be found (are you missing a using directive or an assembly reference?)

Do I miss something?

3 Answers

My script is also the same as yours. Just put the below line of code in the .csproj file of your project you should be good:

<TargetFramework>net4.8</TargetFramework>

Previously I was using net5.0 but when I started using 4.8 I was able to resolve the issue. I hope it helps. Cheers

If you are still not able to get it please take a look at this repo: https://github.com/manu1rock/RestSharpDemo

Above repo will help you.

just use testContextInstance.DataRow["Row1"] and optionally add toString at its end like so testContextInstance.DataRow["Row1"].ToString()

you are making a common mistake that you are trying to use TextContext.DataRow where TextContext is a class which has no static property named DataRow so in order to use it you need to create an instance of the class which you already have called testContextInstance.

Also change the property name from TextContent to TextContextInstance like following

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

because you should never name a property exactly like its type ( class ) when you make this change now you can even use the property directly as you were trying before.

int value = Convert.ToInt32(TestContextInstance.DataRow["Row1"]); 
Related