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.
- Add new WinRT Component DLL project
- File -> New -> Project…
- Visual C++ -> Windows Metro style -> WinRT Component DLL
- Add Test SDK references
- Right click on project from Solution Explorer -> References … -> Common Properties -> Framework and References
- Add New Reference … -> Windows -> Extensions
- Select “Microsoft Visual Studio Test Core” and “MSTest for Native Projects”
- Add Test Include Directory
- Right click on project from Solution Explorer -> Properties … -> Configuration Properties -> C/C++
- Change “Additional Include Directories” to $(ProjectDir);$(IntermediateOutputPath);$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)
- Add Test Library Directory
- Right click on project from Solution Explorer -> Properties … -> Configuration Properties -> Linker
- Change “Additional Library Directories” to $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
- Add required Property in vcxproj
- Right click on project from Solution Explorer -> Unload Project
- Right click on project from Solution Explorer -> Edit ‘…vcxproj’
- Add following property to vcxproj file: <PropertyGroup><AppxPackage>true</AppxPackage></PropertyGroup>
- Close vcxproj file
- Right click on project from Solution Explorer -> Reload Project
- Add package.appxmanifest
- Add package.appxmanifest from a unit test library project to current project.
- Add images
- Add images folder from a unit test library project and add them to project with relative path “Images\<name>.png”
- In cpp file where adding tests add
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
- Add tests in cpp file\
- Run test
- Tests should get discover
- 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