Access Windows Form from Visual Basic Unit Test



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a simple Windows Form created in VB.NET application. In the form there is a checkbox that changes the visibility of other controls in the form.



I'm trying to create a Unit Test to verify that this functionality works as expected. However, I have several issues. Let's start with the constructor in my Unit Test where I create an instance of the Windows Form:



Dim myWindow = Nothing
Public Sub New()
myWindow = New APP_NAME.myWindow()
End Sub


That works. I then move to one of the test methods which starts like this:



<TestMethod()> Public Sub myTest()
Dim checkBox = getFormControl(myWindow, "CHECKBOXNAME")
checkBox.Checked = True


As you can see I have a method called getFormControl which returns a desired control from the form. That also works as well as setting the checked value to true.



The first problem is that changing the value does not trigger the checkbox's CheckedChanged event so nothing gets hidden or shown on the screen as this event and the corresponding method handle those toggles. As a result I had to make this method in the form Public instead of Private so that I can call it next like this:



myWindow.CHECKBOXNAME_CheckedChanged(myWindow, EventArgs.Empty)


Method definition in myForm:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged


I would like to trigger the event without having to change the method to Public so any idea on how to do that?



The second problem is that the code in this method goes and changes the Visible property of several other controls. Let's say it looks like this:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged
myControl.Visible = True
MessageBox("Making visible: " & myControl.Name & " - " & myControl.Visible)
End Sub


The myControl which is let's say a Label has Visible equal to False which is set during initialization of the form.



If I start the application, load the form and click on the checkbox, the MessageBox on the screen shows the name of the control and the value TRUE after. Everything works as expected after.



However, if I run the unit test method, the MessageBox shows the name of the control on the screen followed by FALSE. I think that this is due to the fact that in the second case the main thread running the application is that of the test cases and not the application itself. The test case fails after that as I'm expecting the Visible property to be TRUE.



How can I actually access this properly from the unit test? The code is working just fine and as expected if run from the application but I'm having trouble running it from the unit test project.










share|improve this question



















  • 2





    Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

    – Hans Passant
    Nov 15 '18 at 11:55

















0















I have a simple Windows Form created in VB.NET application. In the form there is a checkbox that changes the visibility of other controls in the form.



I'm trying to create a Unit Test to verify that this functionality works as expected. However, I have several issues. Let's start with the constructor in my Unit Test where I create an instance of the Windows Form:



Dim myWindow = Nothing
Public Sub New()
myWindow = New APP_NAME.myWindow()
End Sub


That works. I then move to one of the test methods which starts like this:



<TestMethod()> Public Sub myTest()
Dim checkBox = getFormControl(myWindow, "CHECKBOXNAME")
checkBox.Checked = True


As you can see I have a method called getFormControl which returns a desired control from the form. That also works as well as setting the checked value to true.



The first problem is that changing the value does not trigger the checkbox's CheckedChanged event so nothing gets hidden or shown on the screen as this event and the corresponding method handle those toggles. As a result I had to make this method in the form Public instead of Private so that I can call it next like this:



myWindow.CHECKBOXNAME_CheckedChanged(myWindow, EventArgs.Empty)


Method definition in myForm:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged


I would like to trigger the event without having to change the method to Public so any idea on how to do that?



The second problem is that the code in this method goes and changes the Visible property of several other controls. Let's say it looks like this:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged
myControl.Visible = True
MessageBox("Making visible: " & myControl.Name & " - " & myControl.Visible)
End Sub


The myControl which is let's say a Label has Visible equal to False which is set during initialization of the form.



If I start the application, load the form and click on the checkbox, the MessageBox on the screen shows the name of the control and the value TRUE after. Everything works as expected after.



However, if I run the unit test method, the MessageBox shows the name of the control on the screen followed by FALSE. I think that this is due to the fact that in the second case the main thread running the application is that of the test cases and not the application itself. The test case fails after that as I'm expecting the Visible property to be TRUE.



How can I actually access this properly from the unit test? The code is working just fine and as expected if run from the application but I'm having trouble running it from the unit test project.










share|improve this question



















  • 2





    Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

    – Hans Passant
    Nov 15 '18 at 11:55













0












0








0








I have a simple Windows Form created in VB.NET application. In the form there is a checkbox that changes the visibility of other controls in the form.



I'm trying to create a Unit Test to verify that this functionality works as expected. However, I have several issues. Let's start with the constructor in my Unit Test where I create an instance of the Windows Form:



Dim myWindow = Nothing
Public Sub New()
myWindow = New APP_NAME.myWindow()
End Sub


That works. I then move to one of the test methods which starts like this:



<TestMethod()> Public Sub myTest()
Dim checkBox = getFormControl(myWindow, "CHECKBOXNAME")
checkBox.Checked = True


As you can see I have a method called getFormControl which returns a desired control from the form. That also works as well as setting the checked value to true.



The first problem is that changing the value does not trigger the checkbox's CheckedChanged event so nothing gets hidden or shown on the screen as this event and the corresponding method handle those toggles. As a result I had to make this method in the form Public instead of Private so that I can call it next like this:



myWindow.CHECKBOXNAME_CheckedChanged(myWindow, EventArgs.Empty)


Method definition in myForm:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged


I would like to trigger the event without having to change the method to Public so any idea on how to do that?



The second problem is that the code in this method goes and changes the Visible property of several other controls. Let's say it looks like this:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged
myControl.Visible = True
MessageBox("Making visible: " & myControl.Name & " - " & myControl.Visible)
End Sub


The myControl which is let's say a Label has Visible equal to False which is set during initialization of the form.



If I start the application, load the form and click on the checkbox, the MessageBox on the screen shows the name of the control and the value TRUE after. Everything works as expected after.



However, if I run the unit test method, the MessageBox shows the name of the control on the screen followed by FALSE. I think that this is due to the fact that in the second case the main thread running the application is that of the test cases and not the application itself. The test case fails after that as I'm expecting the Visible property to be TRUE.



How can I actually access this properly from the unit test? The code is working just fine and as expected if run from the application but I'm having trouble running it from the unit test project.










share|improve this question
















I have a simple Windows Form created in VB.NET application. In the form there is a checkbox that changes the visibility of other controls in the form.



I'm trying to create a Unit Test to verify that this functionality works as expected. However, I have several issues. Let's start with the constructor in my Unit Test where I create an instance of the Windows Form:



Dim myWindow = Nothing
Public Sub New()
myWindow = New APP_NAME.myWindow()
End Sub


That works. I then move to one of the test methods which starts like this:



<TestMethod()> Public Sub myTest()
Dim checkBox = getFormControl(myWindow, "CHECKBOXNAME")
checkBox.Checked = True


As you can see I have a method called getFormControl which returns a desired control from the form. That also works as well as setting the checked value to true.



The first problem is that changing the value does not trigger the checkbox's CheckedChanged event so nothing gets hidden or shown on the screen as this event and the corresponding method handle those toggles. As a result I had to make this method in the form Public instead of Private so that I can call it next like this:



myWindow.CHECKBOXNAME_CheckedChanged(myWindow, EventArgs.Empty)


Method definition in myForm:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged


I would like to trigger the event without having to change the method to Public so any idea on how to do that?



The second problem is that the code in this method goes and changes the Visible property of several other controls. Let's say it looks like this:



Public Sub CHECKBOXNAME_CheckedChanged(sender As Object, e As EventArgs) Handles CHECKBOXNAME.CheckedChanged
myControl.Visible = True
MessageBox("Making visible: " & myControl.Name & " - " & myControl.Visible)
End Sub


The myControl which is let's say a Label has Visible equal to False which is set during initialization of the form.



If I start the application, load the form and click on the checkbox, the MessageBox on the screen shows the name of the control and the value TRUE after. Everything works as expected after.



However, if I run the unit test method, the MessageBox shows the name of the control on the screen followed by FALSE. I think that this is due to the fact that in the second case the main thread running the application is that of the test cases and not the application itself. The test case fails after that as I'm expecting the Visible property to be TRUE.



How can I actually access this properly from the unit test? The code is working just fine and as expected if run from the application but I'm having trouble running it from the unit test project.







vb.net unit-testing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:38







mmvsbg

















asked Nov 15 '18 at 11:19









mmvsbgmmvsbg

2,117153959




2,117153959







  • 2





    Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

    – Hans Passant
    Nov 15 '18 at 11:55












  • 2





    Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

    – Hans Passant
    Nov 15 '18 at 11:55







2




2





Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

– Hans Passant
Nov 15 '18 at 11:55





Unit-testing a GUI app is not that simple. You are almost surely missing the Application.Run() call. It is crucial to let an UI thread do what it needs to do. Google "vb.net coded ui test" for likely to be useful hits. And focus a bit on what you really want to test. It is not like you can file a bug anywhere when the Visible property doesn't work.

– Hans Passant
Nov 15 '18 at 11:55












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318318%2faccess-windows-form-from-visual-basic-unit-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318318%2faccess-windows-form-from-visual-basic-unit-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo