How to use unit testings on forms? RRS feed

  • Question

  • I have a form I want to create a unit testing for a Form. I want to demonstrate user click by calling the method that is activated OnClick. I don't even need to display the form itself. 

    The problem is that after I create a new Form in my tester Method, I am unable to access the OnClick function (even tho it access modifier is "public") , because I get the error: "The type 'Form' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

    I don't think I am approaching it in the right way, should I add a reference? But I don't want to even display the Form, all I want is to call the function. What is the right approach? how does unit test usually conducted on Forms?

    Thank you in advance!!

    • Edited by avivgood Wednesday, October 23, 2019 7:41 PM
    Wednesday, October 23, 2019 7:41 PM


  • Hi avivgood,

    Welcome to MSDN forum.

    Yes, you should add "System.Windows.Forms.dll" into your project. The onclick method is a component of control class, and the class is under "System.Window.Forms" namespace.

    This namesapce contains classes for creating Windows-based applications that take full advantage of the rich user interface features available in the Microsoft Windows operating system. And display feature is just one component other than all.

    Hope it could help you.

    Best Regards,


    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

    • Marked as answer by avivgood Thursday, October 24, 2019 2:07 PM
    Thursday, October 24, 2019 3:23 AM