What is the replacement for TestContext.DataRow[“M

2020-07-05 06:46发布

问题:

Using MSTest in a .Net Core Unit test project. I am attempting to use a csv datasource to provide the data for a test method.

Previously, I would use something like below in a .Net Framework test project:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"data.csv", "data#csv", DataAccessMethod.Sequential),
     DeploymentItem("data.csv"),
     TestMethod]
    public void ValuesController_Post()
    {
        _controller.Post(TestContext.DataRow["body"]);
        _valuesRepository.Verify(_ => _.Post(It.IsAny<string>()), Times.Once);
    }

The key here being the DataRow property found in TestContext. This doesn't appear to exist in the .Net Core version of the TestContext.

How would I go about doing this in .Net Core?

回答1:

Since moving to aspnet core, I've never been able to use the same [Datasource(...)] attribute to iterate through test data, my data-driven tests are always skipped.

Have you considered switching to another approach with [DataTestMethod] and [DynamicData] with a custom source that reads you file ?

Here's a good article on this :

https://www.meziantou.net/2018/02/05/mstest-v2-data-tests

Maybe another way would be to read the whole file at the begining of the test and then iterate through the dataset as One single unit test?

Hope this helps.