Set Up a Unit Testing Project in C#

A free video tutorial from Dmitri Nesteruk
Software/Hardware Engineering • Quant Finance • Algotrading
Rating: 4.5 out of 5Instructor rating
22 courses
289,818 students
Setting Up a Unit Testing Project

Learn more from the full course

Master .NET and C# Unit Testing with NUnit and Moq

Become an expert in unit testing with NUnit, Moq, dotMemoryUnit, ReSharper and other tools!

03:13:23 of on-demand video • Updated August 2017

What unit tests are for and how they are written
How to set up and use the NUnit test framework
What are the different test doubles: fakes, stubs, mocks
How to effectively use the Moq mocking framework
How to use the ReSharper unit test runner
How to write memory-related unit tests using dotMemoryUnit
English [Auto]
So the first thing that we need to discuss is what exactly are you tests and how do they appear as part of your project because while unit test you can place them into a separate project or you can just stick them into an existing project because a unit test is just you know a bunch of methods in a class. Basically if you want to have unit tests as part of a solution then the typical approach is to go for a class library. By the way you'll notice that here in the set of templates we also have test this unit test project is not what you want because the unit test project here is actually for M-S test for the Microsoft variety of unit testing which they came up with at some point but nobody really uses that much. I know some companies that do but generally and unit is the way to go and that's what everyone uses just about. So if we're going to go with a class library project I'm just going to call this unit tests although strictly speaking if this was part of some enterprise development then you would have maybe the name of the company or the name of the product like Fabri cam Daut then some widget if you are making a widget application then unit tests. I'm just going to call this unit test and that's going to be the project that we'll stick to for the duration of this entire course. So I'm going to make this this is a class library so it's not executable. It makes a DSL and there's the can subsequently be loaded and executed in a unit test runner. Now before you can do any of this we're going to look at how to actually add any unit support to your project. Now as I mentioned previously I'm going to be using reshape or for unit testing but you can use whatever you want. The process of adding the references for units are pretty much the same almost. So one way to go is right click the references and go into managed to get packages. So here in the browse Well you can already see it here because it's like one of the most popular packages out there. So it may as well select this and press install and that installs all the appropriate references. It basically takes the latest version of Venona that's available which is 3:6 1 and it installs that. So if we just close this you'll notice that we now have a new reference to the unit framework. That's the assembly that actually makes all the magic possible. But this way of adding the new package is actually the only way there is an additional way so let's get rid of the new package. Here is I'm going to uninstall it and I want to show you a much cooler way if you're a resharpen user. So the cool way involves you actually just using an attribute which you know exists in a particular package in this case what we know or at least I know and you're going to know certainly is that unit comes with a class called test fixture. It's actually an attribute that you use to decorate the test suite. So as you can see the code is rather here but with the sharpie you can press enter and one of the options here is to find this type on. You get to Oregon what this does is it goes into the brain's own cache of the names of types and where they appear in different projects and as you can see and unit here at the very top you can press the Install button once again and press the install button here. This does pretty much the same thing but overall this is a faster mechanism. If you already know the name of the type that you're going to use so having made this we are now ready to actually run unit tests so you have the using statement for and unit framework. Let me just show you a basic overview of what it feels like to work with units. So I have a bunch of attributes here and these attributes actually define the fact that we have a bunch of tests as you can see there are highlighting here. So you would run these tests either using Rechab or or using Visual Studio's own test runner or using some external with s strong and we're going to look at all of those options in the next clip.