locked
[UWP][C++] Suggested way to unit test existing Win 10 Project? RRS feed

  • Question

  • Hello,

    I've been stuck trying to get unit tests up and running. I can create a Universal Unit Test App and run simple tests, but what is the expected and suggested way to integrate this into a larger UWP project?

    I tried to add a reference of my main project to the unit test project, and then add all the appropriate #include directories. This gets the code compiling appropriately, but every time I try to instantiate an object from my main project, I get the error :

    Unhandled WinRT Exception : Platform::ClassNotRegisteredException
    Please check if test is taking reference from an Application. In-process references to applications are not supported in tests.

    What is the expected way to test code in an existing project?
    Thank you for your help,
    ~Kyler


    • Edited by Kyler Mulherin Friday, October 21, 2016 3:19 AM formatting
    Friday, October 21, 2016 3:15 AM

Answers

  • For a long time, I was searching for an answer that did not involve a heavy code refactor. Unfortunately, I could not find one. 

    The simple fact of the matter is that Unit Test Apps cannot test code that is in a Universal Windows App project.

    The solution that I pursued was to migrate chunks of code out of my App project and into specialized WinRT Component projects, and then refactor the App project to point to the new namespaces.

    These WinRT Components can be tested by Unit Test Apps but there were a number of unforseen "gotchas" that I had to deal with when moving my code over.

    • User created ref classes do not support inheritance. All public constructors to ref classes must be sealed (thanks JavaScript), and parent classes cannot have less visibility than their child classes. This makes using polymorphism much more difficult.
      One ugly work around for this is to have your unsealed parent classes inherit from Windows::UI::Xaml::DependencyObject .
    • Language specific native variable types are not supported when trying to make public functions that communicate outside the WinRT component. All of my int's had to be converted to int32's and all my bool's had to be converted to booleans. https://msdn.microsoft.com/en-us/library/windows/desktop/br205768(v=vs.85).aspx was enormously helpful when making the conversion to C++/CX compatible types.
      This came back to bite me because I was passing lambdas as std::functions as parameters for my function callbacks. All of these functions had to be wrapped in explicitly defined public delegate functions.

    Being new to Windows App development, I wish I had known that this was an appropriate way to write testable code.

    • Marked as answer by Kyler Mulherin Wednesday, December 7, 2016 12:55 AM
    Wednesday, December 7, 2016 12:55 AM

All replies

  • Hi Kyler,

    In my side, I have created a Unit Test for Universal Windows, and added Blank App (Universal Windows) project and DLL (Universal Windows) project to the Solution Explorer. I have added a test method to Blank App and DLL project, but I could not call these methods in my Unit Test project.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Thanks for your understanding!

    Sincerely,

    Oscar


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 24, 2016 9:14 AM
  • Hello Oscar,

    Have there been any new developments? 

    Thank you for your continued assistance!

    ~Kyler

    Monday, October 31, 2016 9:36 PM
  • For a long time, I was searching for an answer that did not involve a heavy code refactor. Unfortunately, I could not find one. 

    The simple fact of the matter is that Unit Test Apps cannot test code that is in a Universal Windows App project.

    The solution that I pursued was to migrate chunks of code out of my App project and into specialized WinRT Component projects, and then refactor the App project to point to the new namespaces.

    These WinRT Components can be tested by Unit Test Apps but there were a number of unforseen "gotchas" that I had to deal with when moving my code over.

    • User created ref classes do not support inheritance. All public constructors to ref classes must be sealed (thanks JavaScript), and parent classes cannot have less visibility than their child classes. This makes using polymorphism much more difficult.
      One ugly work around for this is to have your unsealed parent classes inherit from Windows::UI::Xaml::DependencyObject .
    • Language specific native variable types are not supported when trying to make public functions that communicate outside the WinRT component. All of my int's had to be converted to int32's and all my bool's had to be converted to booleans. https://msdn.microsoft.com/en-us/library/windows/desktop/br205768(v=vs.85).aspx was enormously helpful when making the conversion to C++/CX compatible types.
      This came back to bite me because I was passing lambdas as std::functions as parameters for my function callbacks. All of these functions had to be wrapped in explicitly defined public delegate functions.

    Being new to Windows App development, I wish I had known that this was an appropriate way to write testable code.

    • Marked as answer by Kyler Mulherin Wednesday, December 7, 2016 12:55 AM
    Wednesday, December 7, 2016 12:55 AM