What is the equivalent of the DataSource attribute for mstest in .NET Core?

Is DataSource attribute supported in .NET Core projects of MSTest? If not, what is the alternative?

For more information, visit: https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2019

Note: In .NET Core, the DataSource attribute is not supported. Trying to use it in a .NET Core or UWP unit test project will result in an error message like "'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?)".

Answer №1

September 7th, 2020 - No solution yet

An unresolved issue-233 is currently being discussed without a fix timeline provided. While waiting for a resolution, there are several potential workarounds:

Utilize DynamicDateAttribute (CSV Sample)

private static string[] ExtractCsv(string data)
{
    var csvExtractor = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
    var list = new List<string>();
    foreach (Match match in csvExtractor.Matches(data))
    {
        string value = match.Value;
        if (value.Length == 0)
        {
            list.Add(string.Empty);
        }

        list.Add(value.TrimStart(','));
    }
    return list.ToArray();
}

private static IEnumerable<string[]> RetrieveData()
{
    IEnumerable<string> rows = System.IO.File.ReadAllLines(@"Resources\NameAddressCityStateZip.csv").Skip(1);
    foreach (string row in rows)
    {
        yield return ExtractCsv(row);
    }
}

[TestMethod]
[DynamicData(nameof(RetrieveData), DynamicDataSourceType.Method)]
public void TestMethod1(string input, string expected)
{
    // Arrange
    var parser = _serviceProvider.GetService<Parser>();

    // Act
    string actual = parser.MultiParser(input, ModeType.NameAddressCityStateZipCountry).ToString();

    // Assert
    Assert.AreEqual(expected, actual);
}

Please note that these methods may have limitations.

Custom Code Implementation

You can explore how to adapt the code yourself as demonstrated in this comment here

Await Official Fix

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for extracting HTML content using the JavaScript executor?

import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; public class WebDriverExample { public static void main(String[] args) { System.setProperty("webdriver.c ...

How can I use AJAX to read a CSV file uploaded by a client in C#?

My current project involves the scenario where a user needs to choose a CSV file from their local system. It is then necessary for me to read the contents of this file and display them on a JQGrid. The catch? This all needs to work smoothly in Internet E ...

Swapping out the main view for the partial view

Recently, I wrote some jQuery code to fetch data from an action by passing in a dashID. The expected result was to receive HTML containing the relevant information. Unfortunately, my jQuery script is not returning the desired data. Below is the JavaScript ...

Disabling Notifications using Selenium for Microsoft Edge Browser Driver

My Selenium script opening the Edge WebDriver is encountering issues with notifications popups that interfere with clicking on buttons on websites. I suspect these notifications are overlaying the website content, making it difficult for the code to locate ...

Using Selenium to run Chrome in headless mode

Can anyone help me figure out how to launch Chrome in headless mode using Selenium? I attempted the following code: chromeOptions = Options() chromeOptions.add_argument("headless") self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chr ...

How can I use Xpath to find the most efficient way to locate this element?

Within our DOM, we have a series of buttons: /button /button /button /button /button We are tasked with identifying an element using Xpath, but here's the challenge: we are not allowed to use indexes or store elements in a list for access. This quest ...

Managing session timeouts during an AJAX call triggered by the Kendo ComboBox read action

Looking for a solution to handle session timeout specifically with kendo combo-box implementation? Here is the HTML code snippet for my kendo combobox: @(Html.Kendo().ComboBoxFor(model => model.PropertyName) .AutoBind(tru ...

How I tackle web automation in a unique way

I recently started using webdriver and have developed a specific approach for my workflow: public class PageObjectRepresentationClass { protected WebDriver driver; public PageObjectRepresentationClass(WebDriver driver){ this.driver = driver; ...

The CSS selector used in Selenium IDE may vary from the one copied directly from the browser's development tools

When it comes to selecting CSS selectors, Selenium IDE may sometimes generate a different code than what you see in the developer tools. To illustrate this, consider the following example page: For instance, the price selector recorded by Selenium IDE is ...

Having trouble interacting with the 'button' in selenium webdriver using Java

In this code snippet, I am trying to pinpoint the location of an href button: <a class="btn grey-edit" data-original-title="Login" data-placement="top" data-toggle="tooltip" href="/users/userlogin/3"> <i class="fa fa-sign-in" aria-hidden="true"& ...

The best way to organize and position a Label and TextBox in C#

I need help styling the aspx file. The Label and TextBox elements are not aligned properly. I want the Label and TextBox to be positioned side by side with appropriate spacing. <div class="col-md-12"> <p> ...

What is the process for setting up geckodriver to have a trace log level using firefox_options?

When attempting to execute selenium on a VPS for running tests in Python, I encountered an issue. Here's the Python code snippet: from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() options.log.l ...

Receiving Json data from ASP.NET CORE 2.1 using JsonResult

Code snippet for a ASP.NET Core 2.0 controller: [Produces("application/json")] [Route("api/[controller]")] [ApiController] public class GraficResourcesApiController : ControllerBase { private readonly ApplicationDbContext _context; public Gra ...

Tips for implementing Regular Expression Validations with Selenium

I am working on a project that involves checking for special characters in a text box. Specifically, the text box should not accept around 10 different special characters. When invalid special characters are entered, a red asterisk * mark is displayed by t ...

Utilizing Selenium WebDriver in Eclipse: Is there a way to establish a condition for a Span-Element?

Currently, I am developing test cases for an online shop using Selenium in Eclipse. My goal is to assess the functionality of the website. Specifically, I am looking to automate a test that checks if the number "785" displayed on the site is over 100. Giv ...

Navigate up to the ancestor element before moving back to the previous two sibling elements using Xpath

Here is an example of HTML code: <span class="state"> <label class="state button start" ...>"Start"</label> </span> <span class="name tracker_markup"> <span class="labels pre ...

How can one prevent a TimeoutException error in Selenium when making multiple consecutive requests?

Automation of search on a public information site using Python3 and selenium is my goal. This involves entering a person's name, selecting different spelling variations for that name, accessing a page with the list of lawsuits found, and then viewing ...

Ways to eliminate space within a list?

I am currently engaged in a project where I am extracting data from Indeed search results. At the moment, when I print the data I find, it appears with a space before the semicolon. Currently, my data prints like this: "Item 1 ; Item 2" I wan ...

Using Selenium C# Webdriver to ensure completion of all actions before closing the session

In my project, I am utilizing Selenium Web Driver version 2.48.2. The issue I am facing involves the need to ensure that the final URL is completely loaded before capturing a screenshot and closing the browser and driver. During debugging, all of my meth ...

Dealing with outdated element references when using Selenium and Java

While working with Selenium in Java, I often encounter errors related to stale element references. The web application being tested is built using AngularJS 2.0. Occasionally, utilizing explicit waits can resolve this issue, but not always. Is there a wa ...