How do i do a unit test on a button click

Posted on 2008-11-07
Last Modified: 2012-05-05
Hi all I am trying to do unit tests on a button click event. and i cant figure it out for the life of me.
///A test for button1_Click (object, EventArgs)


        [DeploymentItem("Hotel Rate Calculator.exe")]


        public void button1_ClickTest()


            Form1 target = new Form1();

            TestProject2.Hotel_Rate_Calculator_Form1Accessor accessor = new TestProject2.Hotel_Rate_Calculator_Form1Accessor(target);

            object sender = null; // TODO: Initialize to an appropriate value

            EventArgs e = null; // TODO: Initialize to an appropriate value

            accessor.button1_Click(sender, e);

            Assert.Inconclusive("A method that does not return a value cannot be verified.");



Open in new window

Question by:HawaiiDragon
    1 Comment
    LVL 27

    Accepted Solution

    Hello HawaiiDragon,

    It does not make sense to unit test UI functionality.
    It makes sense to unit test the code that the UI calls.

    Hence, if your button1_Click code looks like this:

    private void button1_Click()
     //Your code to calculate rates

    Consider moving the calculation section to a method:

    private void button1_Click()

    public void CalledMethod()
    //Your code to calculate rates

    Now you can call CalledMethod() from your unit test, and compare what you expect to calculate with the actual results.



    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    If you want to move up through the ranks in your technology career, talent and hard work are the bare necessities. But they aren’t enough to make you stand out. Expanding your skills, actively promoting your accomplishments and using promotion st…
    Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
    Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
    Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now