How to unit test a Windows 10 app in Visual Studio

2019-01-23 03:55发布

问题:

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"'.

回答1:

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.



回答2:

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.



回答3:

Try RT-Clicking on a function name & see if there is a "Create Unit Tests" option on the context menu. For some reason adding a new unit test project is buggy, but the context menu worked for me.