Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Windows Forms inside unit test methods

I just discovered unit test projects in Visual Studio .NET and am using test methods to set up examples of global code I have developed.

Some global methods involve displaying reusable dialogs like a date-picker and input box. Trouble is, sometimes forms will display and sometimes they won't.

I thought it was related to modality, because I have a report preview form that can be shown modally or non-modally. When I show that non-modally, it does not display. When I show it modally, it does.

Trying my input box never works:

string input = "";
using (InputBox box = new InputBox(Title, Prompt, Default))
{
    DialogResult result = box.ShowDialog();
    input = box.txtInput.Text.Trim();
}
return input;

Execution stops at the "box.ShowDialog()" line, and at that point I can inspect box and see that its dimensions, location, and visibility are all properly configured -- yet I cannot see the form. I've got to cancel the test to stop everything.

I'd love to use a unit testing project to act as a playground and showcase of existing code, but it seems very limited if I can't display certain forms. I realize this is not really what unit testing is meant for, but I hoped I could build a fun little sandbox this way to help get my developers up to speed.

like image 826
sutekh137 Avatar asked Jan 13 '16 21:01

sutekh137


People also ask

How do you display a form in C #?

Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.

Which event occurs before the form is displayed on the screen?

Load: This event occurs before a form is displayed for the first time. VisibleChanged: This event occurs when the Visible property value changes. Activated: This event occurs when the form is activated in code or by the user.

How do you check if a Windows Form is open or not?

Just call Show method on the instance. You can check Form. Visible property to check if the form open at the moment.

What would you use to display an image on a Windows Form?

With the Windows Forms PictureBox control, you can load and display a picture on a form at design time by setting the Image property to a valid picture.


1 Answers

I finally had some consistent success (and lack thereof) based on a single form property: ShowInTaskbar.

When a form had that property set to true, such forms would NOT display from a unit test method. When that property is false, all forms display. So, here are the rules as far as I know them to make sure a form can display from a unit test:

  • The form should be created as a standard Windows Form item in the project.
  • The form should have its ShowInTaskbar property set to FALSE.
  • The form needs to be displayed modally (i.e. with ShowDialog()).

This has let me display and test all of my utility forms like date selectors and login screens.

like image 95
sutekh137 Avatar answered Sep 24 '22 13:09

sutekh137