Basics of Unit Testing for C# Developers

Every developer needs to test their code or have it tested by someone. In this course, you'll learn how to start unit testing to improve the quality of your applications.
Course info
Rating
(88)
Level
Beginner
Updated
Jun 12, 2017
Duration
2h 9m
Table of contents
Course Overview
Why You Need Unit Testing
Your First Unit Tests
Avoid Hard-Coding in Unit Tests
Initialization and Cleanup
Attributes Help You Organize Your Unit Tests
Assert Classes Save a Lot of Time
Consolidate Tests by Making Them Data-Driven
Automating Unit Tests with VS.Test.Console
Description
Course info
Rating
(88)
Level
Beginner
Updated
Jun 12, 2017
Duration
2h 9m
Description

Every developer knows they should be creating unit tests to improve the quality of their applications. In this course, Basics of Unit Testing for C# Developers, you'll learn how to create unit tests by using Visual Studio. First, you'll see how easy it is to get started with creating unit tests. Next, you'll explore how to simplify the unit test process by creating data-driven tests. Finally, you'll cover how to automate your unit tests by scheduling them to run via the command line utility VSTest.Console. By the end of this course, you'll have the required skills needed to go on and learn more advanced topics in unit testing.

About the author
About the author

Paul loves teaching and technology, and has been teaching tech and business topics for over 30 years. Paul helps clients develop applications, and instructs them on the best use of technology.

More from the author
Angular Security Using JSON Web Tokens
Intermediate
2h 50m
Apr 2, 2018
More courses by Paul D. Sheriff
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name is Paul Sheriff, and welcome to my course, Basics of Unit Testing for C# Developers. I am a Business Solutions Analyst for Fairway Technologies, a premiere provider of expert technology consulting and software development services. Every developer needs to test their code or have it tested by someone. Most developers are not great at testing their own code, and this is where unit testing can help. Learning to create unit tests will improve your code quality, make your end users happier, and will actually make you a better programmer. In this course, you are going to learn to create unit tests from scratch throughout eight modules. These modules are Why You Need Unit Testing, Your First Unit Tests, Avoid Hard-Coding, Initialization and Cleanup, Attributes Help You Organize Your Unit Tests, Assert Classes Save a Lot of Time, Consolidate Tests by Making Them Data Driven, Automating Unit Tests with VSTest. Console. By the end of this course, you will have the required skills needed to go on and learn more advanced topics in unit testing with some of the other great courses in the Pluralsight library. I would suggest after this course, you go on to the Advanced Unit Testing course. I hope you'll join me on your mission to becoming a great programmer by employing unit tests as a part of you our development cycle.

Your First Unit Tests
Hello everybody, Paul Sheriff here with Pluralsight. This module is Your First Unit Tests. Now what we're going to do in this module is we're going to create a class and a method that we need to test. We're then going to create a unit test project, write some unit test stubs, and then write the unit tests themselves. So to get started, we're going to create a class library project with one class called FileProcess, and one method that is going to check to see if a file exists. We'll then create a unit test project, same name, FileProcess, but add Test on the end for the class name. Then we're going to have a few different methods we're going to write that are very descriptive that tell us what we're checking for in that test: FileNameDoesExist, FileNameDoesNotExist, FileNameNullOrEmpty_ThrowsArgumentNullException, very descriptive names. Here is our FileExists method that accepts one parameter, which is a fileName. We then check to make sure that we have a valid parameter that's been passed in, and if not, hey, we're going to throw an exception that says ArgumentNullException for that particular argument, and then we're going to return a true or false based on whether or not that file exists.

Avoid Hard-Coding in Unit Tests
Hello everybody, Paul Sheriff here with Pluralsight. This module is Avoid Hard-Coding in Unit Tests. Our goals for this module are to learn how to use constants, use a configuration file, basically help keep our unit tests as self- contained as possible. In addition, we're going to take a look at the TestContext property, which we'll use in many of the later modules. There are many best practices we use when we're coding regular classes and methods, and there's the same ones that we use basically in doing unit tests, as well. Don't hard code things, use constants wherever you can, maybe store data in a configuration file, set up and tear down as close to each unit test as possible, use comments, and use good variable and method names. Let's take a look at using constants. Let's go and remove this hard-coded file name right here, so let's cut that out, and we'll come up to the top of our class, and let's just add a private constant string called BAD_FILE_NAME, and let's set it equal to that value. So this is just like you would do in regular coding, right, I mean even though you're in a unit test, you still want to apply the best practices, so take advantage of constants whenever you can. We're going to do something different with this one in our next sample.

Initialization and Cleanup
Hello everybody, Paul Sheriff here with Pluralsight. This module is Initialization and Cleanup. The goals for this module are to learn about the initialization attributes that you can apply to a method to have it run first, and the cleanup attributes that you can add to a method to have it run after either a test or a class or the assembly is completed. And we're going to talk about the execution order, the order in which these occur. So, let's go ahead and get started.

Attributes Help You Organize Your Unit Tests
Hello everybody, Paul Sheriff here with Pluralsight. This module is Unit Testing Attributes. In this module, we're going to take a look at the different kinds of attributes you can apply to your test methods. These fall into four categories, grouping, action, informational, and data-driven. Let's take a look. The attributes that fall under the grouping category are those called Owner, Priority, and TestCategory. These help us view the different tests in the Test Explorer window in different groupings. We do have actions attributes as well. These are DeploymentItem, Ignore, and Timeout. These actually do something to your test method. Under informational you have a Description attribute, and under data-driven we have the DataSource attribute. We'll take a look at each one of these.

Assert Classes Save a Lot of Time
Hello everybody. Paul Sheriff here with Pluralsight. This module is Assert Classes Save a Lot of Time. In this module, we're going to learn about classes to help you test. We've already seen the Assert class, but we're going to look at a few different methods. We're also going to take a look at the StringAssert class and the CollectionAssert class. What you'll find is that these do really save you a lot of time because you don't have to then write your own assertions. Yes we've seen a lot of methods in the Assert class, but let's take a look at the full list. There are assertions that we can make that are positive such as AreEqual, AreSame, IsTrue, IsInstanceTypeOf, and IsNull. There are also the negative of these as well. Maybe we want to check two objects to see if they AreNotEqual, are not the same, or that something IsFalse, IsNotInstanceTypeOf, and IsNotNull. There are of course a couple of other methods that we can take advantage of as well, such as Fail and Inconclusive.

Consolidate Tests by Making Them Data-Driven
Hello everybody. Paul Sheriff here with Pluralsight. This module is Consolidate Tests by Making Them Data-Driven. In this module, we're going to create a table to hold test data, and we're going to use the data from that table for doing our testing. We're going to build a method and add on the DataSource attribute. This helps us connect to a table to retrieve data. Let's get started. We're going to do data-driven testing to help eliminate creating multiple test methods. If you remember for our FileExists, we created like four different methods. Doing data-driven allows us to combine all of those into just a single method. What we're going to do is we're going to create a table to hold any inputs going into the method, so any parameters going in, and then the output that we expect to come back. Using data-driven testing, Microsoft has created their unit test framework to retrieve rows from a table automatically. We then use that test data, both the input and the output, to call the method for however many rows we put into our table.

Automating Unit Tests with VS.Test.Console
Hello everyone. This is Paul Sheriff with Pluralsight. This module is Automating Unit Tests with VSTest. Console. In this module, we're going to be using VSTest. Console, which is a command line utility. We're going to see how to view the available loggers that are available and even do some logging. And we're going to show you how to run some specific tests.