none
Open Windows form in unit testing method RRS feed

  • Question

  • Hi,

    In MS unit test or NUnit unit test project, How to open a windows form in Test methods.

    I want to open a windows form and enter the inputs in the Form.

    Based on input values entered, I want to validate the relevant data and show the test results

    If this not possible, can we executed the unit test methods from windows application?


    Saturday, May 11, 2019 9:24 PM

All replies

  • MS unit tests and NUint are for testing your code.   You would create a test method which would call your code and verify what you expect back is what you get back.

    https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest

    If you want to test a windows form you would need to look at the automated UI tests

    https://docs.microsoft.com/en-us/visualstudio/test/use-ui-automation-to-test-your-code?view=vs-2019

     
    Sunday, May 12, 2019 1:10 AM
  • Hi Kumar Jalli,

    Sorry for delay in reply.

    According to your description, I find a appropriate method from this answer. Please follow the steps below:

    At first, the unit test project and winform project should be in the one solution

    1,In winform project, open winform properties window, then change the ShowInTaskbar to false.

    2, Set your windows controls and methods as public in the code 

    3, Back to unit test project, add the winform project as reference.

       right click the Reference>> select the Add Reference.. >>select Projects in the left>>select solution>> choose the winform project in the right window

    4, Enter the sample code below

    WindowsFormsApp1.Form1 box = new WindowsFormsApp1.Form1();
       
    box.textBox1.Text = "test";
    box.ShowDialog();

    Then it will display when the method running.

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Monday, May 13, 2019 6:25 AM