Felipe Castro
Felipe Castro

Reputation: 39

Is it possible to display a windows Form from within unit tests?

So i'm writing this computer vision application in C# using opencv, Emgu and visual studio.

I'm trying to write a few unit tests using the built in unit tests from Microsoft and I'm starting to think that I'm trying to do something really rare here because I simply can't find the answer anywhere on the internet.

The application is a console application because it won't have a user interface but I'd like to display a few debug images while I'm coding it. I have created a Form using the designer which has a few PictureBox in it.

I simply would like to be able to do something like:

DebugViewer debugViewer = new DebugViewer();
debugViewer.SetPicture(debugImage);
debugViewer.Show();

Where DebugViewer is the Form I created and SetPicture simply updates the image of a PictureBox.

The problem is that the form never shows up. Nothing appears in the taskbar even with the ShowInTaskbar property set.

I tried the same code running from a main function and it worked correctly. The problem seems to be with trying to run this from a unit test.

I also successfully displayed images using opencv imShow function even from the unit tests so I'm certain it is possible to open windows from unit tests but I don't know why the Forms don't show up.

I realize opening debug windows from unit test doesn't make much sense but since I've lost an entire afternoon trying to solve this I thought I should at least satisfy my curiosity.

Thanks a lot.

Upvotes: 3

Views: 882

Answers (3)

mBardos
mBardos

Reputation: 3067

For anyone stumbling on this, there is a solution on this Stack Overflow question (standard Windows Form with ShowInTaskbar property set to FALSE displayed with ShowDialog()).

Additionally to that response, I created the form and called its ShowDialog() on a new thread, so my unit test can run in parallel and add data to be displayed in the window (when debugging the test) with the Form's Invoke method.

Upvotes: -1

Simon Ldj
Simon Ldj

Reputation: 31

I think you are trying to use the wrong tool for your problem... Dealing with GUI just for debug purpose can be frustrating...

Check out HypnoLog, an open source tool I developed to help me debug image processing application I been writing using C# and EmguCV.

With HypnoLog you can see the images as output in your browser, which acting as output log.

In your case you should check HypnoLog-CSharp, and there is already a simple visualizer for images. Your code will look something like this:

HL.log(debugImage, "image");

note, debugImage should be image encoded in base64, see ImageVisualizer for more information.

Upvotes: 1

sutekh137
sutekh137

Reputation: 691

Is your debugViewer form shown modally or non-modally? I was just playing with unit testing and trying to display a report preview form I have. When I display the form modally, it works. When I display it non-modally, nothing displays.

I imagine that displaying a form non-modally, where execution continues, takes everything out of scope as the unit test ends. That means the form does not display. Or, perhaps the unit testing code knows it can't display anything unless execution stops and never even displays it? Not sure. So far, though, I have been able to make several different forms pop up inside unit test methods.

(I am sure this is not a very good answer, but I don't have the points to simply comment yet...)

EDIT: I have run into modal forms that also do not display, and I cannot figure out why. I can debug the test, and when it comes to the line where my form is being displayed with ShowDialog(), execution pauses but no form displays. I can even inspect the form in the debug window and see that its dimensions are correct and Visible = true -- still nothing. So, I'm stumped.

Upvotes: 0

Related Questions