Note: In this post I just described a way to enable discovery and execution of tests in a WinRT Component DLL. This is not a recommended or supported way to write unit test for WinRT Component. Recommended way is to create a separate 'Unit Test Library' project. Hence, side effects of adding manifest file etc. in project are not analyzed. Please use this info at your own risk only if you don't have any other option.

 

Typically developers should test public APIs of components. Recommended way of testing "WinRT Component DLL" is to add "Unit Test Library" project from "Visual C++" -> "Windows Metro style", take reference of WinRT Componenet DLL and test public APIs.

 

Following steps can enable discovery and execution of tests in WinRT Component DLL and is only for cases where you don't have any option but to access non public APIs from WinRT Component DLL for testing.

  1. Add new WinRT Component DLL project
    1. File -> New -> Project...
    2. Visual C++ -> Windows Metro style -> WinRT Component DLL
  2. Add Test SDK references
    1. Right click on project from Solution Explorer -> References ... -> Common Properties -> Framework and References
    2. Add New Reference ... -> Windows -> Extensions
    3. Select "Microsoft Visual Studio Test Core" and "MSTest for Native Projects"
  3. Add Test Include Directory
    1. Right click on project from Solution Explorer -> Properties ... -> Configuration Properties -> C/C++
    2. Change "Additional Include Directories" to $(ProjectDir);$(IntermediateOutputPath);$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)
  4. Add Test Library Directory
    1. Right click on project from Solution Explorer -> Properties ... -> Configuration Properties -> Linker
    2. Change "Additional Library Directories" to $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
  5. Add required Property in vcxproj
    1. Right click on project from Solution Explorer -> Unload Project
    2. Right click on project from Solution Explorer -> Edit '...vcxproj'
    3. Add following property to vcxproj  file: <PropertyGroup><AppxPackage>true</AppxPackage></PropertyGroup>
    4. Close vcxproj file
    5. Right click on project from Solution Explorer -> Reload Project
  6. Add package.appxmanifest
    1. Add package.appxmanifest from a unit test library project to current project.
  7. Add images
    1. Add images folder from a unit test library project and add them to project with relative path "Images\<name>.png"
  8. In cpp file where adding tests add

    #include CppUnitTest.h"
    using namespace Microsoft::VisualStudio::CppUnitTestFramework;

  9. Add tests in cpp file\

    TEST_CLASS(TestClass1)
    {
        TEST_METHOD(TestMethod1)
        {
            Assert::Fail(L"Failing Test");
        }

        TEST_METHOD(TestMethod2)
        {
            Assert::AreEqual(1, 1);
        }
    };

  10. Run test
    1. Compile
    2. Tests should get discover
    3. Execute tests

Effectively your WinRTComponent DLL will be a unit test library also.

 

Update: Marcelo has found a way to test WinRT internal class which avoids adding package.appxmanifest file & editing vcxproj. Steps are described in his blog http://marcecaro.blogspot.com/2012/04/unit-testing-private-classes-using-ccx.html

 

If you've any feedback or issue then please do send us on Connect or in the Forums.