A unit testing framework is basically an easy way for us to be able to combine a bunch methods and classes into multiple operations and then execute them as tests. These frameworks provide us amazing things such as being able to perform assertions, drive our tests with data, and categorize and group our tests. It basically makes test execution and automating our tests extremely easy.
MsTest is a framework that's provided to us by Visual Studio automatically. Whenever we download it, it's in there. It's out of the box. It's really fast. It's very user friendly. Microsoft has been doing a lot of work on it and this framework is becoming extremely amazing.
There are other third-party frameworks out there, such as NUnit, which is also very popular, so you may want to check that out. Also, MsTest framework is very similar to other unit testing frameworks if you're familiar with other programming languages, such JUnit, Mocha or Espresso.
One of the most important things about other testing frameworks, but specifically about MsTest, is that they provide us attributes. Attributes are a way to tell the compiler about what is going on with your code.
As you can see here, we have a standard unit test.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
A unit test lives inside a public class and that class is tagged with an attribute known as a [TestClass]
. That's a MsTest attribute that tells the compiler that we have a test class. It's not a regular class, but a test class that can be executed and provide some kind of a result.
Inside of our test classes, we have test methods, denoted by [TestMethod]
. Test methods tell the compiler that here we have a method that's going to be executed as a test.
All of our actions such as interacting with Selenium, interacting with browsers, and doing assertions go inside of our the test method. Ultimately, whenever that test method passes or fails, we're going to get a result on that test method. All test methods need to be public and void.
Note
This course is not meant to be a comprehensive course in MsTest. If you want to learn more, there are plenty of other resources. I've provided a bunch of other attributes that are provided to us from MsTest from Microsoft.
As you can see, there are many different options and many different attributes that we can add. Options that allow us to datadrive our test. They allow us to clean up and tear down our test data and there are so many other options.
The other really important part about MsTest is the assertions. That's where all the power comes from that allows us to basically validate that an object is in some specific state or that one object matches to another object or any kind of validation that we're trying to do. We do that through assertions.
There are many different kinds of assertions. We can check that "one plus one equals two" or we can check that an element exists on a page.
Again, there are many different methods. This is not a comprehensive course on MsTest, but just know that these assertions basically are a key part of all of our tests. As we start digging into the code, you will see us using these assertions to be able to set a pass or fail state for all of our test methods. All MsTest assertions can be found on their website.