What is the replacement for TestContext.DataRow[“MyColumnName”]

允我心安 提交于 2019-12-21 09:18:32

问题


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.



来源:https://stackoverflow.com/questions/44953827/what-is-the-replacement-for-testcontext-datarowmycolumnname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!