MSTest V2 is a cross platform, open source testing framework. This course will teach you how to create automated tests to check your code is working as expected, create data driven tests, control test execution, and also how to extend the framework.
MSTest V2 is the evolution of the original "in-box" testing framework from Microsoft and is now a cross platform, open source testing framework. In this course, Automated Testing with MSTest V2, you'll learn how to use the framework to create automated test to help check your code is working correctly. First, you'll discover how to use the various assert features such as checking numeric, string, and collection values. Next, you'll explore how to take control of the test execution lifecycle and also execute additional setup or cleanup code. Finally, you'll learn how to execute a test multiple times with data driven tests and also how to customize and extend the MSTest framework to create more maintainable and readable test code. By the end of this course, you'll know how to create readable, maintainable, and customized automated tests with MSTest V2.
Course Overview Hi everyone. My name is Jason Roberts. Welcome to my course, Automated Testing With MSTest V2. I'm a Microsoft MVP, freelance developer, and author. In this course, we're going to learn how to create automated tests with the MSTest V2 testing framework. Some of the major topics that we'll cover in this course include how to check the results of production code by using MSTest asserts, how to take control of and also customize how your tests are executed, and reduce test code duplication by creating data-driven tests, and using the customization and extensibility features of the MSTest framework. By the end of this course, you'll understand how to create automated tests to ensure your code is behaving as it should and help to reduce production errors. Before beginning the course, it's recommended that you have at least a basic understanding of C#. I hope you'll join me on this journey with the Automated Testing With MSTest V2 course at Pluralsight.
Getting Started Hi everyone. My name is Jason Roberts from Pluralsight. Welcome to this course, Automated Testing with MSTest V2. MSTest has been around for many, many years and indeed was the first testing framework I ever used back in the day. MSTest V2 is the next iteration of the testing framework and is now open source and has support for testing. NET,. NET Core, desktop apps, and UWP store apps. The V2 framework is also delivered as a NuGet package rather than being tightly coupled to Visual Studio releases. In this module, we're going to learn how to get started with MSTest V2. We're going to kick off this module by looking at some of the benefits of automated tests, and we'll also get a high-level overview of the MSTest V2 testing framework. We'll learn about the high-level components of the testing framework, the essential NuGet packages that comprise the framework, and we'll also look at some of the framework's high-level features. We'll then head over to Visual Studio and create a new MSTest test project, and we'll write an initial test just so we can demonstrate how things work. We'll learn how we can run the tests within Visual Studio itself and also how we can execute tests at the command line.
Using Asserts to Pass or Fail Tests Hi. Welcome back. In the previous module, we got started with MSTest, and we created our first test. In this module, we're going to be delving in on the different ways that we can assert that our production code is doing the correct thing. So we're going to kick off this module with a brief overview of the asserts that we'll be looking at, and then we're going to head over to Visual Studio. We'll learn how we can assert on Boolean and null results, we'll learn how we can assert that the correct numeric results are being returned from our production code, and we'll also learn how we can deal with double values by providing an assert delta. We'll learn about simple string equality asserts and also a number of specialized string assert methods. We'll learn some of the different ways we can assert that collections contain the correct items and how to check that the production code is throwing the correct exceptions at the correct time. Finally we'll learn how we can assert that object are of the correct type and also how to assert on object references. So let's kick off this module with a brief overview of the types of asserts we can make with MSTest.
Controlling and Customizing Test Execution Hi. Welcome back. In the previous module, we learned how to use asserts to pass and fail tests. In this module, we're going to learn how we can control and customize the test execution lifecycle. So in this module, we're going to start off by learning how we can organize tests into arbitrary categories. We'll learn that we can do this at the individual test method level and also at the class level. And we'll learn how we can also execute only specific categories of tests. We'll learn that we can temporarily stop a test from running and also how we can output additional message during the text execution lifecycle. We'll also learn how we can run additional code during the test execution lifecycle, so we'll learn how we can execute code before and after test executes, before the first test in a test class executes, and also after the last test in a test class executes. And we'll also learn how to execute additional code before any test in a test assembly executes and also after the last test in a test assembly executes. Finally we're going to learn how we can share objects between tests during test execution. So without further ado, let's head over to Visual Studio, and we'll learn how we can categorize tests.
Creating Data Driven Tests Hi. Welcome back. In the previous module, we learned how we can customize and control the test execution lifecycle. In this module, we're going to learn how we can execute tests multiple times with sets of test data. So in this module, we're going to start off by getting a high-level overview of why we might want to create data-driven tests, and then we'll jump into Visual Studio and we'll learn how we can create a data-driven test and specify test data at the TestMethod level. When we take this approach, however, we're not able to share the set of test data across multiple tests, so we'll go and see how we can actually do this. We'll learn how we can share test data across multiple tests by implementing a getter-only static property that returns the data or alternatively implementing a static method that returns the data. And we'll also learn that we can centralize this data in a separate class to make it available across multiple test classes. We'll then learn that in addition to hardcoding test data within our test code, we can also get this test data from an external source. So we're going to learn how we can get this data from a CSV file, but this data could also come from a database, Excel file, REST service, or some other data source. So let's kick off this module with a high-level overview of why we might want to create data-driven tests.
Reducing Code Duplication and Increasing Test Readability Hi. Welcome back. In the previous module, we learned how we can start to reduce code duplication by making use of data-driven tests. In this module, we're going to continue this process and learn how we can further reduce code duplication and also increase test readability. So in this module, we're going to start off with an initial refactoring of the test code and start to reduce some of the duplication. We'll then look at some of the tests that we've already written, and we'll see that some of the asserts aren't as readable as they could be. To solve this, we're going to see how we can create custom asserts, so we're going to create a custom assert to check a numeric value is within a given range. We'll create an assert to check a string is not null or whitespace. We'll learn how we can create custom asserts that work on collections, such as checking all items in a collection of strings are not null or whitespace, asserting that all items within a collection satisfy a specified predicate, asserting that at least one item in the collection satisfies a predicate, and also a more generic assert to run an action on each item in a collection so we can make multiple asserts against each item. We'll then take a look at the test categories that we've defined, and we'll learn how we can create custom reusable test category attributes and how this improve readability and also remove duplicated magic strings. Finally we'll learn that if we're creating data-driven tests, we can actually go and create custom test data source attributes. Once again, this can increase readability and also remove any hardcoded values from the data access code. So let's head over to Visual Studio now, and we'll perform some initial test code refactoring.