Edukiak
Indizea 🔎︎

# SFUnitTests.UnitTest zerbitzua

UnitTest zerbitzuak unitate-probak automatizatzeko lan-markoa eskaintzen du Basic lengoaiarako. Besteak beste, honakoak egin ditzake:

- proba-kasuak proba-suitetan eta unitate-probetan bildu.

- Unitate-proben artean konfiguraziorako eta itzaltzerako kodea partekatu.

- Proben emaitzak jakinarazi Console bidez.

Bai unitate-proba bai probatuko den kodea Basic lengoaian idatzi behar dira. Probatuko den kodeak beste lengoaia batzuetan idatzitako funtzioei dei diezaieke.

UnitTest zerbitzua ezin da erabili Python scriptekin.

## Definizioak

### Proba-kasua

Proba-kasu bat probetarako banakako unitatea da. Sarrera multzo jakin baten erantzun espezifikoa egiaztatzen du.

In the UnitTest service, a test case is represented by a single Basic Sub whose name starts with a common prefix (the default is "Test_").

The test case fails if one of the AssertX methods returns False.

### Proba-suitea

Proba-suite bat elkarrekin exekutatu daitezkeen proba-kasuen bilduma bat da.

Proba-suite bateko kasu guztiak Basic modulu bakar batean biltegiratzen dira.

A test suite may implement the SetUp and TearDown methods to prepare for test cases in its module.

### Unitate-proba

Unitate-proba bat Basic liburutegi bereko proba-suiten multzo bat da.

## Zerbitzuari deitzea

UnitTest zerbitzua erabiltzeko, ScriptForge liburutegia kargatu edo inportatu behar da:

• Basic makroak erabiltzkeo, ScriptForge liburutegia kargatu behar da honako instrukzioa erabiliz:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Python scriptak erabiltzeko, berriz, scriptforge modulu baten inportazioa behar dute:
from scriptforge import CreateScriptService

### Modu sinplea

Invoke the service in simple mode to call AssertX functions without having to build the full hierarchy of test suites and test cases.

Modu sinplean, zerbitzuari testu-probaren barruan deitzen zaio, beheko adibidean erakusten den moduan:

        Sub SimpleTest
            On Local Error GoTo CatchError
            Dim myTest As Variant
            myTest = CreateScriptService("UnitTest")
            ' Simulatutako proba batzuk
            myTest.AssertEqual(1 + 1, 2)
            myTest.AssertEqual(1 - 1, 0)
            MsgBox("Proba guztiak gainditu dira")
            Exit Sub
        CatchError:
            myTest.ReportError("Proba batek huts egin du")
        End Sub

Honako adibidean, AssertEqual deietako edozeinek huts egiten badu, interpretatzailea CatchError etiketara joango da eta errorearen berri emango du ReportError metodoari deituta.

### Modu osoa

Modu osoan deitua izatean, zerbitzuaren sorrera proba-kodetik kanpo gertatzen da eta proba guztiak proba-kasuetan eta proba-suitetan antolatzen dira liburutegi bakar batean.

The following example creates a UnitTest instance whose tests are located inside the current document (ThisComponent) in the "Tests" library.

        GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
        Dim myUnitTest As Variant
        myUnitTest = CreateScriptService("UnitTest", ThisComponent, "Tests")

### Modu osoko adibide minimalista bat

Kontuan izan ODS fitxategi batek "MathUtils" izeneko modulua duela "Standard" liburutegian, eta modulu horrek honako kodea duela:

        ' Standard.MathUtils moduluko kodea
        Function Sum(a, b) As Double
            Sum = a + b
        End Function

        Function Multiply(a, b) As Double
            Multiply = a * b
        End Function

To create a full test suite, consider that a new library named "Tests" is created in the file with a single module "AllTests" containing the code below:

        ' Code in module Tests.AllTests
        Sub Main()
            GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
            Dim test As Variant
            test = CreateScriptService("UnitTest", ThisComponent, "Tests")
            test.RunTest("AllTests")
            test.Dispose()
        End Sub

        Sub Setup(test)
            ' Lehen proba-kasuaren aurretik exekutatzen den prestakuntza-kodea
            Dim exc As Variant
            exc = CreateScriptService("Exception")
            exc.Console(Modal := False)
        End Sub

        Sub TearDown(test)
            ' Azken proba-kasuaren ondoren deitutako garbiketa-kodea, aukerakoa
        End Sub

        Sub Test_Sum(test)
            On Local Error GoTo CatchError
            test.AssertEqual(Sum(1, 1), 2, "Bi osoko balio positiboren batuketa")
            test.AssertEqual(Sum(-10, 20), 10, "Osoko balio negatibo eta positiboen batuketa")
            test.AssertEqual(Sum(1.5, 1), 2.5, "Sum of float and integer values")
            Exit Sub
        CatchError:
            test.ReportError("Sum method is broken")
        End Sub

        Sub Test_Multiply(test)
            On Local Error GoTo CatchError
            test.AssertEqual(Multiply(2, 2), 4, "Multiply two positive integers")
            test.AssertEqual(Multiply(-4, 2), -8, "Multiply negative and positive integers")
            test.AssertEqual(Multiply(1.5, 3), 4.5, "Multiply of float and integer values")
            Exit Sub
        CatchError:
            test.ReportError("Multiply method is broken")
        End Sub

The test suite above consists of two test cases Test_Sum and Test_Multiply. To run all tests simply run the Main method from the "AllTests" module.

The Console from the Exception service is used as the default output to print test results. After running the example above, the following output will be displayed in the console:

        ' RUNTEST ENTER testsuite='Tests.AllTests', pattern='Test_*'
        '   SETUP Tests.AllTests.Setup() ENTER
        '   SETUP Tests.AllTests.Setup() EXIT
        '   TESTCASE Tests.AllTests.Test_Multiply() ENTER
        '   TESTCASE Tests.AllTests.Test_Multiply() EXIT (0,017 sec)
        '   TESTCASE Tests.AllTests.Test_Sum() ENTER
        '   TESTCASE Tests.AllTests.Test_Sum() EXIT (0,016 sec)
        '   TEARDOWN Tests.AllTests.TearDown() ENTER
        '   TEARDOWN Tests.AllTests.TearDown() EXIT
        ' RUNTEST EXIT testsuite='Tests.AllTests' (0,223 sec)

AssertEqual metodoetako batek huts egiten badu probetan zehar, errore-mezu bat gehituko zaio kontsolari.

## Propietateak

Izena | Irakurtzeko soilik | Mota | Deskribapena
--- | --- | --- | ---
LongMessage | Ez | Boolean | When set to True (default) the console shows the standard message appended to the message provided by the tester. When False, only the message defined by the tester is used.
ReturnCode | Bai | Integer | Value returned by RunTest after the unit test is finished. Next is a list of possible values: 0 - Test finished without errors or test not started 1 - An assertion within a test case returned False 2 - A SkipTest was issued by the Setup method or by one of the test cases. 3 - Abnormal end of test
Verbose | Ez | Boolean | When set to True, all assertions are reported in the console (failing or not). When False (default), only failing assertions are reported.
WhenAssertionFails | Ez | Integer | Defines what is done when an assertion fails. Next is a list of possible values: 0 - Ignore the failure and continue running the test 1 - The TearDown method in the module is executed in the current test suite and the next suite is started (default in full mode). 2 - Stop immediately (default in simple mode)

List of Methods in the UnitTest Service
AssertAlmostEqual AssertEqual AssertFalse AssertGreater AssertGreaterEqual AssertIn AssertIsInstance AssertIsNothing AssertLike | AssertNotRegex AssertIsNull AssertLess AssertLessEqual AssertNotAlmostEqual AssertNotEqual AssertNotIn AssertNotInstance AssertNotLike | AssertNotNothing AssertNotNull AssertRegex AssertTrue Fail Log ReportError RunTest SkipTest

## Arguments of the AssertX methods

All assertions test one or two expressions, referred in the remainder of this help page as A and B. They are always the first one or two arguments in the AssertX method.

All AssertX methods accept a message argument specifying a custom message to be reported in the console regarding the assertion. By default an empty string is used. This argument is always in the last position of the assertion.

Zenbait AssertX metodok argumentu gehigarriak ere onartzen dituzte, behean azaltzen diren sintaxien arabera.

## AssertAlmostEqual

True (egia) itzultzen du A eta B zenbakizko balioak direnean eta elkarrekiko hurbil daudela kontsideratzen denean, emandako tolerantzia baten arabera.

### Sintaxia:

svc.AssertAlmostEqual(a: any, b: any, tolerance: double = 1E-09, message: str = ""): bool

True itzultzen du beheko bi baldintzak betetzen badira:

- A and B can be converted to the Double type.

- The absolute difference between A and B divided by the largest absolute value of A or B is lower than the value specified in tolerance.

## AssertEqual

Returns True when A and B are considered to be equal.

### Sintaxia:

svc.AssertEqual(a: any, b: any, message: str = ""): bool

When A and B are scalars, True is returned if:

- Both expressions have the same VarType or are both numeric.

- Booleans and numeric values are compared with the = operator.

- Strings are compared with the builtin StrComp function. The comparison is case-sensitive.

- Dates and times are compared up to the second.

- Null, Empty and Nothing are not equal, but AssertEqual(Nothing, Nothing) returns True.

- UNO objects are compared with the builtin EqualUnoObjects method.

- Note that Basic objects are never equal.

When A and B are arrays, True is returned if:

- Both arrays have the same number of dimensions (up to 2 dimensions) and their lower and upper bounds are identical for all dimensions.

- All items in both arrays are equal, one by one.

- Two empty arrays are considered to be equal.

## AssertFalse

Returns True when the type of A is Boolean and its value is False.

### Sintaxia:

svc.AssertFalse(a: any, message: str = ""): bool

## AssertGreater

Returns True when A is greater than B.

### Sintaxia:

svc.AssertGreater(a: any, b: any, message: str = ""): bool

The comparison between A and B assumes the following:

- Eligible data types are String, Date or numeric.

- Bi adierazpenek VarType bera izan behar dute edo biek zenbakizkoak izan behar dute.

- Kateen arteko konparazioetan maiuskulak/minuskulak kontuan hartzen dira.

## AssertGreaterEqual

True (egia) itzultzen du A balioa B balioa baino handiagoa edo berdina bada.

### Sintaxia:

svc.AssertGreaterEqual(a: any, b: any, message: str = ""): bool

A eta B arteko konparazioak honakoa onartzen du:

- Eligible data types are String, Date or numeric.

- Both expressions must have the same VarType or both must be numeric.

- Kateen arteko konparazioetan maiuskulak/minuskulak kontuan hartzen dira.

## AssertIn

True itzultzen du A balioa B balioan aurkitzen bada.

### Sintaxia:

svc.AssertIn(a: any, b: any, message: str = ""): bool

Baieztapen horrek honakoa onartzen du:

- B adierazpena izan daiteke 1D matrize bat, ScriptForge Dictionary objektu bat edo kate bat.

- B adierazpena 1D matrize bat bada, A adierazpena data bat edo zenbakizko balio bat izan daiteke.

- B adierazpena ScriptForge Dictionary objektua bada, A katea arakatzen da Bko kateen bila.

- Kateen konparazioetan maiuskulak/minuskulak kontuan hartzen dira.

## AssertIsInstance

True itzultzen du A zehaztutako objektu motaren instantzia bat denean. Objektua motaren izena duen kate gisa zehaztu behar da.

### Sintaxia:

svc.AssertIsInstance(a: any, objecttype: str, message: str = ""): bool

A adierazpena honakoetako bat izan daiteke:

- ScriptForge objektua. Kasu horretan, objecttype argumentua kate bat da, adibidez "DICTIONARY", "calc", "Dialog", etab.

- UNO objektua. Kasu horretan, objecttype argumentuak SF_Session.UnoObjectType() metodoak itzultzen dituen balioen berdina den kate bat izan behar du.

- Matrize bat. Kasu horretan, objecttype argumentuak "array" izan behar du.

- Beste edozein aldagai (Object zein Array ). Kasu horretan, objecttype zera da, integratutako TypeName funtzioak itzultzen duen balioarekin bat datorren kate bat.

## AssertIsNothing

True itzultzen du A Nothing balioa duen objektu bat bada.

### Sintaxia:

svc.AssertIsNothing(a: any, message: str = ""): bool

## AssertIsNull

True itzultzen du Ak Null balioa duenean.

### Sintaxia:

svc.AssertIsNull(a: any, message: str = ""): bool

## AssertLess

True itzultzen du A B baino txikiagoa denean.

### Sintaxia:

svc.AssertLess(a: any, b: any, message: str = ""): bool

A eta B arteko konparazioak honakoa onartzen du:

- Eligible data types are String, Date or numeric.

- Bi adierazpenek VarType mota bera izan behar dute edo biek zenbakizkoak izan behar dute.

- Kateen arteko konparazioek maiuskulak eta minuskulak kontuan hartzen dituzte.

## AssertLessEqual

True (egia) itzultzen du A balioa B balioa baino txikiagoa edo berdina denean.

### Sintaxia:

svc.AssertLessEqual(a: any, b: any, message: str = ""): bool

A eta B arteko konparazioak honakoa onartzen du:

- Eligible data types are String, Date or numeric.

- Both expressions must have the same VarType or both must be numeric.

- String comparisons are case-sensitive.

## AssertLike

Returns True if string A matches a given pattern containing wildcards.

### Sintaxia:

svc.AssertLike(a: any, pattern: str = "", message: str = ""): bool

The following wildcards are accepted:

- ? - Represents any single character.

- * - Represents zero, one, or multiple characters.

## AssertNotAlmostEqual

Returns True when A and B are numerical values and are not considered to be close to each other, given a relative tolerance.

### Sintaxia:

svc.AssertNotAlmostEqual(a: any, b: any, tolerance: double = 1E-09, message: str = ""): bool

This assertion returns True if the two conditions below are met:

- A and B can be converted to the Double type.

- The absolute difference between A and B divided by the largest absolute value of A or B is greater than the value specified in tolerance.

## AssertNotEqual

Returns True when A and B are not considered to be equal.

### Sintaxia:

svc.AssertNotEqual(a: any, b: any, message: str = ""): bool

This method works both for scalars and arrays. Read the instructions in AssertEqual for more information on what equality means in this assertion.

## AssertNotIn

Returns True when A (a string) is not found in B.

### Sintaxia:

svc.AssertNotIn(a: any, b: any, message: str = ""): bool

Read the instructions in AssertIn for more information on the assumptions of this method.

## AssertNotInstance

Returns True when A is not an instance of a specified object type.

### Sintaxia:

svc.AssertNotInstance(a: any, objecttype: str, message: str = ""): bool

Read the instructions in AssertIsInstance for more information on the assumptions of this method.

## AssertNotLike

Returns True if string A does not match a given pattern containing wildcards.

### Sintaxia:

svc.AssertNotLike(a: any, pattern: str = "", message: str = ""): bool

Read the instructions in AssertLike for more information on the assumptions of this method.

## AssertNotNothing

Returns True except when A is an object that has the Nothing value.

### Sintaxia:

svc.AssertNotNothing(a: any, message: str = ""): bool

## AssertNotNull

Returns True except when A has the Null value.

### Sintaxia:

svc.AssertNotNull(a: any, message: str = ""): bool

## AssertNotRegex

Returns True when A is not a string or does not match the given regular expression.

### Sintaxia:

svc.AssertNotRegex(a: any, regex: str = "", message: str = ""): bool

Konparazioan maiuskulak/minuskulak kontuan hartzen dira.

## AssertRegex

True itzultzen du A katea emandako adierazpen erregularrarekin bat datorrenean.

### Sintaxia:

svc.AssertRegex(a: any, regex: str = "", message: str = ""): bool

Konparazioan maiuskulak/minuskulak kontuan hartzen dira.

## AssertTrue

Returns True when expression A is a Boolean and its value is True.

### Sintaxia:

svc.AssertTrue(a: any, message: str = ""): bool

## Fail

Proba-kasu batek huts egin dezan behartzen du.

### Sintaxia:

svc.Fail(message: str = "")

Mezu bat eman daiteke kontsolan agertu dadin.

## Log

Zehaztutako message mezua kontsolan idazten du.

### Sintaxia:

svc.Log(message: str = "")

Mezu bat eman daiteke kontsolan agertu dadin.

## ReportError

Exception zerbitzuaren uneko propietate-balioa eta mezu bat dituen koadro bat erakusten du.

This method is commonly used in the exception handling section of the Sub containing the test case, which is reached when an assertion fails or when the Fail method is called.

### Sintaxia:

svc.ReportError(message: str = "")

Depending on the value of the property WhenAssertionFails, the test execution may continue or be interrupted.

When writing test cases it is recommended to include a call to the ReportError method in the exception handling section of the Sub.

If the property LongMessage is equal to True, the specified message is followed by the standard error message description. Otherwise only the message is displayed.

## RunTest

Zehaztutako moduluan inplementatutako proba-suite osoa exekutatzen du. Proba-kasuak bakoitza bere aldetik exekutatzen dira.

Running a test suite consists of:

1. Executing the optional Setup method present in the module.

2. Executing once each test case, in no specific order.

3. Executing the optional TearDown method present in the module.

### Sintaxia:

svc.RunTest(testsuite: str, testcasepattern: str = "", message: str = ""): int

The argument testcasepattern specifies a pattern composed of "?" and "*" wildcards to select which test cases will be run. The comparison is not case-sensitive.

If a message is provided, it is written to the console when the test starts.

## SkipTest

Interrupts the running test suite without calling the TearDown method.

Skipping a test is usually meaningful during the Setup method when not all conditions to run the test are met.

It is up to the Setup method to exit the Sub shortly after the SkipTest call.

If SkipTest is called from within a test case, the execution of the test suite is interrupted and the remaining test cases are not run. Keep in mind that the order in which test cases are run is arbitrary within a test suite.

### Sintaxia:

svc.SkipTest(message: str = "")

If a message is provided, it is written to the console.

Erlazionatutako gaiak

ScriptForge.Exception zerbitzua