1

I have a standard WPF application. The VS solution has a main project and a unit test project (using the standard VS unit test suite)

For testing purposes beyond unit tests (e.g. just interactively trying out a new piece of code) I have to wait for my whole application to start and then click my way to the form I want to test.

I'd prefer if I could choose an alternative build with a different entry point, which skips all the stuff I do not need and drops me off right where I want to. I have created an additional build configuration besides "debug" and "release" called "debug (test)". How do I set different startup object that is executed when I choose this build?

Or is there an alternative solution for this problem? To simplify testing and profiling I'd prefer something that does not involve adding an additional project or dll.

One possible solution would be to define a compiler symbol and check for it in my app.xaml.cs

#if DEBUG_TEST
    TestWindow view = new TestWindow();
    view.Show();
#else
    MainWindow view = new MainWindow();
    view.Show();
#endif

anything less hacky?

HugoRune
  • 13,157
  • 7
  • 69
  • 144

2 Answers2

2

I don't know why you consider this approach 'hacky'.
This seems to be the right way to go.
Just select your configuration DEBUG(TEST) and add the DEBUG_TEST define to the properties page of your project.

Return to normal configuration when you have done with your tests.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • That solution only occurred to me while writing the question, but to me it seems weird to have a part of the test code in the same file as the normal code instead of simply having another app2.xaml.cs. I guess I can do it this way anyway. – HugoRune May 23 '12 at 11:46
2

An alternative approach would be Dependency Injection: http://www.martinfowler.com/articles/injection.html

Let's say you use this code:

IMainView view = DIContainer.GetInstance<IMainView>();
view.Show();

In your Main project you configure your Dependency Injection Container to use MainWindows

DIContainer.SetDefaultInstance<IMainView, MainView>();

and in your test project

DIContainer.SetDefaultInstance<IMainView, TestView>();

MainView and TestView Need to implement

interface IMainView
{
     void Show();
}

This simple approach can be done by yourself (use Activator.CreateInstance<T> in GetInstance) but there are a couple of frameworks out there that take care of that.

Which .NET Dependency Injection frameworks are worth looking into?

Community
  • 1
  • 1
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 1
    in the end I went with the simple compiler symbol approach, but dependency injection seems like something I should definitely look into, thanks for the info! – HugoRune May 26 '12 at 20:53