开发者

C# Unit Test Case Help

开发者 https://www.devze.com 2023-03-16 14:08 出处:网络
I am learning C# (coming from Java background) and am trying to implement test cases in my practice.I have been through a few examples of creating test cases but they were very basic and dealt with ob

I am learning C# (coming from Java background) and am trying to implement test cases in my practice. I have been through a few examples of creating test cases but they were very basic and dealt with obvious constraints (i.e., angle is between 0-360).

I am going thru examples of creating forms and have not been able to find any examples on implementing tests with the forms. Is this something that is usually done and if so could someone give me a few examples of what I would test for int he following example:

namespace TabControlExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CreateNewCompanyTab("Test Company 1");
            CreateNewCompanyTab("Test Company 2");

        }

        private void CreateNewCompanyTab(string companyName)
        {
            BaseCompanyPanel companyPanel = new BaseCompanyPanel();
            TabPage tabPage = new TabPage();

            // Set the tab text
            tabPage.Text = companyName;

            // Set the companyPanel.Parent value to tabPage
            // this will basically insert the companyPanel into the tab
            companyPanel.Parent = tabPage;
            tabControl.TabPages.Add(tabPag开发者_JAVA技巧e);

        }


    }
}


namespace TabControlExample
{
    public partial class BaseCompanyPanel : UserControl
    {
        public BaseCompanyPanel()
        {
            InitializeComponent();
        }
    }
}

This example creates a user control that can be reused on multiple tabs.

Any help is appreciated.

TIA,

Brian Enderle


You should have your business logic separate from your UI. If you can, you may want to explore using WPF and MVVM.


FWIW, I wouldn't say you should be unit-testing forms, because they are tightly coupled to the underlying Windows Forms framework.

However, unit testing can motivate/force you to keep as much as possible of the logic off the forms, so you can test those pieces of code.

If you are trying to create unit tests for code that references Windows Forms in turn, you can use a mocking framework such as Moq to moq the windows forms referenced by the code under test. This in turn requires you to inject all those dependencies.

0

精彩评论

暂无评论...
验证码 换一张
取 消