- Visual Studio Unit Test Not Running
- Visual Studio Unit Test Tutorial
- Visual Studio Unit Tests
- Visual Studio Unit Test C#
This topic describes a step-by-step process of creating the simplest unit test in Microsoft Visual Studio 2010 (C++) for a CLR Console Application. Using this example, you can learn to create your own Unit-tests. The example also demonstrates the use of the Assert class for testing the work functions.
- Live Unit Testing automatically runs any impacted unit tests in the background and shows your code coverage live in Visual Studio. As you modify your code, Live Unit Testing lets you know if your code changes are covered by existing tests or if you need to write new tests. Get gentle reminders to write new tests as you type.
- Today we’re here to talk about JavaScript unit testing in Visual Studio. If you’re a.NET developer creating web applications in Visual Studio, it makes sense for you to use just a single environment to test both your C# (or another.NET language) code and your JavaScript code.
Contents

- Instructions
- 4. Creating a test
- 4.5. Connection of the “MaxApp.cpp” module of the MaxApp project to the TestMaxApp project
- 4. Creating a test
You can use Test Explorer to start a debugging session for your tests. Stepping through your code with the Visual Studio debugger seamlessly takes you back and forth between the unit tests and the project under test. To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Set a breakpoint in the unit test that you want to debug. Right click anywhere within the unit test and select 'Debug Tests' from the context menu. Stepping through the unit test is very similar to how we step through any other code in Visual Studio. Step Over - F10. Step Into - F11. Test Explorer is a handy feature of Visual Studio that allows you to run unit tests within a project, customize how they’re run, and interpret their output. Test Explorer allows you to manipulate the order that unit tests are run, create custom playlists to segment out which unit tests run, and run tests in various user-defined groups.
Search other websites:
The task
For a CLR Console Application, develop a Unit-test that tests the Max3() function, which defines the maximum element of three numbers. For the Max3() function, set the TestMax() test method. Check the function.
Instructions
1. Create a CLR Console Application
After starting Microsoft Visual Studio, you must select
As a result, the New Project window opens (Figure 1), in which you need to select template
Project name set as MaxApp (or, if desired, another). In our case, the project folder is set to “E:Test” (the “Location:” field).
Figure 1. Creating an application using the CLR Console Application template
After selecting OK, an application of the CLR Console Application type will be created.
After creating the application, the Microsoft Visual Studio text box will have an approximate view, as shown in Figure 2.
Figure 2. Application view after creating
As you can see in Figure 2, the MyApp.cpp module contains the connection of the module “stdafx.h” and the main() function, which is the entry point to the program (this function is associated with the C++ program).
2. Implementation of function Max()
Before the declaration of the main() function, the text of the Max() function is entered, which looks like this:
This function will need to be tested using Unit-test in Microsoft Visual Studio.
3. The text of the program, which must be tested
At the moment, the text of the program you want to test is as follows:
Because this program will be tested from another testing module, then nothing needs to be entered in the main() function. Since, according to the condition of the task, you need to test the operation of the Max() function. But this will already be done from the testing module. At the moment our program is ready for testing.
4. Creating a test
The test is created by a separate project in the Solution. The program, which will be tested does not know about it. The test program that will test calls the functions of the program under test. In our case, the test program will call the function
4.1. Adding a new project to the solution
To create a test, you need to create a new project in the Solution. To do this, a sequence of commands is called in Visual Studio
This will open the “Add New Project” window, shown in Figure 3.
Figure 3. The “Add New Project” window
In the window a templates group of Visual C ++ – Test is selected. From the proposed templates, the project template “Test Project” is selected. The “Name” field indicates the name of the project that will test our program. You need to set, for example, TestMaxApp. The project is located in a separate folder “E:TestMaxApp”. After selecting OK, the system creates new project files that will be tested, as shown in Figure 4. A file (module) is created with the name “UnitTest1.cpp”. In this file, you need to enter the code that will test the Max() function from the MaxApp.cpp module.
Figure 4. The text of UnitTest1.cpp file. The Solution Explorer window with the displayed projects TestMaxApp and MaxApp
4.2. The structure of solution
As you can see in Figure 4, Solution Explorer displays the Solution Items structure, which contains two projects:
- the MaxApp project. This is a project created using the CLR Console Application template with the Max() function, which to be tested;
- the TestMaxApp project. This project is designed to test the functions of the MaxApp project. The program code that will test the Max() function will be added to the project file UnitTest1 of the TestMaxApp project.
Both projects can be executed independently of each other.
4.3. The text of the test file “UnitTest1.cpp”. Attributes [TestMethod] and [TestClass]
In the TestMaxApp project, the UnitTest1.cpp test file is the main test file. This file contains methods that will test the functions of the project “MaxApp.cpp”. The TestMaxApp project can contain any number of files that contain tests (for example, UnitTest2.cpp, UnitTest3.cpp, etc.).
Listing of the UnitTest1.cpp file generated by MS Visual Studio is as follows:
As you can see from the above code, the file contains a class named UnitTest1. The class has a public method named TestMethod1().Before implementation of TestMethod1() method, the [TestMethod] attribute is placed. This means that in the body of the method, you need to enter code that will test the functions of the MaxApp project.
In the class, you can enter any number of methods that will test different functions from different modules. The main thing is that these methods are marked with the attribute [TestMethod]. For example, if you want to add a second test method named MySecondTestMethod(), you need to enter approximately the following code into the class body:
Similarly, the [TestClass] attribute is placed before the UnitTest1 class. This means that there are testing methods in the class.
4.4. Making changes in the text of UnitTest1
You can change the method names and add new methods that are marked with the [TestMethod] attribute in the UnitTest1.cpp module. With this in mind, in the UnitTest1.cpp text, the TestMethod1() method must be renamed to TestMax().
After the changes have been made, the abbreviated text of UnitTest1.cpp file module will look like:
4.5. Connection of the “MaxApp.cpp” module of the MaxApp project to the TestMaxApp project
To access the Max() function of the “MaxApp.cpp” module of the MaxApp project from the TestMaxApp project, you need to call this module using the #include directive.
There are 2 methods of this connection:
- connect the file “MaxApp.cpp”, specifying its full name on the disk (or on another source);
- in the references to the Microsoft Visual Studio assemblies, configure the folder with the MaxApp project so that it is automatically connected.
After this, you can access the “MaxApp.cpp” file by its abbreviated name. In this topic, both methods are described.
4.5.1. Method 1. Connection with a full name request
This method is more simplified. It is convenient when you need to connect a small number of files to the project for testing.
In the TestMaxApp project, in the “UnitTest1.cpp” file, after connecting namespaces, you need to set the following line:
This specifies the full name of the MaxApp.cpp file on the disk, in which the Max() function is located, which you need to test.
4.5.2. Method 2. Connection with an abbreviated name
This method is effective when you need to test several modules of different projects. A whole directory (folder) is connected with the files of the tested project. In our case, it is needed to use the folder:
in the list of references to assemblies (projects) that automatically connect to the TestMaxApp project. After that, you can connect the MaxApp.cpp module by abbreviated name
avoiding the use of a long name as shown in section 4.5.1.
First of all, the “References …” command is called from the TestMaxApp context menu as shown in Figure 5. Another way to call – the command Project-> References … (previously need to be select TestMaxApp project).
Figure 5. Calling the default references viewer window for assemblies that are used in the TestMaxApp project
This opens the “TestMaxApp Property Pages” window which is shown in Figure 6.
Figure 6. The “TestMaxApp Property Pages” window, command “Add New Reference …”
In the “TestMaxApp Property Pages” window, you first need to activate the “Common Properties” -> “Framework and References” tab.
Then you need to select the command “Add New Reference …”. This command allows adding new folders with modules to the project, methods (functions, resources) of which can then be included in the project by the #include directive. After selecting the command, the Add Reference window, shown in Figure 7, opens.
Figure 7. The “Add Reference” window with the displayed projects in the current solution
In the “Add Reference” window, the Project tab displays the project folders that are used in this solution. In our case, only one MaxApp project is displayed, which is located in the folder
After selecting OK, you return to the previous window, in which the reference to the MaxApp project will be displayed in the list of references to the assemblies.
Figure 8. The “TestMaxApp Property Pages” window after adding the MaxApp project to the list of public assemblies
That’s not all! The next step is to connect the MaxTest project folder to the “Include Directories” list.
To do this, you must first activate the line
as shown in Figure 9.
Figure 9. The “TestMaxApp Property Pages” window, line “Include Directories”
In the “General” list, “Include Directories” is selected. As a result, a dropdown list is opened, in which you need to select the command “<Edit …>”. After that, the “Include Directories” window shown in Figure 10 opens.

Figure 10. The “New Line” command and the folder selection button with the files that connect
In the “Include Directories” window, add a new line with the “New Line” command and select the folder with the MaxApp project, as shown in Figure 10. After selecting the “…” button, the standard Windows window opens to select the desired folder. In our case, you need to select a folder
After selecting, the Include Directories window will have the appearance as shown in Figure 11.
Figure 11. The Include Directories window after selecting the folder E:TestMaxAppMaxApp
To go to the previous window, select OK.
Figure 12. The TextMaxApp Property Pages window after configuring the line “Include Directories”
To proceed to writing the program test code, select OK.
After the performed actions, all files from the folder
can be connected by abbreviated name, for example:
4.6. Writing code in the TestMax() method
The code that tests the Max() function fits into the body of the TestMax() method of the “UnitTest1.cpp” module. The code fragment of the TestMax() method looks like this:
The above code calls the function AreEqual() from the Assert class. This function compares the value that was returned from the Max() function and the value 6. In this case, the number 7 (maximum) is compared with the number 6. For this way, the test will not be passed, since 7 is not equal to 6. As a result, the Assert::AreEqual() function throws an exception, which will be displayed in a special window (see paragraph 5).
5. Running the test and checking the test results
In Microsoft Visual Studio, a special menu of commands, called Test, is implemented to work with Unit-tests. To run the test, you must select one of the commands
or
as shown in Figure 13.
Figure 13. Calling the test command and viewing the result
After starting the test, the result can be viewed at the bottom of the “Test Results” window. As you can see, the test is failed. This is logical, because in the function Assert::AreEqual() we compare the numbers 6 and 7, which are different from each other. Here the number 6 is specially introduced instead of the number 7.
If instead of 6 you enter the correct answer – the number 7 (maximum between 5, 6, 7), then the test will be passed. In this case, the text of the TestMax() method will be as follows:
The result window is shown in Figure 14.
Figure 14. The test result for the case if you enter the correct checking
Now we can conclude that the function Max() was developed correctly.
6. The code of UnitTest1 module
Below is the text of UnitTest1.cpp, which tests the Max() function from the module “MaxApp.cpp”:
7. Conclusion. Interaction between projects
In this topic, two projects are developed in the solution. One MaxApp project contains the Max() function to be tested. The second project TestMaxApp contains methods that test.
In Microsoft Visual Studio, each project is run using various menu commands. For example, the MaxApp project is launched in the standard way from the Run menu. And the test project TestMaxApp is run from the special menu “Test”.
Related topics
When you find yourself (or your company) with more code than anyone could ever test by hand, what can you do? Well, unit testing has always been the perfect solution, as you can run tests that check more data than a person could in a day in a matter of milliseconds. So today I’ll take a look into a few popular C# unit testing frameworks and try them out first hand so you can choose which one best suits your project.
Unit tests can be run as often as you want, on as many different kinds of data as you want and with next to no human involvement beyond once the tests are written.
Not only that, but using code to test code will often result in you noticing flaws with your program that would have been very difficult to spot from a programmer’s viewpoint.
Raygun lets you detect and diagnose errors and performance issues in your codebase with ease
Popular C# unit testing frameworks
The unit testing frameworks I’ll be testing are:
- NUnit
- XUnit
- Built-in Visual Studio testing tools
All of these unit testing frameworks offer a similar end goal, to help make writing unit tests faster, simpler and easier! But there are still a few key differences between them. Some are more focused towards powerful complex tests, while others rank simplicity and usability as a higher priority.
First up is Microsoft’s own built in Visual Studio unit testing tools
In most versions since 2005, Visual Studio has come with a built in testing framework supported by Microsoft. This framework certainly wins the most points for installation. Though if your copy of Visual Studio doesn’t come with it already included you are going to have to jump though a few hoops to get it going. (We wrote a review of the 2017 version of Visual Studio here.)
This framework is the simplest of the three, and uses an easy to understand method attribute structure (much like most testing frameworks) where you are able to add tags such as ‘[TestClass]’and ‘[TestMethod]’to your code in order to get testing.
Visual Studio even has a UI panel dedicated to visualizing your tests, which can be found under Test -> Windows -> Test Explorer.
Now before we dive into trying out this testing framework let’s introduce our example classes that need testing.
First we have a Raygun, which we can fireand recharge. The only thing we need to keep track of with our Raygun is it’s ammo, which can run out.
We also have a bug, which we can shootat with our Raygun. But this bug has the ability to dodgeour attempts to shoot it.
If we shoot at a bug after it has just dodged, we will miss. Though if we hit the bug square on, it’s safe to assume that it will be dead.
These two classes are defined as follows:
Seems simple enough, but we need to make sure that our Rayguns and bugs behave as we want them to.
So then it’s time to write some unit tests! (We wrote about how to write robust unit tests in C# here.)
First up let’s try a simple situation where we want to shootat, and hit, a bug.
What we would expect is that afterwards the bug will be dead, and the Raygun will still have a bit of juice left in it.
Well, let’s see if we are right:
The two new things you will notice in this snippet of code is the [TestClass]and [TestMethod]tags, which certainly don’t just float around in normal code.
These tags are what allow Visual Studio’s built in testing framework to recognize this particular class as a class that contains unit tests, and to treat the method TryShootBug()as a test case, instead of just an ordinary method.
Since these tools are built for Visual Studio, running your tests from within Visual Studio is very simple.
Just right click on any[TestMethod]tags as shown:
And would you look at that, the test passed. Looks like our Raygun can at least hit a stationary bug.
Of course this is only showing the bare basics of what Visual Studio’s testing tools can do.
Some other very useful tags you will surely be using are the [TestInitialize]and [TestCleanup]tags.
These tags allow you to specify code that is run before(initialize) and after(cleanup) every individual test is run.
So if you want to reload your Raygun after every encounter like a stylish gunslinger, then this should do the trick:
Stylish.
Visual Studio Unit Test Not Running
While we are still talking about the Visual Studio testing tools I’ll quickly mention the [ExpectedException]tag, which is incredibly useful for when you want to deliberately cause an exception in your test (which you will certainly want to do at some point to make sure your program isn’t accepting data it shouldn’t).
Here’s a quick example of how you would write a test that results in an exception:
Don’t worry about the index out of bounds exception, thanks to the [ExpectedException]tag the exception will be treated as a success and your test will pass. On the contrary if the exception isn’t thrown then the test will fail. Anyway, it’s about time we moved on to some more testing platforms!
Overall the built in Visual Studio testing tools do exactly what they say on the box. They are simple, easy to use and handle all the basic testing functionality you would need. Plus if you’re already working in Visual Studio then they are already ready to use!
Next up is arguably the most popular C# testing platform, NUnit
NUnit is an incredibly widely used tool for testing, and it serves as an excellent example of the open source unit testing frameworks. It’s a broad and powerful testing solution. In fact it’s what we use here at Raygun for the bulk of our unit testing.
NUnit is installed via a NuGet package, which you can search for within Visual Studio.
The packages I’ve used for this example are NUnitand NUnit.ConsoleRunner, though you also have the option of installing a GUI-based plugin for Visual Studio.
NUnit uses a very similar attribute style system just like the visual studio testing tools, but now we will be referring to a [TestClass] as a [TestFixture],and a [TestMethod] as simply a [Test].
Visual Studio Unit Test Tutorial
Now let’s go back to our Rayguns and bugs and have a look at another example, but this time using NUnit.
This time let’s make sure our dodgesand ammoare working properly, so let’s try and shoot a much more mobile bug:
Notice the new [TestFixture]and [Test]tags.
Now in order to run this test using NUnit, we need to seek the command line(unless of course you’ve chosen to install a GUI based plugin.)
First, you must make sure you are in your project’s root directory (e.g. C:Users</span>yourUserNameDocumentsVisual Studio 2015Projects</span>YourProjectName) and then enter the following command in a new cmd window:
packagesNUnit.ConsoleRunner.3.6.0toolsnunit3-console.exe YourProjectNamebinDebug</span>YourProjectName.dll
Assuming everything is set up properly, the NUnit console runner will run all the tests in your project and give you a nice little report on how things went:
Looks like our bug sure can dodge and our Raygun can certainly run out of ammo!
One feature of NUnit that makes it incredibly useful is the ability to include parameters in your tests!
This means that you can write a test case with arguments, then easily run the same test with a range of unique data. This removes the need to write unique test cases for every set of arguments you want to test.
Here’s a quick example test case we could use to make sure our Raygun was actually running out of ammoat the right time, in a much smarter way than before:
Excellent, with this one test case we were able to make sure a Raygun which has fired two shots still has ammo, while one that has fired three is empty. And thanks to the [TestCase]tag we were easily able to test a whole bunch of other values while we were at it!
Overall NUnit is an excellent testing framework, and as you delve deeper into what it can offer, it surely exceeds what Microsoft’s built in testing can offer.
Anyway, let’s look at our last testing framework, and our last attempt as shooting bugs with Rayguns!
If you like the sound of Facts and Theories, then it’s time to look at XUnit
XUnit is an open source testing platform with a larger focus in extensibility and flexibility. XUnit follows a more community minded development structure and focuses on being easy to expand upon.
Visual Studio Unit Tests
XUnit actually refers to a grouping of frameworks, but we will be focusing on the C# version.
Other versions include JUnit, a very well known testing framework for Java.
XUnit also uses a more modern and unique style of testing, by doing away with the standard [test] [testfixture] terminology and using new fancy tags like Factsand Theories.
NUnit and XUnit are actually quite similar in many ways, as NUnit serves as a base for a lot of the new features XUnit brings forward.
Visual Studio Unit Test C#
Note that XUnit is also installed via a NuGet package much like NUnit, which you can search for within Visual Studio. The packages I’ve used for this example are XUnitand XUnit.ConsoleRunner, though you also have the option of installing a GUI-based plugin for Visual Studio.
Much like the [TestCase] tag in NUnit, XUnit has its own solution to providing parameters to a test case. To do so we will be using the new[InLineData]tag and Theories.
In general, a test case that has no parameters (so it doesn’t rely on any changing data) is referred to as a Factin XUnit, meaning that it will always execute the same (so ‘Fact’ suits it pretty well). On the other hand, we have Theories, which refers to a test case that can take data directly from [InLineData]tags or even from an Excel spreadsheet


So with all these new fancy keywords in mind, let’s write a test in XUnit that uses a theory to test our bugs dodge ability:
This test covers both cases at once, where the bug dodges and survives, or doesn’t dodge and gets hit. Lovely!
Now, last step, lets run the XUnit test runner from the command line (note that much like NUnit, XUnit also has a GUI based visual studio plugin available for you to run tests with).
First you must make sure you are in your project’s root directory, just like NUnit (e.g. C:Users</span>yourUserNameDocumentsVisual Studio 2015Projects</span>YourProjectName) and then enter the following command in a new cmd window:
packagesxunit.runner.console.2.1.0toolsxunit.console.exe YourProjectNamebinDebug</span>YourProjectName.dll
Assuming everything is set up properly, the XUnit console runner will run all the tests in your project and let you know how your tests turned out.
Looks like our dodging tests passed!
Overall XUnit acts as the more contemporary version of NUnit, offering flexible and usable testing with a fresh coat of paint.
In conclusion…
Regardless of which of the unit testing frameworks you use, you’re going to be getting all the basics. However, there are a few differences between them that I hope I’ve highlighted so you can choose the right one for your project. Whether it’s the convenience of Microsoft’s built in unit testing framework, the solid and well proven status of NUnit, or the modern take on unit testing that XUnit provides, theres always something out there that will give you exactly what you need!
Want to add an extra layer of protection for your code? Catch the errors that fall through the cracks with Raygun. Take a free trial here.