简体   繁体   中英

Selenium and MSTest: [DataRow] at a TestClass level?

Many thanks in advance!

I am creating a Selenium framework (C#, MSTest) and got stuck, trying to make the tests data-driven. What I am trying to achieve: I need to support logging in the application under test by different types of users (lets say I have 4 of them). As all these users have different permissions I was planning to put test methods into separate test classes based on the users who perform them.

**Example: ** For instance, I have several tests that can be performed only by user 1 and user 2 and I also have several tests that can only be performed by user 3 and user 4. So, the first bunch of tests goes into the test class 1 and the second bunch of tests - into the test class 2.

Currently I am using DataRows for each of the methods in a test class. And it works, however it seems a ripetitive and inefficient solution. In this case I do login for every single test for the same user. What I want is that in the class1 user1 logins once, then does all the tests in the class without repeating the login part in each test method. Then user2 logins once and does all the tests. .. The same for the other test classes.

[TestClass1]
public class TestClass1
{
   [DataTestMethod1]
   [DataRow("UserType1", "Selenium_pssw1")]
   [DataRow("UserType2", "Selenium_pssw2")]
   public void Test1 (string usernameString, string passwordString)
   {
   //do smth and assert smth
   }

   [DataTestMethod2]
   [DataRow("UserType1", "Selenium_pssw1")]
   [DataRow("UserType2", "Selenium_pssw2")]
   public void Test2 (string usernameString, string passwordString)
   {
   //do smth and assert smth
   }
}

TestClass2 would also contain several test methods but for different user types, say only for UserType1 and UserType4.

Is there a possibility to use a data source at the level of a test class and not single test method?

PS: I am sorry for my English and naivety on programming. I am just starting this path:) And I DID googled a lot trying to find an answer online. Perhaps, I just don't know what to ask...

Have a look at annotations of MSTest. You could use [ClassInitialize] annotation to login and then run all the tests. Also you will need a suitable annotation for the method that closes the webdriver so that it would not close after each test but only after all the tests in the class have been run.

Also get familiar with inheritance. You can setup a BaseTest class which does the initialization and teardown for the driver with specific annotations.

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