简体   繁体   中英

C# IEnumerable query

I have a question regarding this c# code. I can follow the last part using selenium where it is entering data into the text field. But I have problem in understanding how this function works private IEnumerable GetXlData() and how the valus are being takenup by EnterData(string firstName, string lastName) this test.

I can follow that data1 and data2 hold the first and second column values but how this alue being returned and being used by the EnterData(string firstName, string lastName) test.

namespace XYZ
{
    [TestFixture]
    public class readXl
    {
        ReadXLS xl = new ReadXLS("TestData.xls", "Sheet1");

        private IEnumerable<string[]> GetXlData()
        {
            foreach (ExcelData e in xl.TestData)
            {
                string data1 = e["firstName"];
                string data2 = e["lastName"];

                yield return new[] { data1, data2};
            }
        }

        [Test, TestCaseSource("GetXlData")]
        public void EnterData(string firstName, string lastName)
        {
            driver.FindElement(By.Name("FirstName")).SendKeys(firstName);
            driver.FindElement(By.Name("LastName")).SendKeys(lastName);
        }
    }
}

See documentation for TestCaseSource . NUnit is essentially doing this:

foreach (var x in GetXlData())
{
    EnterData(x[0], x[1]);
}

The TestCaseSource attribute in NUnit is used in so-called data-driven testing. The mechanism works like this:

  • Its input (ie what is given to the attribute as method name) must be an enumerator method that returns object ( string[] in your case, but it could be an untyped object as well). This object must match the signature of the decorated test method ( Enterdata() ).
  • The decorated test method then is executed as many times in a row as the enumerator method returns values, each time with the values that were provided by the enumerator method (therefore these methods are also called 'data factories').

Data-driven tests of this kind are very useful to throw a bunch of different data to a single algorithm, without needing to write the same test code again and again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM