Festivejelly
Festivejelly

Reputation: 690

How do I interface with my WinForms application from a UnitTest

I have built a WinForms application which consists of just a RichTextBox and ive built it into an EXE.

The purpose of this EXE is to act as a logger so that when im running tests the log is output onto this EXE.

Important note is that this EXE is referenced in a separate Unit Test (to be run by Microsoft Test Manager) solution by adding the EXE as a reference in the project. This seems to expose the objects I need.

I've had some success using HTTP through the ChannelFactory interfaces, but i'd prefer to talk to the EXE directly.

These unit tests I have are essentially sending and receiving data from OpenVMS, and some of these tests can take some time to complete.

So I built a new Console project to test the public methods I've exposed in my logger.exe and so far heres my code:

TerminalLogger.Logger term = new TerminalLogger.Logger();
term.TerminalLog("Test");

When I run this the form opens, but nothing loads in. So im assuming thats because the form runs on the same thread? Do I need to have this form running on its own thread?

I notice that if I add "term" to watch in Visual Studio and inspect the richtextbox I can see that it has actually written the value "Test" to the object, but of course I cannot see this as the form hasnt fully rendered in.

I still need to be able to call methods like term.LogMessage("Example Message") and get it to display on the form.

If you need extra info please add a comment and i'll do my best to elaborate more on my question.

Upvotes: 0

Views: 84

Answers (1)

Festivejelly
Festivejelly

Reputation: 690

Well I asked one of the web developers at my organisation on the offchance they might be able to help, and apparently calling a redraw fixed my issue!.

I simply added:

        LogConsole.TextChanged += (sender, e) =>
        {
            if (this.Visible)
            {
                this.Refresh();
            }
        };

Here is how I call the code from my unit test:

TerminalLogger.Logger term = new TerminalLogger.Logger();

        term.Show();
        term.TerminalLog("Test1");
        term.TerminalLog("Test2");

And I can send messages to it without it locking up.

Upvotes: 0

Related Questions