Before starting, make sure you are using a GeneXus version that already supports the Unit Testing Framework.
The easiest way to check this is to take a look at your Genexus IDE menu. It should have a “Test” menu on the menu bar:
If you don't have this option, check here how to install it.
Unit tests are a new type of Object in your GeneXus KB with the particularity of being a special GeneXus Procedure. This means that you can write GeneXus code inside it for testing your objects with different data and validations (assertions). The easiest way to create a unit test is by right-clicking the Object you want to test in the KB Explorer and selecting the “Create Unit Test” option. This option will automatically create a new Unit Test Object that will test the object that you’ve selected.
For this purpose, a new unit test will be automatically generated, where you will probably need to add different data input and output to test different scenarios. If you want to modify the number of data sets generated, you need to set property “Data sets count” in KB test properties.
By default, the test generates 3 objects:
- an SDT for handle input and output structure
- a DataProvider containing the generated test case data sets
- a UnitTest object with the code of the test that you need to modify
Here you can find a video explaining these 3 objects: https://training.genexus.com/gxtest-es/overview-gxtest-4-https://wiki.genexus.com/commwiki/servlet/wiki?38347,Test+Options,#KB+Test+Properties?es#explaining-unit-test-objects]
For example, let's imagine a very simple procedure that concatenates two input strings received by parameter, called "stringConcatenation". So this will be a simple test auto-generated:
This auto-generated code iterates over a collection of data (StringConcatenationUnitTestData), an auto-generated data provider with random data that you will need to edit for your different test scenarios:
So, this data provider enables you to run the test with several data combinations. Please note that the code template is intended to be fully edited to add different inputs and validating output using assertions.
Assertions are used to validate the procedure's result/output during test execution, usually by comparing an output variable with an expected value. Any number of assertions can be added to a Test, each validating some different results. If any of them fail, the Test result will be FAILED.
You can also add database assertions navigating results using FOREACH as usual, comparing database results against your procedure's output.
Lastly, you can also test other types of programmatic objects (with no UI), like Data Providers and Business Components.
* Note the randomly generated sets of data is in the development process in GeneXus Beta version.