Unit test a few small suggestions

xiaoxiao2021-03-06  41

The following examples are carried out under DUnit, but equally applicable to XUnit. It is only the syntax, which recently used Dunit to write programs. Written, and I have encountered some small problems, and I also found some solutions. At the same time, I also found a lot of precautions. mutual encouragement. 1. It is best to replace the display dialog in an abnormality with the ID. Advantages: The dialog cannot be captured. Anomaly can be disadvantage: the program is to join an exception handling mechanism, and request to throw, the amount of code is increased: Question, but if you want to operate in unit testing, you must get this prompt or return to the state that can return, and the plus state word is very troublesome. We can define an exception class, then, then throw. In this way, it can be captured when the unit test is performed. EDisplayException = class (Exception) public ErrorID: Integer; constructor Create (Msg: string; ID: Integer); virtual; end; constructor EDisplayException.Create (Msg: string; ID: Integer); begin ErrorID: = ID; Inherited Create ( MSG); END; When used, you can perform try ..Except on E: EdisplayException do begin E.ERRORID .... you can want to do what END2, separation form and procedure The form is very close to the program, you must do a lot of manual operation, you can't reach the DUnit automatic test. For example, we have such a code. Procedure t1.check (atype: string); var Tmpstr: String; Begin Tmpstr: = INPUTBOX ('Test', 'Enter', TMPSTR); ... END Because the process requested by the user to enter the process, Let our automatic test don't do it, we'd better separate it into two processes. Procedure t1.check (atype: string); var TMPSTR: String; Begin Tmpstr: = InputBox ('test', 'input', tmpstr); T2 (atype, tmpstr); endprocedure t2.check (atype: string; avalue: String); Begin ..end This time we write tests, as long as T2 is tested. 3, pay attention to the display domain of the method. When we test in units, we may often need to test a private method. Although it is not very like, but it will also test, but our app does not allow it to be in private, what should I do, the best way is Add a compilation instruction, such as Tsign = Class {$ IFDEF UnitTest} public {$ else} private {$ ELSE} private {$ ELSE}: bolean; workermustexists: boolean; in our unit testing project defines UnitTest. This solves the problems we need. 4, independent initial and judgment.

For items that may be tested multiple times, it is best to independently independently of the initialization conditions and final judgment results. This makes them reuse. For example, we have such a way. Procedure TMYTEST.TEST1 / / TMY test begin ... a large heap initialization my.do; // This is called the test item. Check (..) .. Check (..) .. Another big pile decision end; in our program, there may be another Test1 method of TMY2 to call TMY, and we have to do once the initialization variable, we will Initialization and results are separated, you can take care of both sides. The shaped TDataSwitchCheck = class public class procedure BeginDownSet; class procedure CheckDownSet. (ACase: TTestCase); end; class procedure TDataSwitchCheck.BeginDownSet; begin // delete the server SystemSetup.ServerAccess.ExecSQL ( 'delete from p_pos_setup where dsection =' 'Unit test' ''); SystemSetup.serveraccess.execsql ('INSERT INTO P_POS_SETUP (DSECTION, Dident, Dvalue, DPOS)' 'Values ​​(' ')', 'test', '' test ' , '' 99 ')');

SystemSetup.serveraccess.execsql ('INSERT INTO P_POS_SETUP (DSECTION, Dident, Dvalue, DPOS)' 'VALUES (' 'unit test' ", '' test ',' 'test', ' quotedstr (systemsetup.posno ) ')');

/ / Remove the locations on the locations.execsql ('delete from p_pos_setup where dsection =' unit test '');

end; class procedure TDataSwitchCheck.CheckDownSet (ACase: TTestCase); var tmpString: string; begin SystemSetup.ClientAccess.GetSQLValue ( 'select dsection from p_pos_setup where dsection =' 'unit test' 'and dpos =' QuotedStr (SystemSetup.PosNo) , tmpstring; acase.check (tmpstring = 'unit test', 'download this POS set error');

Systemsetup.clientAccess.getsqlvalue ('select dsection from p_pos_setup where dsection =' unit test '' and dpos = '' 99 '', tmpstring); acase.check (tmpstring = '', "Download does not belong to this POS machine Setup error '); END; // The following is the test case procataSwitchtests.testdownSet; begin tdataSwitchCheck.begindownSet; // Initialize systemsetup.DataSwitch.downset (nil); // This is a method of test.

TDataSwitchCheck.checkdownSet // Check results end; // another test case. procedure TFrm_DataSwtichTests.TestStartSwtich; begin TDataSwitchCheck.BeginDownRight; TDataSwitchCheck.BeginDownSet; TDataSwitchCheck.BeginDownKey; TDataSwitchCheck.BeginUpdateSet; TDataSwitchCheck.BeginSaleDataUp;

TFRM_DATASWTICH.Alldataswtich;

TDataSwitchCheck.CheckDownRight (Self); TDataSwitchCheck.CheckDownSet (Self); TDataSwitchCheck.CheckDownKey (Self); TDataSwitchCheck.CheckUpdateSet (Self); TDataSwitchCheck.CheckSaleDataUp (Self);

END;

转载请注明原文地址:https://www.9cbs.com/read-54929.html

New Post(0)