Build
1. decompress the file to any directory.
2. Run test. bat from the. NET command line to build the program.
Write Unit Tests
In unit tests written in NUnit, You need to instantiate the Form to be tested and call its Show () method. Form form = new Form ();
Form. Show ();
For each control you will use in the test, create an appropriate ControlTester instance with its name. ButtonTester button = new ButtonTester ("buttonName ");
Each ControlTester provides some convenient methods to access and control it in a common way. For example, there is a Click method in ButtonTester, and there is a Enter (string Text) method in TextBoxTester.
If you open multiple forms, you can specify the name of the Form containing your control. ButtonTester button = new ButtonTester ("buttonName", "formName ");
If you omit the "formName" parameter, NUnitForms searches for controls in all open forms. this only requires the control name and Form name to be used when the control cannot be uniquely identified (if multiple instances have controls with the same name, the formName must be explicitly stated ).
You can use the controltester class to test any control (of course there is no convenient method). You can access its attributes through attribute indexes like this. Controltester textbox = new controltester ("nameofsometextbox ");
Assertion. assertequals ("defaulttext", textbox ["text"]);
Textbox ["text"] = "newtext ";
You can also try to use the fireevent method to trigger an event of the control: Controltester button = new controltester ("nameofsomebutton ");
Button. fireevent ("click ");
(Note that a convenient button is called here. click ();, but not every control has some convenient methods for calling. of course, the best way is to add a new convenient method or a new controltester and submit it back to nunitforms to enhance its functionality .)
Control name
Nunitforms uses the name attribute of the control to find the control you want to test. If there are multiple controls with the same name in a form, they must be limited as follows: Form
Panela
Usercontrol1
Button (panela. usercontrol1.button)
Usercontrol2
Button (usercontrol2.button)
Panelb
Usercontrol1
Button (panelb. usercontrol1.button)
Note that you only need to make sure that you have sufficient qualifications to become a unique name control. if nunitforms cannot find your control, it will throw a nosuchcontrolexception exception. if the control name is not qualified to make it a unique named control (more qualifications are required), an ambiguousnameexception will be thrown.
Since controls in a form may be dynamically added or deleted, these exceptions will not be thrown unless you actually use them. the qualified Control name can be changed at any time. (The recording program should process these items, and each time it uses the control name as short as possible. this helps the recording program to determine the best form name ...)
Recording Program usage
1.start nunitformsrecorderapplication.exe
2. It will prompt you to browse and select a DLL file containing the form you want to test.
3. when a program is opened, it contains the loaded. the drop-down list of all form classes in the DLL file. select a form from the drop-down list to display your recording program and start an empty unit test.
4. When you operate on the form, the test records your behavior (at least it records nunitforms and recorded activities supported by the recording program ).
5. right-click the control and you can insert an assertion about any attributes of the control. the recording program behavior can be extended to the recording class you write yourself. you can take the original file of the provided recording program class as an example, or the "how to add control recording" section later.
6. Once you write the test metadata, copy and paste it to your unit test class. Of course, you can manually modify it there.
How to add a new controltester
Controltester should:
1. inherit the controltester class, like this: Public class comboboxtester: controltester
2. implement three constructors, as shown in the following code:
Public ComboBoxTester (string name, Form form): base (name, form ){}
Public comboboxtester (string name, string formname): Base (name, formname ){}
Public comboboxtester (string name): Base (name ){}
<P> 3. to support the recording program, you must implement a read-only "properties" attribute, the returned result is an object that contains all attributes (that is, an object that needs to be asserted when you right-click a recording function during recording ). the simplest way to display all attributes is to convert the "control" returned to an appropriate type. (control is a base class that all controls must inherit .)
Public ComboBox properties {
Get {
Return (ComboBox) Control;
}
}
4. implement direct attributes and methods to provide common attributes and interactions: Public void Select (int I ){
Properties. SelectedIndex = I;
}
Public string Text {
Get {
Return Properties. Text;
}
}
5. controlTester distributed by any NunitForms should have a unit test on the functions they provide. the NUnitFormsTestApplications directory contains some small and simple unit tests for typical controls. at each release, I want each Tester to have related Recorders.
How to add a recorder for a control
The Recorder of the control should be:
1. inherit the ControlRecorder class, like this: Public class ComboBoxRecorder: ControlRecorder
2. Rewrite the RecorderType attribute like this. It should return the type of the control recorded by the recording program. Public override Type RecorderType {
Get {
Return typeof (ComboBox );
}
}
3. Repeat the TesterType attribute like this. It should return the corresponding ControlTester. Public override Type TesterType {
Get {
Return typeof (ComboBoxTester );
}
}
4. Implement a constructor like this: Public ComboBoxRecorder (Listener listener): base (listener ){}
5. create a signature-based method for each event you want to record that matches the delegate type of the event you want to record. the method name should be consistent with the event name. listener should be called during implementation. fireEvent (). listener to be reloaded. the version of FireEvent () depends on what you want to record. note that the recording event corresponds to the ControlTester's convenient method. the following are some examples: // Records button. Click ()
Public void Click (object sender, EventArgs args ){
Listener. FireEvent (TesterType, sender, "Click ");
}
// Records: comboBox. Enter ("text ");
Public void TextChanged (object sender, System. EventArgs e ){
Listener. FireEvent (TesterType, sender, "Enter ",
(ComboBox) sender). Text );
}
// Records: comboBox. Select (3); // text of item 3
Public void SelectedIndexChanged (object sender, System. EventArgs e ){
EventAction action = new EventAction ("Select ",
(ComboBox) sender). SelectedIndex );
Action. Comment = (ComboBox) sender). Text;
Listener. FireEvent (TesterType, sender, action );
}
6. in some (hopefully very rare) cases, you may have to rewrite the EventKey method of the base class Recorder. I think I have handled common cases and made necessary extensions in the base class Rcorder of this framework.
----------------------- Full text -------------------------
For the first translation, there must be many poor translations. I hope you can correct me. ^_^
Address: http://nunitforms.sourceforge.net/docs.html
Reprinted please indicate the source (http://leonny.cnblogs.com /).