WPF Component Resources during Automated Test WPF Component Resources during Automated Test wpf wpf

WPF Component Resources during Automated Test


This isn't actually all that hard you just need to use Application.LoadComponent to create instances of everything so that the right resources are available at the right time.

The key is to load everything via its XAML rather than creating instances of the classes as the class only contains half the information.

[TestClass]public class View_Test{    [TestMethod]    public void Test()    {        //set initial ResourceAssembly so we can load the App        Application.ResourceAssembly = Assembly.GetAssembly(typeof (App));        //load app        var app = (App) Application.LoadComponent(new Uri("App.xaml", UriKind.Relative));        //load window and assign to app        var mainWindow = (Window) Application.LoadComponent(new Uri("MainWindow.xaml", UriKind.Relative));        app.MainWindow = mainWindow;        //load view and assign to window content        var view = (UserControl) Application.LoadComponent(new Uri("View.xaml", UriKind.Relative));        mainWindow.Content = view;        //show the window        mainWindow.Show();    }}

Edit: Simpler Version

I just had a look at some disassembled code to see how its done internally and this can be simplified to not require the XAML references. The most important bits to get things going are setting Application.ResourceAssembly and creating the App and calling InitializeComponent on it. The window isn't necessary specifically you could just create a new window to hold the view.

[TestClass]public class View_Test{    [TestMethod]    public void Test()    {        Application.ResourceAssembly = Assembly.GetAssembly(typeof (App));        var app = new App();        app.InitializeComponent();        var mainWindow = new MainWindow();        app.MainWindow = mainWindow;        var view = new View();        mainWindow.Content = view;        mainWindow.Show();    }}


What you're after here is a UI test. Microsoft's Coded UI framework would be your best bet. Taking a screenshot of your application is not going to be a reliable way of determining that it's working properly.

With a Coded UI test, you can record actions against your UI and add assertions to verify that the UI is behaving properly. When you execute the test, it actually launches the application and plays back the actions you recorded to get the application to the expected state. It's generally pretty painless to get started with.

Naturally, UI tests are best used sparingly since they're a lot slower. A reasonable unit test should take a few milliseconds to run, but pretty much any UI test is going to take several orders of magnitude longer.


The system doesn't care where it finds the needed resources. They need to be found somewhere up the visual tree. That's why its ok to add the styles to the window instead the Application.

Having this in mind I'd simply load the style.xaml using the XAMLReader which results in a ResourceDictionary object. Then assign/add this resource to your Window object.