Most Complete MSTest Framework Tutorial Using .Net Core
In this tutorial, we will learn about MSTest framework – the default test framework that comes along with Visual Studio. We will also explore how to use MSTest for cross-browser testing and parallel execution of tests on a remote selenium grid using LambdaTest cloud platform. The topics covered in this tutorial are: 1. Introduction to MSTest V2 2. Installing MSTest V2 3. Creating an MSTest test project 4. Local Testing using MSTest 5. Parallel Testing using MSTest 6. Cross Browser Testing using MSTest on the Cloud Let’s dive in! 1. Introduction to MSTest V2 Microsoft's unit testing framework, also known as MSTest, is a built-in feature of Visual Studio. MSTest V2 supports parallel execution of tests and has cross-platform support. The advantages of using MSTest are: a) It’s free to use with Visual studio b) Provides rich set of attributes for defining the behavior of the test c) Supports data-driven testing d) Can be used in conjunction with other frameworks like NUnit and xUnit 2. Installing MSTest V2 MSTest is a part of Visual Studio, so you don’t need to install it separately. However, if you want to use the latest version of MSTest (i.e., MSTest V2), then you can follow these steps: a) Open Visual studio and go to Tools > Extensions and Updates b) In the extensions window, search for ‘MSTest V2’ and install it c) Once installed, restart your Visual studio 3. Creating an MSTest test project To create a new MSTest test project, follow these steps: a) Right-click on the solution in Solution Explorer b) Choose Add > New Project > Installed > Visual C# > Test > choose MSTest Test Project (.NET Core) c) In the Name box, enter the name and then select OK 4. Local Testing using MSTest In this section, we will create a simple test case for testing the ‘LambdaTest Sample To-Do App’ on local selenium grid. The test case is as follows: a) Navigate to the to-do app https://lambdatest.github.io/sample-todo-app/ b) Mark the first two items as Done, i.e., Check those two items c) Add a new item – Adding item to the list d) Click the Add button to add that new item to the list The test case is implemented using MSTest framework in C# programming language. Below are the steps for implementing this test case: a) Create a new class file named ‘LocalMsTest.cs’ under the newly created test project b) Import necessary namespaces/packages, i.e., OpenQA.Selenium and OpenQA.Selenium.Interactions c) Define a method named ‘NavigateToDoApp’, which will contain the actual test implementation d) Use [TestClass] attribute to define this class as a Test Class, and use [TestMethod] attribute to mark the method as a Test Method e) Inside the NavigateToDoApp method, create an instance of ChromeDriver using WebDriverInfra.Create_Browser(BrowserType.Chrome) f) Use driver.Navigate().GoToUrl(test_url) for navigating to the test URL g) Use driver.Manage().Window.Maximize() for maximizing the browser window h) Locate the required web elements on the page using the Inspect Tool functionality of Chrome and perform the necessary operations i) Assert the expected outcome using the Assert class methods j) Finally, use driver.Quit() to close the browser window k) Run this test case by right-clicking on the method name in Visual Studio and choosing Run Tests option l) The Test Explorer window will show the execution status of the test 5. Parallel Testing using MSTest Parallelism in the MSTest framework can be achieved by using the In-assembly Parallel feature of MSTest V2. Below are the two ways in which it can be implemented: a) Using Annotations b) Using RunSettings In this tutorial, we will demonstrate how to achieve parallel testing on local infrastructure using annotations. The test case implementation remains the same as demonstrated in the previous section, but with a slight modification: a) Add [assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)] at the top of the class file b) This line will enable parallel execution of tests at Method Level with two threads/tests c) Run this test case by right-clicking on the method name in Visual Studio and choosing Run Tests option d) The Test Explorer window will show the execution status of the test, but now it will be executed in parallel 6. Cross Browser Testing using MSTest on the Cloud Parallel testing on local infrastructure can hit a roadblock if you want to test your website/web application across ‘N’ different browsers, browser versions, operating systems, and devices. You obviously cannot invest endlessly in setting up the test infrastructure since it is neither scalable nor economical. Not performing thorough cross-browser testing might result in a buggy experience if the customer accesses your product from an untested web browser. It reduces the overall test coverage as well. A feasible solution is to use a cloud based cross browser testing platform like the LambdaTest, where tests are executed on a Remote Selenium Grid. Using LambdaTest, cross browser tests can be executed on 3000+ different combinations of browsers, operating systems, and devices. Porting the existing implementation to remote Selenium Grid requires changes in the infrastructure-related code. Once you create an account on LambdaTest, you should make a note of the user-name & access-key from Profile Section as that combination is used for accessing the remote Selenium Grid on LambdaTest. The Automation Dashboard can be used to view all your test logs, screenshots, and video recording for your entire Selenium test. The desired browser and platform capabilities are generated using LambdaTest capabilities Generator. For example, below are the capabilities for Safari on the macOS Mojave platform: a) Set Capability("user", "Your Lambda Username") b) Set Capability("accessKey", "Your Lambda Access Key") c) Set Capability("build", "your build name") d) Set Capability("name", "your test name") e) Set Capability("platform", "MacOS Mojave") f) Set Capability("browserName", "Safari") g) Set Capability("version","12.0") For demonstrating the usage of LambdaTest and the effectiveness of Parallel test execution on a Remote Selenium Grid, we will implement the below three test cases: a) Test Case 1 – LamdaTest Sample To-Do App. b) Test Case 2 & 3 – Google Search for LambdaTest. The implementation is as follows: a) OpenQA.Selenium.Remote package/namespace is imported as RemoteWebDriver class is defined in it. b) The LambdaTest credentials are passed for accessing the remote Selenium Grid. c) Browser & Device capabilities are generated using the LambdaTest capabilities generator, and the same is passed to the Remote WebDriver API. d) The current plan on LambdaTest defines how many tests can be executed in parallel on the Remote Selenium Grid. For demo purposes, we have used 5 Parallel tests. e) The initialization code is implemented as a part of the [TestInitialize] attribute. It will be called only once during the initialization of the tests. f) As the browser-name, browser-version, platform-name accept input parameters as browser-name, browser-version, platform-name, and perform the necessary operations. Results1) LT_ToDo_TestDuration: 1 min2) LT_ToDo_Test (chrome,72.0,Windows 10))Duration: 1 min3) LT_ToDo_Test (MicrosoftEdge,18.0,Windows 10))Duration: 2 sec4) LT_ToDo_Test (Firefox,70.0,macOS High Sierra))Duration: 1 min567891011213141516171819202122232425262728293031111111111111111111111111111111
Company
LambdaTest
Date published
March 6, 2021
Author(s)
Himanshu Sheth
Word count
4517
Hacker News points
None found.
Language
English