2016-07-18 1 views
0

Visual Studio의 코딩 된 UI 테스트에 사용자 목록이 포함 된 CSV가 있습니다 (두 명의 사용자, 한 명의 관리자, 한 명의 일반 사용자).C# Data Driven Testing Logic

필자는 CSV (즉, 두 사용자)의 모든 레코드에 대해 실행하고 싶은 몇 가지 테스트를 실시하고 있습니다. 그러나 몇 가지 테스트의 경우 CSV의 레코드 중 하나에 대해서만 실행하려고합니다.

내 테스트 방법/데이터 소스에서이를 구성하는 방법이 있습니까?

데이터 드리븐 테스트를 작성한 것은 이번이 처음이므로 newb 질문에 사과드립니다.

+0

이것은 셀레늄과 전혀 관련이없는 것 같습니다. 필요한 것은 단일 레코드 만 읽는 ReadCSV를 구현하는 것입니다. Fixture/Category 레벨 이상의 Test Setup에서 ReadCSV를 호출하면 여러 번 호출되지 않습니다. – kurakura88

답변

0

이것은 매우 간단합니다. 모든 레코드에 대해 테스트 사례를 실행하려면 CSV의 모든 레코드를 반복하고 각 레코드에서 텍스트를 실행합니다. 이름이나 역할별로 특정 사용자를 식별하기위한 아래 코드가 있습니다. Record 클래스를 확장하여 필요한 모든 특성을 갖추어야합니다.

namespace Selenium 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      IWebDriver Driver = new FirefoxDriver(); 
      Driver.Manage().Window.Maximize(); 
      Driver.Navigate().GoToUrl("<some url>"); 
      List<Record> records = readCSV(); 

      // this is for using all records 
      foreach (Record record in records) 
      { 
       // execute the test case using the current record 
       // do something with record.name and record.role 
      } 

      // this is for using only admins 
      foreach (Record record in records) 
      { 
       if (record.role == Record.Role.admin) 
       // if (Record.Role.name == "John Smith") // use something like this for a particular user name 
       { 
        // execute the test case using the current record 
        // do something with record.name and record.role 
       } 
      } 
     } 

     static List<Record> readCSV() 
     { 
      // START loop over each line in the CSV 
      List<Record> records = new List<Record>(); 
      string name = ""; // code that pull the user's name from the CSV 
      string role = ""; // code that pull the user's role from the CSV 
      records.Add(new Record(name, role)); 
      // END loop over each line in the CSV 

      return records; 
     } 
    } 

    class Record 
    { 
     public string name; 
     public Role role; 

     public Record(string name, string role) 
     { 
      this.name = name; 
      this.role = (Role)Enum.Parse(typeof(Role), role); 
     } 

     public enum Role 
     { 
      admin, regular 
     }; 
    } 
} 
+0

고마워요.하지만 TestCategory와 TestMethod의 맥락에서 말이죠. 지금 현재 Microsoft는 csv [TestCategory ("Web")]의 모든 레코드를 반복합니다. [DataSource ("Microsoft.VisualStudio.TestTools.DataSource.CSV", "C : \\ SourceControl \\\\공개 무효 ValidateLinks_CINavBar_Admin() {// 논리} – KillerSmalls

+0

아마도 질문을 명확히하고 ... 위의 주석에 코드를 추가해야합니다. (예 : Users.csv ","Users # csv ", DataAccessMethod.Sequential), TestMethod 질문 자체. – JeffC