Testing UWP app with xUnit on VS2015

2019-02-18 14:11发布

问题:

I have managed to create an xUnit project on VS2015 to unit-test UWP apps. There is a background to this question here on stackoverflow which gives some context to this question.

I can compile and run the test, however when I reference a project to be tested, the following error comes up.

------ Run test started ------
Updating the layout...

Checking whether required frameworks are installed...

Registering the application to run from layout...

Deployment complete (1857ms). Full package name: "8f4533e5-fec8-415b-94ab-6bce6b37374f_1.0.0.0_x86__5gyrq6psz227t"

A user callback threw an exception.  Check the exception stack and inner exception to determine the callback that failed.
========== Run test finished: 0 run (0:00:04.3378913) ==========

I understand the referenced project (which is to be unit-tested) is causing the a user callback threw an exception but I see no obvious way to debug this error. The mode of execution doesn't look like a normal debug that can be examined with breakpoints.

Does anybody know how to dig into and find out what is causing the a user callback threw an exception error? The app project to be tested is error-free and runs fine on its own.

回答1:

Okay, did some researching and self-learning and now I can answer my own question.

The most important fact:

You cannot unit test a UWP project, only a class library (at this time).

The above is a quote from Jerry Nixon (see toward the end of linked page). So, that says it all, but not knowing this important fact will no doubt set you on a wild goose chase as it did to me.

What the above means is that, if you haven't done already, you need to split your app project into the UI part (View and other UI related parts), and the rest into non-UI library project (comprising models, data access methods, etc.); the non-UI library project being the candidate to be unit-tested. Fortunately, it was easy to accomplish this with my app and now I can unit test the most crucial parts of my app by referencing the class library part in the test project. Is was also instructive for the future.

The above error is therefore due to referencing a UI laden UWP project which cannot be tested at this time.