Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a Windows 10 app in Visual Studio?

How can I unit test a Windows 10 app in Visual Studio 2015?

I created a Blank App Universal Windows project and added a new Unit Test Project to the solution, but when I try to add a reference to my UWP app in the Test Project, I get an error saying 'Unable to add a reference to project "Project Name"'.

enter image description here

like image 207
Lukasz Ciastko Avatar asked Aug 07 '15 19:08

Lukasz Ciastko


People also ask

How do I run a unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

How do I test a project in Visual Studio?

Select the test project in Solution Explorer. On the Project menu, select Add Reference. In Reference Manager, select the Solution node under Projects. Select the code project you want to test, and then select OK.


2 Answers

Right click on your solution and choose Add > New Project. Then select Unit Test App (Windows Universal). Make sure that you don't select Unit Test Library or Unit Test Project. It must be the Universal one. This should generate a base class for you to work with.

You'll probably then want to add a reference to your main project. To do this, right click on your test project and select Add > Reference and then choose your main project. You should now be able to create classes from your main project to test.

like image 87
Gordonium Avatar answered Oct 22 '22 00:10

Gordonium


I can understand the confusion - the Visual Studio team is not consistent in there otherwise fine project hierarchy decisions.

Normal approach was to place Test projects under Test. For reasons unknown, they decided to place the Unit Test App (Universal Windows) directly under Windows -> Universal and hereby bypassing common sense.

In summary; Test projects for Windows -> Classic Desktop and Web is placed under Test. Test projects for Windows -> Universal is placed under Windows -> Universal.

like image 32
gimlichael Avatar answered Oct 22 '22 00:10

gimlichael