Testing the application

After we have built all the functions, we can start testing the application. Please note that we have not built any user interface. The basic methods of our application can all be tested without the UI. We will only need to test the UI code if the code was intended to influence the UI.

We need to build a test Codeunit, containing the elements that we would like to test. It should also create the Setup and Master Data that we need.

This can be split into two test code units, but for the sake of simplicity, we have combined them into one.

Testing the application

The first set of test functions implements the data, whereas the second set tests the result. We have a set of helper functions to avoid duplicate code.

One of the tests is the price calculation. We can create a specific record in the price table, and then test whether the price is applied:

[Test] TestPrice()
Stay.GET('STAY0001');
Stay.TESTFIELD(Price, 10);

Test isolation

During development, we might need to run the test multiple times. In order for the test to be complete, we have to start with a fresh situation before each run. This can be achieved by implementing Test Runner, which runs in the TestIsolation mode of the Codeunit.

Test isolation

This guarantees that we can run the result, and the data is not actually stored in the database.

Tip

The MSDN article at http://blogs.msdn.com/b/nav/archive/2012/12/03/writing-unit-tests-in-c-al.aspx explains the writing of test routines, and how the TestIsolation function works.

Analyzing the results

If we run the TestRunner Codeunit from Object Designer, we get a window displaying the result of each test function. If none of the tests fail, the status of the run is a success. Each error is displayed in a window, similar to this:

Analyzing the results

The ASSERTERROR command can be implemented to write code against the expected errors. This allows us to write specific tests against commonly occurring errors, or the typical scenarios that we try to catch.

Tip

The MSDN article at https://msdn.microsoft.com/en-us/library/ee414211.aspx explains the ASSERTERROR function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset