Is it possible to write Prism apps such that they can be tested without the UI?

Topics: Prism v4 - WPF 4
Dec 15, 2011 at 11:24 AM
Edited Dec 15, 2011 at 11:26 AM

Here's the Stackoverflow question which may help with what I am getting at

Q#1 Does prism offer any support for presenter-first style of writing applications ?

Q#2 If not, is there a way in which I can stub out the UI dependent part of Prism but still maintain all the dynamic composition ?

Disclaimer: Noob but fast learner.. drilling through the Prism v4 manual. Chap7 at this point in time.

I am trying to write system level acceptance tests that work at a layer below the UI (i.e. the presenter). I hit a hurdle with Prism here because with the application that I am working with - there is no single composition root.

The composition happens in a declarative manner. When the UI shows up, Prism finds regions. It looks up the corresponding  registered views for the regions and instantiates them. DI pulls in the presenter dependencies for these views and so on down ...

This poses a problem in that I cannot construct the presenter object graph without PRISM or to be more precise the UI. If there is no UI.. the composition won't happen.

The top-answer on the SO post suggests renouncing "Prism regions" and handling composition manually to achieve this. Just want an official (preferable) confirmation one way or the other.






Dec 15, 2011 at 8:40 PM
Edited Dec 15, 2011 at 8:42 PM


Based on my understanding the Composite Application Library supports both types of composition. Regardless of the specific separated presentation pattern you use, your application can create and configure the view, presenter, and model components in a number of ways depending on your decision.

You can find more information about this in the following links:

I believe you could use the ServiceLocator to retrieve view models/presenters and then unit test them according to the requirements of your scenario.

On the other hand, in the following discussion, you can find a case that uses the view model first approach in Prism:

Also, the Prism Library source code, Quickstarts and RI includes unit and UI automation tests examples, that can be used as reference.

I hope you find this handy

Agustin Adami

Dec 26, 2011 at 9:21 AM

I guess I should have emphasized the fact that I am looking at acceptance / system tests (NOT unit tests). I need the entire app with some predefined set of actions executed (setup) ; all the ViewModels created and linked up correctly but **without the UI**.

So I need all the viewmodels that would exist at that point if I was manually exercising the UI.

Among the links that you posted, there is a item with some potential (that you can add viewModels to a region and use DataTemplates to render the view). Still reading the Prism docs.. will dig into this bullet and let you know.

Dec 29, 2011 at 9:14 AM
Edited Dec 29, 2011 at 9:15 AM

Alright I just ran through the Prism guidance and my question still remains...

Can Prism still handle recursive view composition (based on "regions") if the UI is never shown ? I want to write acceptance tests that talk to ViewModels (under the UI tests) ; UI Tests are out of proc and sometimes flaky. I'd like to write tests that talk to VM classes instead - more reliable and less work.

I didn't see anything that resembled ViewModel first : view discovery is view-first. View injection is still code that injects specific views into named regions ; the regions themselves are instantiated only if the UI is shown. If the UI is suppressed somehow, the regions would not be created and exceptions would be raised.

I tried to add ViewModels into regions instead of views + use DataTemplates to locate the corresponding view. This works for one level of composition. If the corresponding view for level-1 subview has 2 child regions, I'm not able to handle that generically. Is it somehow possible to Recursively discover regions without the views ? Or get some kind of hook-point where I can intercept and bypass the actual showing of the UI ?

RFC. Please feel free to tell me how wrong I am :)

Dec 29, 2011 at 8:43 PM
Edited Dec 29, 2011 at 8:54 PM

Hi Gishu,

As far as I know, Prism does not provide guidance for the scenario of performing acceptance test without composing the UI. Based on my understanding, the UI Composition capabilities provided by Prism out of the box, such as Regions and RegionBehaviors, are designed to compose the visual aspect of the application. In other words, the UI Composition functionality is not designed to be used without an user interface to compose. Also, the usual approach regarding UI Composition is to define Regions in the views, so if no view is created, the Regions defined in those views are not created either.

Regarding this, Prism provides guidance on acceptance testing and includes an AcceptanceTestLibrary, which is used to perform acceptance testing on applications (such as the Stock Trader RI) showing and interacting with the UI.

As a possible approach, the RegionManager implementation provided by Prism allows to register a Region in code; however, to do this without an existing UI, you might need to create a Control that can be defined as a region, register that control as a Region and attach the RegionManager to the control as a dependecy property (this is because, as the control won't be part of a UI, it will not have a parent and thus, it won't be able to obtain the corresponding RegionManager automatically). Also, as most applications define the Regions in the views, you will have to design a method to parse the XAML code of a view you want to "simulate" in order to obtain the Regions contained in it and the corresponding control type that was defined as a Region. Taking all of this into account, it might be possible to "simulate" the composition of a user interface; however, currently there is no guidance about this and you could find several unexpected undesired behaviors in such scenario.

Also, take into account that, as mentioned in this MSDN article, the most common form of acceptance testing consists of tests that exercise a user scenario from the user interface, as these tests emulate the application keyboard and user interface interactions.


Damian Cherubini

Dec 30, 2011 at 10:42 AM

Thanks Agustin and Damian.

My thoughts exactly. It seems 'simulating Prism' is  too much effort + likely to not work out in the end ; since it seems to be 'going against the grain'. UI Composition seems to be a strong underlying concept of the the Prism way.

I'm struggling to find a definition for Presenter-first. But my internal association was

* presenters are composed first

* Views are attached later (e.g. locate view for ViewModelX, create, set view.datacontext = viewmodel

That opens the door to simplified unit as well as system testing. You can just peel away the UI and do under the skin tests that do all the user-interaction at the ViewModel level. No need to write UI automation code or test stabilization delays (effort spent to get UI tests to behave predictably. UI tests are notoriously moody.. stops running after some update or behaves differently on a specific OS). In contrast POCOs are a god-send.

Once again, thank you for sharing your inputs..