locked
How do I write unit tests to cover code in a Windows 8.1 Universal App Shared project? RRS feed

  • Question

  • I started a new Windows 8.1 app yesterday and I noticed that I was not able to write a unit test for code within the Universal App's shared project.  I believe I was using the Windows 8.1 Universal App Hub project template.

    I cannot remember if I was using a Windows 8 unit test project or a Portable class library unit test project.  In my opinion the project types are becoming a little confusing with the addition of the universal app templates.  Regardless, I should be able to unit test code that I write in a Universal App's Shared Project.  Otherwise, why would you feel comfortable sharing the code between other projects.

    Is this by design or is there some way to accomplish this?

    Thursday, April 10, 2014 1:40 PM

Answers

  • Hi,

    I believe that we can’t unit test a shared project separately. We need to unit test the windows store app and windows phone app separately.

    Please see:

    http://www.jevgeni.net/2014/04/06/shared-projects-portable-class-libraries-windows-8-1-windows-phone-8-1/

    When it comes to Unit Testing the major drawback of using a Shared Project is that you have to unit test your Windows app and Windows Phone app separately. You cannot unit test a shared project separately. For Portable Class Libraries you can write unit tests just to cover the logic within a library. So you will be unit testing a separate module, which is the right approach.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Wednesday, April 16, 2014 1:27 AM
    • Marked as answer by Amanda Zhu Thursday, April 17, 2014 1:32 AM
    Friday, April 11, 2014 9:15 AM

All replies

  • Hi,

    I believe that we can’t unit test a shared project separately. We need to unit test the windows store app and windows phone app separately.

    Please see:

    http://www.jevgeni.net/2014/04/06/shared-projects-portable-class-libraries-windows-8-1-windows-phone-8-1/

    When it comes to Unit Testing the major drawback of using a Shared Project is that you have to unit test your Windows app and Windows Phone app separately. You cannot unit test a shared project separately. For Portable Class Libraries you can write unit tests just to cover the logic within a library. So you will be unit testing a separate module, which is the right approach.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Wednesday, April 16, 2014 1:27 AM
    • Marked as answer by Amanda Zhu Thursday, April 17, 2014 1:32 AM
    Friday, April 11, 2014 9:15 AM
  • I found that it is also not possible to integration test a Windows Phone app which uses logic from a Portable Class Library for win81+wpa81.
    Monday, May 26, 2014 7:31 PM
  • What, then, is the best way to Unit Test code in the Shared project?

    I created a PCL project and moved my code there. In my unit test project I added a reference to the DLL that was built in the PCL project. Since there is a debug dll and a release dll, one must be careful which to reference. So far it seems to be working. 



    • Edited by tnesham Friday, July 11, 2014 3:02 PM Learned some more....
    • Proposed as answer by tnesham Friday, July 11, 2014 3:03 PM
    Sunday, July 6, 2014 11:38 AM
  • Could someone answer this? I tried putting unit tests inside the Shared code but the attributes for [TestClass] and [TestMethod] don't exist. So I added references to "MS Test for Managed Projects" and now the tests show up in the Test Explorer. But when I attempt to run them for Windows (not Phone) I get the error below. I don't mind if my tests take twice as long to run - once for Windows and once for Phone - but if I have to somehow duplicate all the code for the unit tests then that is a non-starter. Is there some way to write unit tests for shared code in a universal project?

    Failed to activate Windows Store app unit test executor. Error: This app does not support the contract specified or is not installed.

    Sunday, September 7, 2014 1:43 AM
  • I've discovered another way to get this working. You can manually make a shared project by copying the *.shproj and *.projitems files from a Universal App shared project. Modify the namespace references and paths in those files (in Notepad) as necessary, and change the project GUID (the last number should be sufficient). Make sure that the root namespace for each of the test projects is the same, just like for a Universal App project.

    You can load this shared project into your solution and reference it from your Windows/WindowsPhone Test projects, and be able to run the shared test code in both contexts!

    Though you may have to manually reference the project in VS2013. To do that, manually add the line <Import Project="..\Namespace.Shared\Namespace.Shared.projitems" Label="Shared" /> to the app-specific test project files just before the "<Import Project="$(MSBuildExtensionsPath)\..." line. VS2015 Preview seems to allow Shared projects to be referenced normally.

    • Proposed as answer by BioTurboNick Wednesday, December 10, 2014 10:36 PM
    • Edited by BioTurboNick Wednesday, December 10, 2014 10:42 PM added info
    Wednesday, December 10, 2014 10:36 PM